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

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

Pull IOMMU updates from Joerg Roedel:
"Slightly more changes than usual this time:

- KDump Kernel IOMMU take-over code for AMD IOMMU. The code now tries
to preserve the mappings of the kernel so that master aborts for
devices are avoided. Master aborts cause some devices to fail in
the kdump kernel, so this code makes the dump more likely to
succeed when AMD IOMMU is enabled.

- common flush queue implementation for IOVA code users. The code is
still optional, but AMD and Intel IOMMU drivers had their own
implementation which is now unified.

- finish support for iommu-groups. All drivers implement this feature
now so that IOMMU core code can rely on it.

- finish support for 'struct iommu_device' in iommu drivers. All
drivers now use the interface.

- new functions in the IOMMU-API for explicit IO/TLB flushing. This
will help to reduce the number of IO/TLB flushes when IOMMU drivers
support this interface.

- support for mt2712 in the Mediatek IOMMU driver

- new IOMMU driver for QCOM hardware

- system PM support for ARM-SMMU

- shutdown method for ARM-SMMU-v3

- some constification patches

- various other small improvements and fixes"

* tag 'iommu-updates-v4.14' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu: (87 commits)
iommu/vt-d: Don't be too aggressive when clearing one context entry
iommu: Introduce Interface for IOMMU TLB Flushing
iommu/s390: Constify iommu_ops
iommu/vt-d: Avoid calling virt_to_phys() on null pointer
iommu/vt-d: IOMMU Page Request needs to check if address is canonical.
arm/tegra: Call bus_set_iommu() after iommu_device_register()
iommu/exynos: Constify iommu_ops
iommu/ipmmu-vmsa: Make ipmmu_gather_ops const
iommu/ipmmu-vmsa: Rereserving a free context before setting up a pagetable
iommu/amd: Rename a few flush functions
iommu/amd: Check if domain is NULL in get_domain() and return -EBUSY
iommu/mediatek: Fix a build warning of BIT(32) in ARM
iommu/mediatek: Fix a build fail of m4u_type
iommu: qcom: annotate PM functions as __maybe_unused
iommu/pamu: Fix PAMU boot crash
memory: mtk-smi: Degrade SMI init to module_init
iommu/mediatek: Enlarge the validate PA range for 4GB mode
iommu/mediatek: Disable iommu clock when system suspend
iommu/mediatek: Move pgtable allocation into domain_alloc
iommu/mediatek: Merge 2 M4U HWs into one iommu domain
...

+2883 -1235
+121
Documentation/devicetree/bindings/iommu/qcom,iommu.txt
··· 1 + * QCOM IOMMU v1 Implementation 2 + 3 + Qualcomm "B" family devices which are not compatible with arm-smmu have 4 + a similar looking IOMMU but without access to the global register space, 5 + and optionally requiring additional configuration to route context irqs 6 + to non-secure vs secure interrupt line. 7 + 8 + ** Required properties: 9 + 10 + - compatible : Should be one of: 11 + 12 + "qcom,msm8916-iommu" 13 + 14 + Followed by "qcom,msm-iommu-v1". 15 + 16 + - clock-names : Should be a pair of "iface" (required for IOMMUs 17 + register group access) and "bus" (required for 18 + the IOMMUs underlying bus access). 19 + 20 + - clocks : Phandles for respective clocks described by 21 + clock-names. 22 + 23 + - #address-cells : must be 1. 24 + 25 + - #size-cells : must be 1. 26 + 27 + - #iommu-cells : Must be 1. Index identifies the context-bank #. 28 + 29 + - ranges : Base address and size of the iommu context banks. 30 + 31 + - qcom,iommu-secure-id : secure-id. 32 + 33 + - List of sub-nodes, one per translation context bank. Each sub-node 34 + has the following required properties: 35 + 36 + - compatible : Should be one of: 37 + - "qcom,msm-iommu-v1-ns" : non-secure context bank 38 + - "qcom,msm-iommu-v1-sec" : secure context bank 39 + - reg : Base address and size of context bank within the iommu 40 + - interrupts : The context fault irq. 41 + 42 + ** Optional properties: 43 + 44 + - reg : Base address and size of the SMMU local base, should 45 + be only specified if the iommu requires configuration 46 + for routing of context bank irq's to secure vs non- 47 + secure lines. (Ie. if the iommu contains secure 48 + context banks) 49 + 50 + 51 + ** Examples: 52 + 53 + apps_iommu: iommu@1e20000 { 54 + #address-cells = <1>; 55 + #size-cells = <1>; 56 + #iommu-cells = <1>; 57 + compatible = "qcom,msm8916-iommu", "qcom,msm-iommu-v1"; 58 + ranges = <0 0x1e20000 0x40000>; 59 + reg = <0x1ef0000 0x3000>; 60 + clocks = <&gcc GCC_SMMU_CFG_CLK>, 61 + <&gcc GCC_APSS_TCU_CLK>; 62 + clock-names = "iface", "bus"; 63 + qcom,iommu-secure-id = <17>; 64 + 65 + // mdp_0: 66 + iommu-ctx@4000 { 67 + compatible = "qcom,msm-iommu-v1-ns"; 68 + reg = <0x4000 0x1000>; 69 + interrupts = <GIC_SPI 70 IRQ_TYPE_LEVEL_HIGH>; 70 + }; 71 + 72 + // venus_ns: 73 + iommu-ctx@5000 { 74 + compatible = "qcom,msm-iommu-v1-sec"; 75 + reg = <0x5000 0x1000>; 76 + interrupts = <GIC_SPI 70 IRQ_TYPE_LEVEL_HIGH>; 77 + }; 78 + }; 79 + 80 + gpu_iommu: iommu@1f08000 { 81 + #address-cells = <1>; 82 + #size-cells = <1>; 83 + #iommu-cells = <1>; 84 + compatible = "qcom,msm8916-iommu", "qcom,msm-iommu-v1"; 85 + ranges = <0 0x1f08000 0x10000>; 86 + clocks = <&gcc GCC_SMMU_CFG_CLK>, 87 + <&gcc GCC_GFX_TCU_CLK>; 88 + clock-names = "iface", "bus"; 89 + qcom,iommu-secure-id = <18>; 90 + 91 + // gfx3d_user: 92 + iommu-ctx@1000 { 93 + compatible = "qcom,msm-iommu-v1-ns"; 94 + reg = <0x1000 0x1000>; 95 + interrupts = <GIC_SPI 241 IRQ_TYPE_LEVEL_HIGH>; 96 + }; 97 + 98 + // gfx3d_priv: 99 + iommu-ctx@2000 { 100 + compatible = "qcom,msm-iommu-v1-ns"; 101 + reg = <0x2000 0x1000>; 102 + interrupts = <GIC_SPI 242 IRQ_TYPE_LEVEL_HIGH>; 103 + }; 104 + }; 105 + 106 + ... 107 + 108 + venus: video-codec@1d00000 { 109 + ... 110 + iommus = <&apps_iommu 5>; 111 + }; 112 + 113 + mdp: mdp@1a01000 { 114 + ... 115 + iommus = <&apps_iommu 4>; 116 + }; 117 + 118 + gpu@01c00000 { 119 + ... 120 + iommus = <&gpu_iommu 1>, <&gpu_iommu 2>; 121 + };
+5
Documentation/devicetree/bindings/iommu/rockchip,iommu.txt
··· 15 15 to associate with its master device. See: 16 16 Documentation/devicetree/bindings/iommu/iommu.txt 17 17 18 + Optional properties: 19 + - rockchip,disable-mmu-reset : Don't use the mmu reset operation. 20 + Some mmu instances may produce unexpected results 21 + when the reset operation is used. 22 + 18 23 Example: 19 24 20 25 vopl_mmu: iommu@ff940300 {
+15
Documentation/devicetree/bindings/memory-controllers/mediatek,smi-larb.txt
··· 15 15 the register. 16 16 - "smi" : It's the clock for transfer data and command. 17 17 18 + Required property for mt2701: 19 + - mediatek,larb-id :the hardware id of this larb. 20 + 18 21 Example: 19 22 larb1: larb@16010000 { 20 23 compatible = "mediatek,mt8173-smi-larb"; ··· 27 24 clocks = <&vdecsys CLK_VDEC_CKEN>, 28 25 <&vdecsys CLK_VDEC_LARB_CKEN>; 29 26 clock-names = "apb", "smi"; 27 + }; 28 + 29 + Example for mt2701: 30 + larb0: larb@14010000 { 31 + compatible = "mediatek,mt2701-smi-larb"; 32 + reg = <0 0x14010000 0 0x1000>; 33 + mediatek,smi = <&smi_common>; 34 + mediatek,larb-id = <0>; 35 + clocks = <&mmsys CLK_MM_SMI_LARB0>, 36 + <&mmsys CLK_MM_SMI_LARB0>; 37 + clock-names = "apb", "smi"; 38 + power-domains = <&scpsys MT2701_POWER_DOMAIN_DISP>; 30 39 };
+7
MAINTAINERS
··· 11117 11117 S: Supported 11118 11118 F: arch/hexagon/ 11119 11119 11120 + QUALCOMM IOMMU 11121 + M: Rob Clark <robdclark@gmail.com> 11122 + L: iommu@lists.linux-foundation.org 11123 + L: linux-arm-msm@vger.kernel.org 11124 + S: Maintained 11125 + F: drivers/iommu/qcom_iommu.c 11126 + 11120 11127 QUALCOMM VENUS VIDEO ACCELERATOR DRIVER 11121 11128 M: Stanimir Varbanov <stanimir.varbanov@linaro.org> 11122 11129 L: linux-media@vger.kernel.org
+7
arch/s390/include/asm/pci.h
··· 8 8 9 9 #include <linux/pci.h> 10 10 #include <linux/mutex.h> 11 + #include <linux/iommu.h> 11 12 #include <asm-generic/pci.h> 12 13 #include <asm/pci_clp.h> 13 14 #include <asm/pci_debug.h> ··· 123 122 unsigned long iommu_pages; 124 123 unsigned int next_bit; 125 124 125 + struct iommu_device iommu_dev; /* IOMMU core handle */ 126 + 126 127 char res_name[16]; 127 128 struct zpci_bar_struct bars[PCI_BAR_COUNT]; 128 129 ··· 176 173 int clp_enable_fh(struct zpci_dev *, u8); 177 174 int clp_disable_fh(struct zpci_dev *); 178 175 int clp_get_state(u32 fid, enum zpci_state *state); 176 + 177 + /* IOMMU Interface */ 178 + int zpci_init_iommu(struct zpci_dev *zdev); 179 + void zpci_destroy_iommu(struct zpci_dev *zdev); 179 180 180 181 #ifdef CONFIG_PCI 181 182 /* Error handling and recovery */
+8 -1
arch/s390/pci/pci.c
··· 772 772 773 773 zpci_exit_slot(zdev); 774 774 zpci_cleanup_bus_resources(zdev); 775 + zpci_destroy_iommu(zdev); 775 776 zpci_free_domain(zdev); 776 777 777 778 spin_lock(&zpci_list_lock); ··· 845 844 if (rc) 846 845 goto out; 847 846 847 + rc = zpci_init_iommu(zdev); 848 + if (rc) 849 + goto out_free; 850 + 848 851 mutex_init(&zdev->lock); 849 852 if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 850 853 rc = zpci_enable_device(zdev); 851 854 if (rc) 852 - goto out_free; 855 + goto out_destroy_iommu; 853 856 } 854 857 rc = zpci_scan_bus(zdev); 855 858 if (rc) ··· 870 865 out_disable: 871 866 if (zdev->state == ZPCI_FN_STATE_ONLINE) 872 867 zpci_disable_device(zdev); 868 + out_destroy_iommu: 869 + zpci_destroy_iommu(zdev); 873 870 out_free: 874 871 zpci_free_domain(zdev); 875 872 out:
+13
drivers/iommu/Kconfig
··· 76 76 77 77 config FSL_PAMU 78 78 bool "Freescale IOMMU support" 79 + depends on PCI 80 + depends on PHYS_64BIT 79 81 depends on PPC_E500MC || (COMPILE_TEST && PPC) 80 82 select IOMMU_API 81 83 select GENERIC_ALLOCATOR ··· 255 253 config EXYNOS_IOMMU 256 254 bool "Exynos IOMMU Support" 257 255 depends on ARCH_EXYNOS && MMU 256 + depends on !CPU_BIG_ENDIAN # revisit driver if we can enable big-endian ptes 258 257 select IOMMU_API 259 258 select ARM_DMA_USE_IOMMU 260 259 help ··· 369 366 DMA memory accesses for the multimedia subsystem. 370 367 371 368 if unsure, say N here. 369 + 370 + config QCOM_IOMMU 371 + # Note: iommu drivers cannot (yet?) be built as modules 372 + bool "Qualcomm IOMMU Support" 373 + depends on ARCH_QCOM || COMPILE_TEST 374 + select IOMMU_API 375 + select IOMMU_IO_PGTABLE_LPAE 376 + select ARM_DMA_USE_IOMMU 377 + help 378 + Support for IOMMU on certain Qualcomm SoCs. 372 379 373 380 endif # IOMMU_SUPPORT
+1
drivers/iommu/Makefile
··· 27 27 obj-$(CONFIG_EXYNOS_IOMMU) += exynos-iommu.o 28 28 obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o 29 29 obj-$(CONFIG_S390_IOMMU) += s390-iommu.o 30 + obj-$(CONFIG_QCOM_IOMMU) += qcom_iommu.o
+49 -264
drivers/iommu/amd_iommu.c
··· 103 103 static const struct dma_map_ops amd_iommu_dma_ops; 104 104 105 105 /* 106 - * This struct contains device specific data for the IOMMU 107 - */ 108 - struct iommu_dev_data { 109 - struct list_head list; /* For domain->dev_list */ 110 - struct list_head dev_data_list; /* For global dev_data_list */ 111 - struct protection_domain *domain; /* Domain the device is bound to */ 112 - u16 devid; /* PCI Device ID */ 113 - u16 alias; /* Alias Device ID */ 114 - bool iommu_v2; /* Device can make use of IOMMUv2 */ 115 - bool passthrough; /* Device is identity mapped */ 116 - struct { 117 - bool enabled; 118 - int qdep; 119 - } ats; /* ATS state */ 120 - bool pri_tlp; /* PASID TLB required for 121 - PPR completions */ 122 - u32 errata; /* Bitmap for errata to apply */ 123 - bool use_vapic; /* Enable device to use vapic mode */ 124 - 125 - struct ratelimit_state rs; /* Ratelimit IOPF messages */ 126 - }; 127 - 128 - /* 129 106 * general struct to manage commands send to an IOMMU 130 107 */ 131 108 struct iommu_cmd { ··· 114 137 static void update_domain(struct protection_domain *domain); 115 138 static int protection_domain_init(struct protection_domain *domain); 116 139 static void detach_device(struct device *dev); 117 - 118 - #define FLUSH_QUEUE_SIZE 256 119 - 120 - struct flush_queue_entry { 121 - unsigned long iova_pfn; 122 - unsigned long pages; 123 - u64 counter; /* Flush counter when this entry was added to the queue */ 124 - }; 125 - 126 - struct flush_queue { 127 - struct flush_queue_entry *entries; 128 - unsigned head, tail; 129 - spinlock_t lock; 130 - }; 140 + static void iova_domain_flush_tlb(struct iova_domain *iovad); 131 141 132 142 /* 133 143 * Data container for a dma_ops specific protection domain ··· 125 161 126 162 /* IOVA RB-Tree */ 127 163 struct iova_domain iovad; 128 - 129 - struct flush_queue __percpu *flush_queue; 130 - 131 - /* 132 - * We need two counter here to be race-free wrt. IOTLB flushing and 133 - * adding entries to the flush queue. 134 - * 135 - * The flush_start_cnt is incremented _before_ the IOTLB flush starts. 136 - * New entries added to the flush ring-buffer get their 'counter' value 137 - * from here. This way we can make sure that entries added to the queue 138 - * (or other per-cpu queues of the same domain) while the TLB is about 139 - * to be flushed are not considered to be flushed already. 140 - */ 141 - atomic64_t flush_start_cnt; 142 - 143 - /* 144 - * The flush_finish_cnt is incremented when an IOTLB flush is complete. 145 - * This value is always smaller than flush_start_cnt. The queue_add 146 - * function frees all IOVAs that have a counter value smaller than 147 - * flush_finish_cnt. This makes sure that we only free IOVAs that are 148 - * flushed out of the IOTLB of the domain. 149 - */ 150 - atomic64_t flush_finish_cnt; 151 - 152 - /* 153 - * Timer to make sure we don't keep IOVAs around unflushed 154 - * for too long 155 - */ 156 - struct timer_list flush_timer; 157 - atomic_t flush_timer_on; 158 164 }; 159 165 160 166 static struct iova_domain reserved_iova_ranges; ··· 305 371 static struct iommu_dev_data *find_dev_data(u16 devid) 306 372 { 307 373 struct iommu_dev_data *dev_data; 374 + struct amd_iommu *iommu = amd_iommu_rlookup_table[devid]; 308 375 309 376 dev_data = search_dev_data(devid); 310 377 311 - if (dev_data == NULL) 378 + if (dev_data == NULL) { 312 379 dev_data = alloc_dev_data(devid); 380 + 381 + if (translation_pre_enabled(iommu)) 382 + dev_data->defer_attach = true; 383 + } 313 384 314 385 return dev_data; 315 386 } 316 387 317 - static struct iommu_dev_data *get_dev_data(struct device *dev) 388 + struct iommu_dev_data *get_dev_data(struct device *dev) 318 389 { 319 390 return dev->archdata.iommu; 320 391 } 392 + EXPORT_SYMBOL(get_dev_data); 321 393 322 394 /* 323 395 * Find or create an IOMMU group for a acpihid device. ··· 1107 1167 return iommu_queue_command(iommu, &cmd); 1108 1168 } 1109 1169 1110 - static void iommu_flush_dte_all(struct amd_iommu *iommu) 1170 + static void amd_iommu_flush_dte_all(struct amd_iommu *iommu) 1111 1171 { 1112 1172 u32 devid; 1113 1173 ··· 1121 1181 * This function uses heavy locking and may disable irqs for some time. But 1122 1182 * this is no issue because it is only called during resume. 1123 1183 */ 1124 - static void iommu_flush_tlb_all(struct amd_iommu *iommu) 1184 + static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu) 1125 1185 { 1126 1186 u32 dom_id; 1127 1187 ··· 1135 1195 iommu_completion_wait(iommu); 1136 1196 } 1137 1197 1138 - static void iommu_flush_all(struct amd_iommu *iommu) 1198 + static void amd_iommu_flush_all(struct amd_iommu *iommu) 1139 1199 { 1140 1200 struct iommu_cmd cmd; 1141 1201 ··· 1154 1214 iommu_queue_command(iommu, &cmd); 1155 1215 } 1156 1216 1157 - static void iommu_flush_irt_all(struct amd_iommu *iommu) 1217 + static void amd_iommu_flush_irt_all(struct amd_iommu *iommu) 1158 1218 { 1159 1219 u32 devid; 1160 1220 ··· 1167 1227 void iommu_flush_all_caches(struct amd_iommu *iommu) 1168 1228 { 1169 1229 if (iommu_feature(iommu, FEATURE_IA)) { 1170 - iommu_flush_all(iommu); 1230 + amd_iommu_flush_all(iommu); 1171 1231 } else { 1172 - iommu_flush_dte_all(iommu); 1173 - iommu_flush_irt_all(iommu); 1174 - iommu_flush_tlb_all(iommu); 1232 + amd_iommu_flush_dte_all(iommu); 1233 + amd_iommu_flush_irt_all(iommu); 1234 + amd_iommu_flush_tlb_all(iommu); 1175 1235 } 1176 1236 } 1177 1237 ··· 1479 1539 1480 1540 if (count > 1) { 1481 1541 __pte = PAGE_SIZE_PTE(__sme_set(phys_addr), page_size); 1482 - __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC; 1542 + __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC; 1483 1543 } else 1484 - __pte = __sme_set(phys_addr) | IOMMU_PTE_P | IOMMU_PTE_FC; 1544 + __pte = __sme_set(phys_addr) | IOMMU_PTE_PR | IOMMU_PTE_FC; 1485 1545 1486 1546 if (prot & IOMMU_PROT_IR) 1487 1547 __pte |= IOMMU_PTE_IR; ··· 1730 1790 free_page((unsigned long)domain->gcr3_tbl); 1731 1791 } 1732 1792 1733 - static void dma_ops_domain_free_flush_queue(struct dma_ops_domain *dom) 1734 - { 1735 - int cpu; 1736 - 1737 - for_each_possible_cpu(cpu) { 1738 - struct flush_queue *queue; 1739 - 1740 - queue = per_cpu_ptr(dom->flush_queue, cpu); 1741 - kfree(queue->entries); 1742 - } 1743 - 1744 - free_percpu(dom->flush_queue); 1745 - 1746 - dom->flush_queue = NULL; 1747 - } 1748 - 1749 - static int dma_ops_domain_alloc_flush_queue(struct dma_ops_domain *dom) 1750 - { 1751 - int cpu; 1752 - 1753 - atomic64_set(&dom->flush_start_cnt, 0); 1754 - atomic64_set(&dom->flush_finish_cnt, 0); 1755 - 1756 - dom->flush_queue = alloc_percpu(struct flush_queue); 1757 - if (!dom->flush_queue) 1758 - return -ENOMEM; 1759 - 1760 - /* First make sure everything is cleared */ 1761 - for_each_possible_cpu(cpu) { 1762 - struct flush_queue *queue; 1763 - 1764 - queue = per_cpu_ptr(dom->flush_queue, cpu); 1765 - queue->head = 0; 1766 - queue->tail = 0; 1767 - queue->entries = NULL; 1768 - } 1769 - 1770 - /* Now start doing the allocation */ 1771 - for_each_possible_cpu(cpu) { 1772 - struct flush_queue *queue; 1773 - 1774 - queue = per_cpu_ptr(dom->flush_queue, cpu); 1775 - queue->entries = kzalloc(FLUSH_QUEUE_SIZE * sizeof(*queue->entries), 1776 - GFP_KERNEL); 1777 - if (!queue->entries) { 1778 - dma_ops_domain_free_flush_queue(dom); 1779 - return -ENOMEM; 1780 - } 1781 - 1782 - spin_lock_init(&queue->lock); 1783 - } 1784 - 1785 - return 0; 1786 - } 1787 - 1788 1793 static void dma_ops_domain_flush_tlb(struct dma_ops_domain *dom) 1789 1794 { 1790 - atomic64_inc(&dom->flush_start_cnt); 1791 1795 domain_flush_tlb(&dom->domain); 1792 1796 domain_flush_complete(&dom->domain); 1793 - atomic64_inc(&dom->flush_finish_cnt); 1794 1797 } 1795 1798 1796 - static inline bool queue_ring_full(struct flush_queue *queue) 1799 + static void iova_domain_flush_tlb(struct iova_domain *iovad) 1797 1800 { 1798 - assert_spin_locked(&queue->lock); 1801 + struct dma_ops_domain *dom; 1799 1802 1800 - return (((queue->tail + 1) % FLUSH_QUEUE_SIZE) == queue->head); 1801 - } 1802 - 1803 - #define queue_ring_for_each(i, q) \ 1804 - for (i = (q)->head; i != (q)->tail; i = (i + 1) % FLUSH_QUEUE_SIZE) 1805 - 1806 - static inline unsigned queue_ring_add(struct flush_queue *queue) 1807 - { 1808 - unsigned idx = queue->tail; 1809 - 1810 - assert_spin_locked(&queue->lock); 1811 - queue->tail = (idx + 1) % FLUSH_QUEUE_SIZE; 1812 - 1813 - return idx; 1814 - } 1815 - 1816 - static inline void queue_ring_remove_head(struct flush_queue *queue) 1817 - { 1818 - assert_spin_locked(&queue->lock); 1819 - queue->head = (queue->head + 1) % FLUSH_QUEUE_SIZE; 1820 - } 1821 - 1822 - static void queue_ring_free_flushed(struct dma_ops_domain *dom, 1823 - struct flush_queue *queue) 1824 - { 1825 - u64 counter = atomic64_read(&dom->flush_finish_cnt); 1826 - int idx; 1827 - 1828 - queue_ring_for_each(idx, queue) { 1829 - /* 1830 - * This assumes that counter values in the ring-buffer are 1831 - * monotonously rising. 1832 - */ 1833 - if (queue->entries[idx].counter >= counter) 1834 - break; 1835 - 1836 - free_iova_fast(&dom->iovad, 1837 - queue->entries[idx].iova_pfn, 1838 - queue->entries[idx].pages); 1839 - 1840 - queue_ring_remove_head(queue); 1841 - } 1842 - } 1843 - 1844 - static void queue_add(struct dma_ops_domain *dom, 1845 - unsigned long address, unsigned long pages) 1846 - { 1847 - struct flush_queue *queue; 1848 - unsigned long flags; 1849 - int idx; 1850 - 1851 - pages = __roundup_pow_of_two(pages); 1852 - address >>= PAGE_SHIFT; 1853 - 1854 - queue = get_cpu_ptr(dom->flush_queue); 1855 - spin_lock_irqsave(&queue->lock, flags); 1856 - 1857 - /* 1858 - * First remove the enries from the ring-buffer that are already 1859 - * flushed to make the below queue_ring_full() check less likely 1860 - */ 1861 - queue_ring_free_flushed(dom, queue); 1862 - 1863 - /* 1864 - * When ring-queue is full, flush the entries from the IOTLB so 1865 - * that we can free all entries with queue_ring_free_flushed() 1866 - * below. 1867 - */ 1868 - if (queue_ring_full(queue)) { 1869 - dma_ops_domain_flush_tlb(dom); 1870 - queue_ring_free_flushed(dom, queue); 1871 - } 1872 - 1873 - idx = queue_ring_add(queue); 1874 - 1875 - queue->entries[idx].iova_pfn = address; 1876 - queue->entries[idx].pages = pages; 1877 - queue->entries[idx].counter = atomic64_read(&dom->flush_start_cnt); 1878 - 1879 - spin_unlock_irqrestore(&queue->lock, flags); 1880 - 1881 - if (atomic_cmpxchg(&dom->flush_timer_on, 0, 1) == 0) 1882 - mod_timer(&dom->flush_timer, jiffies + msecs_to_jiffies(10)); 1883 - 1884 - put_cpu_ptr(dom->flush_queue); 1885 - } 1886 - 1887 - static void queue_flush_timeout(unsigned long data) 1888 - { 1889 - struct dma_ops_domain *dom = (struct dma_ops_domain *)data; 1890 - int cpu; 1891 - 1892 - atomic_set(&dom->flush_timer_on, 0); 1803 + dom = container_of(iovad, struct dma_ops_domain, iovad); 1893 1804 1894 1805 dma_ops_domain_flush_tlb(dom); 1895 - 1896 - for_each_possible_cpu(cpu) { 1897 - struct flush_queue *queue; 1898 - unsigned long flags; 1899 - 1900 - queue = per_cpu_ptr(dom->flush_queue, cpu); 1901 - spin_lock_irqsave(&queue->lock, flags); 1902 - queue_ring_free_flushed(dom, queue); 1903 - spin_unlock_irqrestore(&queue->lock, flags); 1904 - } 1905 1806 } 1906 1807 1907 1808 /* ··· 1755 1974 return; 1756 1975 1757 1976 del_domain_from_list(&dom->domain); 1758 - 1759 - if (timer_pending(&dom->flush_timer)) 1760 - del_timer(&dom->flush_timer); 1761 - 1762 - dma_ops_domain_free_flush_queue(dom); 1763 1977 1764 1978 put_iova_domain(&dom->iovad); 1765 1979 ··· 1791 2015 init_iova_domain(&dma_dom->iovad, PAGE_SIZE, 1792 2016 IOVA_START_PFN, DMA_32BIT_PFN); 1793 2017 1794 - /* Initialize reserved ranges */ 1795 - copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad); 1796 - 1797 - if (dma_ops_domain_alloc_flush_queue(dma_dom)) 2018 + if (init_iova_flush_queue(&dma_dom->iovad, iova_domain_flush_tlb, NULL)) 1798 2019 goto free_dma_dom; 1799 2020 1800 - setup_timer(&dma_dom->flush_timer, queue_flush_timeout, 1801 - (unsigned long)dma_dom); 1802 - 1803 - atomic_set(&dma_dom->flush_timer_on, 0); 2021 + /* Initialize reserved ranges */ 2022 + copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad); 1804 2023 1805 2024 add_domain_to_list(&dma_dom->domain); 1806 2025 ··· 1826 2055 1827 2056 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK) 1828 2057 << DEV_ENTRY_MODE_SHIFT; 1829 - pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV; 2058 + pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV; 1830 2059 1831 2060 flags = amd_iommu_dev_table[devid].data[1]; 1832 2061 ··· 1859 2088 flags |= tmp; 1860 2089 } 1861 2090 1862 - 1863 - flags &= ~(DTE_FLAG_SA | 0xffffULL); 2091 + flags &= ~DEV_DOMID_MASK; 1864 2092 flags |= domain->id; 1865 2093 1866 2094 amd_iommu_dev_table[devid].data[1] = flags; ··· 1869 2099 static void clear_dte_entry(u16 devid) 1870 2100 { 1871 2101 /* remove entry from the device table seen by the hardware */ 1872 - amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV; 2102 + amd_iommu_dev_table[devid].data[0] = DTE_FLAG_V | DTE_FLAG_TV; 1873 2103 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK; 1874 2104 1875 2105 amd_iommu_apply_erratum_63(devid); ··· 2250 2480 static struct protection_domain *get_domain(struct device *dev) 2251 2481 { 2252 2482 struct protection_domain *domain; 2483 + struct iommu_domain *io_domain; 2253 2484 2254 2485 if (!check_device(dev)) 2255 2486 return ERR_PTR(-EINVAL); 2256 2487 2257 2488 domain = get_dev_data(dev)->domain; 2489 + if (domain == NULL && get_dev_data(dev)->defer_attach) { 2490 + get_dev_data(dev)->defer_attach = false; 2491 + io_domain = iommu_get_domain_for_dev(dev); 2492 + domain = to_pdomain(io_domain); 2493 + attach_device(dev, domain); 2494 + } 2495 + if (domain == NULL) 2496 + return ERR_PTR(-EBUSY); 2497 + 2258 2498 if (!dma_ops_domain(domain)) 2259 2499 return ERR_PTR(-EBUSY); 2260 2500 ··· 2310 2530 else 2311 2531 return 0; 2312 2532 } 2533 + 2313 2534 /* 2314 2535 * This function contains common code for mapping of a physically 2315 2536 * contiguous memory region into DMA address space. It is used by all ··· 2402 2621 domain_flush_tlb(&dma_dom->domain); 2403 2622 domain_flush_complete(&dma_dom->domain); 2404 2623 } else { 2405 - queue_add(dma_dom, dma_addr, pages); 2624 + pages = __roundup_pow_of_two(pages); 2625 + queue_iova(&dma_dom->iovad, dma_addr >> PAGE_SHIFT, pages, 0); 2406 2626 } 2407 2627 } 2408 2628 ··· 3157 3375 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL); 3158 3376 } 3159 3377 3378 + static bool amd_iommu_is_attach_deferred(struct iommu_domain *domain, 3379 + struct device *dev) 3380 + { 3381 + struct iommu_dev_data *dev_data = dev->archdata.iommu; 3382 + return dev_data->defer_attach; 3383 + } 3384 + 3160 3385 const struct iommu_ops amd_iommu_ops = { 3161 3386 .capable = amd_iommu_capable, 3162 3387 .domain_alloc = amd_iommu_domain_alloc, ··· 3180 3391 .get_resv_regions = amd_iommu_get_resv_regions, 3181 3392 .put_resv_regions = amd_iommu_put_resv_regions, 3182 3393 .apply_resv_region = amd_iommu_apply_resv_region, 3394 + .is_attach_deferred = amd_iommu_is_attach_deferred, 3183 3395 .pgsize_bitmap = AMD_IOMMU_PGSIZES, 3184 3396 }; 3185 3397 ··· 3568 3778 *****************************************************************************/ 3569 3779 3570 3780 static struct irq_chip amd_ir_chip; 3571 - 3572 - #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6) 3573 - #define DTE_IRQ_REMAP_INTCTL (2ULL << 60) 3574 - #define DTE_IRQ_TABLE_LEN (8ULL << 1) 3575 - #define DTE_IRQ_REMAP_ENABLE 1ULL 3576 3781 3577 3782 static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table) 3578 3783 {
+199 -24
drivers/iommu/amd_iommu_init.c
··· 29 29 #include <linux/export.h> 30 30 #include <linux/iommu.h> 31 31 #include <linux/kmemleak.h> 32 - #include <linux/crash_dump.h> 33 32 #include <linux/mem_encrypt.h> 34 33 #include <asm/pci-direct.h> 35 34 #include <asm/iommu.h> ··· 38 39 #include <asm/io_apic.h> 39 40 #include <asm/irq_remapping.h> 40 41 42 + #include <linux/crash_dump.h> 41 43 #include "amd_iommu_proto.h" 42 44 #include "amd_iommu_types.h" 43 45 #include "irq_remapping.h" ··· 197 197 * page table root pointer. 198 198 */ 199 199 struct dev_table_entry *amd_iommu_dev_table; 200 + /* 201 + * Pointer to a device table which the content of old device table 202 + * will be copied to. It's only be used in kdump kernel. 203 + */ 204 + static struct dev_table_entry *old_dev_tbl_cpy; 200 205 201 206 /* 202 207 * The alias table is a driver specific data structure which contains the ··· 215 210 * for a specific device. It is also indexed by the PCI device id. 216 211 */ 217 212 struct amd_iommu **amd_iommu_rlookup_table; 213 + EXPORT_SYMBOL(amd_iommu_rlookup_table); 218 214 219 215 /* 220 216 * This table is used to find the irq remapping table for a given device id ··· 264 258 static int amd_iommu_enable_interrupts(void); 265 259 static int __init iommu_go_to_state(enum iommu_init_state state); 266 260 static void init_device_table_dma(void); 261 + 262 + static bool amd_iommu_pre_enabled = true; 263 + 264 + bool translation_pre_enabled(struct amd_iommu *iommu) 265 + { 266 + return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED); 267 + } 268 + EXPORT_SYMBOL(translation_pre_enabled); 269 + 270 + static void clear_translation_pre_enabled(struct amd_iommu *iommu) 271 + { 272 + iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED; 273 + } 274 + 275 + static void init_translation_status(struct amd_iommu *iommu) 276 + { 277 + u32 ctrl; 278 + 279 + ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET); 280 + if (ctrl & (1<<CONTROL_IOMMU_EN)) 281 + iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED; 282 + } 267 283 268 284 static inline void update_last_devid(u16 devid) 269 285 { ··· 644 616 amd_iommu_reset_cmd_buffer(iommu); 645 617 } 646 618 619 + /* 620 + * This function disables the command buffer 621 + */ 622 + static void iommu_disable_command_buffer(struct amd_iommu *iommu) 623 + { 624 + iommu_feature_disable(iommu, CONTROL_CMDBUF_EN); 625 + } 626 + 647 627 static void __init free_command_buffer(struct amd_iommu *iommu) 648 628 { 649 629 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE)); ··· 682 646 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); 683 647 684 648 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN); 649 + } 650 + 651 + /* 652 + * This function disables the event log buffer 653 + */ 654 + static void iommu_disable_event_buffer(struct amd_iommu *iommu) 655 + { 656 + iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN); 685 657 } 686 658 687 659 static void __init free_event_buffer(struct amd_iommu *iommu) ··· 852 808 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit; 853 809 } 854 810 811 + 812 + static bool copy_device_table(void) 813 + { 814 + u64 int_ctl, int_tab_len, entry = 0, last_entry = 0; 815 + struct dev_table_entry *old_devtb = NULL; 816 + u32 lo, hi, devid, old_devtb_size; 817 + phys_addr_t old_devtb_phys; 818 + struct amd_iommu *iommu; 819 + u16 dom_id, dte_v, irq_v; 820 + gfp_t gfp_flag; 821 + u64 tmp; 822 + 823 + if (!amd_iommu_pre_enabled) 824 + return false; 825 + 826 + pr_warn("Translation is already enabled - trying to copy translation structures\n"); 827 + for_each_iommu(iommu) { 828 + /* All IOMMUs should use the same device table with the same size */ 829 + lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET); 830 + hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4); 831 + entry = (((u64) hi) << 32) + lo; 832 + if (last_entry && last_entry != entry) { 833 + pr_err("IOMMU:%d should use the same dev table as others!/n", 834 + iommu->index); 835 + return false; 836 + } 837 + last_entry = entry; 838 + 839 + old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12; 840 + if (old_devtb_size != dev_table_size) { 841 + pr_err("The device table size of IOMMU:%d is not expected!/n", 842 + iommu->index); 843 + return false; 844 + } 845 + } 846 + 847 + old_devtb_phys = entry & PAGE_MASK; 848 + if (old_devtb_phys >= 0x100000000ULL) { 849 + pr_err("The address of old device table is above 4G, not trustworthy!/n"); 850 + return false; 851 + } 852 + old_devtb = memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB); 853 + if (!old_devtb) 854 + return false; 855 + 856 + gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32; 857 + old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag, 858 + get_order(dev_table_size)); 859 + if (old_dev_tbl_cpy == NULL) { 860 + pr_err("Failed to allocate memory for copying old device table!/n"); 861 + return false; 862 + } 863 + 864 + for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) { 865 + old_dev_tbl_cpy[devid] = old_devtb[devid]; 866 + dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK; 867 + dte_v = old_devtb[devid].data[0] & DTE_FLAG_V; 868 + 869 + if (dte_v && dom_id) { 870 + old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0]; 871 + old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1]; 872 + __set_bit(dom_id, amd_iommu_pd_alloc_bitmap); 873 + /* If gcr3 table existed, mask it out */ 874 + if (old_devtb[devid].data[0] & DTE_FLAG_GV) { 875 + tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B; 876 + tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C; 877 + old_dev_tbl_cpy[devid].data[1] &= ~tmp; 878 + tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A; 879 + tmp |= DTE_FLAG_GV; 880 + old_dev_tbl_cpy[devid].data[0] &= ~tmp; 881 + } 882 + } 883 + 884 + irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE; 885 + int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK; 886 + int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK; 887 + if (irq_v && (int_ctl || int_tab_len)) { 888 + if ((int_ctl != DTE_IRQ_REMAP_INTCTL) || 889 + (int_tab_len != DTE_IRQ_TABLE_LEN)) { 890 + pr_err("Wrong old irq remapping flag: %#x\n", devid); 891 + return false; 892 + } 893 + 894 + old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2]; 895 + } 896 + } 897 + memunmap(old_devtb); 898 + 899 + return true; 900 + } 855 901 856 902 void amd_iommu_apply_erratum_63(u16 devid) 857 903 { ··· 1534 1400 1535 1401 iommu->int_enabled = false; 1536 1402 1403 + init_translation_status(iommu); 1404 + if (translation_pre_enabled(iommu) && !is_kdump_kernel()) { 1405 + iommu_disable(iommu); 1406 + clear_translation_pre_enabled(iommu); 1407 + pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n", 1408 + iommu->index); 1409 + } 1410 + if (amd_iommu_pre_enabled) 1411 + amd_iommu_pre_enabled = translation_pre_enabled(iommu); 1412 + 1537 1413 ret = init_iommu_from_acpi(iommu, h); 1538 1414 if (ret) 1539 1415 return ret; ··· 2037 1893 } 2038 1894 2039 1895 /* 2040 - * Init the device table to not allow DMA access for devices and 2041 - * suppress all page faults 1896 + * Init the device table to not allow DMA access for devices 2042 1897 */ 2043 1898 static void init_device_table_dma(void) 2044 1899 { ··· 2046 1903 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) { 2047 1904 set_dev_entry_bit(devid, DEV_ENTRY_VALID); 2048 1905 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION); 2049 - /* 2050 - * In kdump kernels in-flight DMA from the old kernel might 2051 - * cause IO_PAGE_FAULTs. There are no reports that a kdump 2052 - * actually failed because of that, so just disable fault 2053 - * reporting in the hardware to get rid of the messages 2054 - */ 2055 - if (is_kdump_kernel()) 2056 - set_dev_entry_bit(devid, DEV_ENTRY_NO_PAGE_FAULT); 2057 1906 } 2058 1907 } 2059 1908 ··· 2158 2023 #endif 2159 2024 } 2160 2025 2026 + static void early_enable_iommu(struct amd_iommu *iommu) 2027 + { 2028 + iommu_disable(iommu); 2029 + iommu_init_flags(iommu); 2030 + iommu_set_device_table(iommu); 2031 + iommu_enable_command_buffer(iommu); 2032 + iommu_enable_event_buffer(iommu); 2033 + iommu_set_exclusion_range(iommu); 2034 + iommu_enable_ga(iommu); 2035 + iommu_enable(iommu); 2036 + iommu_flush_all_caches(iommu); 2037 + } 2038 + 2161 2039 /* 2162 2040 * This function finally enables all IOMMUs found in the system after 2163 - * they have been initialized 2041 + * they have been initialized. 2042 + * 2043 + * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy 2044 + * the old content of device table entries. Not this case or copy failed, 2045 + * just continue as normal kernel does. 2164 2046 */ 2165 2047 static void early_enable_iommus(void) 2166 2048 { 2167 2049 struct amd_iommu *iommu; 2168 2050 2169 - for_each_iommu(iommu) { 2170 - iommu_disable(iommu); 2171 - iommu_init_flags(iommu); 2172 - iommu_set_device_table(iommu); 2173 - iommu_enable_command_buffer(iommu); 2174 - iommu_enable_event_buffer(iommu); 2175 - iommu_set_exclusion_range(iommu); 2176 - iommu_enable_ga(iommu); 2177 - iommu_enable(iommu); 2178 - iommu_flush_all_caches(iommu); 2051 + 2052 + if (!copy_device_table()) { 2053 + /* 2054 + * If come here because of failure in copying device table from old 2055 + * kernel with all IOMMUs enabled, print error message and try to 2056 + * free allocated old_dev_tbl_cpy. 2057 + */ 2058 + if (amd_iommu_pre_enabled) 2059 + pr_err("Failed to copy DEV table from previous kernel.\n"); 2060 + if (old_dev_tbl_cpy != NULL) 2061 + free_pages((unsigned long)old_dev_tbl_cpy, 2062 + get_order(dev_table_size)); 2063 + 2064 + for_each_iommu(iommu) { 2065 + clear_translation_pre_enabled(iommu); 2066 + early_enable_iommu(iommu); 2067 + } 2068 + } else { 2069 + pr_info("Copied DEV table from previous kernel.\n"); 2070 + free_pages((unsigned long)amd_iommu_dev_table, 2071 + get_order(dev_table_size)); 2072 + amd_iommu_dev_table = old_dev_tbl_cpy; 2073 + for_each_iommu(iommu) { 2074 + iommu_disable_command_buffer(iommu); 2075 + iommu_disable_event_buffer(iommu); 2076 + iommu_enable_command_buffer(iommu); 2077 + iommu_enable_event_buffer(iommu); 2078 + iommu_enable_ga(iommu); 2079 + iommu_set_device_table(iommu); 2080 + iommu_flush_all_caches(iommu); 2081 + } 2179 2082 } 2180 2083 2181 2084 #ifdef CONFIG_IRQ_REMAP ··· 2449 2276 2450 2277 /* Device table - directly used by all IOMMUs */ 2451 2278 ret = -ENOMEM; 2452 - amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2279 + amd_iommu_dev_table = (void *)__get_free_pages( 2280 + GFP_KERNEL | __GFP_ZERO | GFP_DMA32, 2453 2281 get_order(dev_table_size)); 2454 2282 if (amd_iommu_dev_table == NULL) 2455 2283 goto out; ··· 2500 2326 goto out; 2501 2327 2502 2328 /* Disable any previously enabled IOMMUs */ 2503 - disable_iommus(); 2329 + if (!is_kdump_kernel() || amd_iommu_disabled) 2330 + disable_iommus(); 2504 2331 2505 2332 if (amd_iommu_irq_remap) 2506 2333 amd_iommu_irq_remap = check_ioapic_information();
+2
drivers/iommu/amd_iommu_proto.h
··· 97 97 return phys_to_virt(__sme_clr(paddr)); 98 98 } 99 99 100 + extern bool translation_pre_enabled(struct amd_iommu *iommu); 101 + extern struct iommu_dev_data *get_dev_data(struct device *dev); 100 102 #endif /* _ASM_X86_AMD_IOMMU_PROTO_H */
+50 -5
drivers/iommu/amd_iommu_types.h
··· 250 250 251 251 #define GA_GUEST_NR 0x1 252 252 253 + /* Bit value definition for dte irq remapping fields*/ 254 + #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6) 255 + #define DTE_IRQ_REMAP_INTCTL_MASK (0x3ULL << 60) 256 + #define DTE_IRQ_TABLE_LEN_MASK (0xfULL << 1) 257 + #define DTE_IRQ_REMAP_INTCTL (2ULL << 60) 258 + #define DTE_IRQ_TABLE_LEN (8ULL << 1) 259 + #define DTE_IRQ_REMAP_ENABLE 1ULL 260 + 253 261 #define PAGE_MODE_NONE 0x00 254 262 #define PAGE_MODE_1_LEVEL 0x01 255 263 #define PAGE_MODE_2_LEVEL 0x02 ··· 273 265 #define PM_LEVEL_INDEX(x, a) (((a) >> PM_LEVEL_SHIFT((x))) & 0x1ffULL) 274 266 #define PM_LEVEL_ENC(x) (((x) << 9) & 0xe00ULL) 275 267 #define PM_LEVEL_PDE(x, a) ((a) | PM_LEVEL_ENC((x)) | \ 276 - IOMMU_PTE_P | IOMMU_PTE_IR | IOMMU_PTE_IW) 268 + IOMMU_PTE_PR | IOMMU_PTE_IR | IOMMU_PTE_IW) 277 269 #define PM_PTE_LEVEL(pte) (((pte) >> 9) & 0x7ULL) 278 270 279 271 #define PM_MAP_4k 0 ··· 322 314 #define PTE_LEVEL_PAGE_SIZE(level) \ 323 315 (1ULL << (12 + (9 * (level)))) 324 316 325 - #define IOMMU_PTE_P (1ULL << 0) 326 - #define IOMMU_PTE_TV (1ULL << 1) 317 + /* 318 + * Bit value definition for I/O PTE fields 319 + */ 320 + #define IOMMU_PTE_PR (1ULL << 0) 327 321 #define IOMMU_PTE_U (1ULL << 59) 328 322 #define IOMMU_PTE_FC (1ULL << 60) 329 323 #define IOMMU_PTE_IR (1ULL << 61) 330 324 #define IOMMU_PTE_IW (1ULL << 62) 331 325 326 + /* 327 + * Bit value definition for DTE fields 328 + */ 329 + #define DTE_FLAG_V (1ULL << 0) 330 + #define DTE_FLAG_TV (1ULL << 1) 331 + #define DTE_FLAG_IR (1ULL << 61) 332 + #define DTE_FLAG_IW (1ULL << 62) 333 + 332 334 #define DTE_FLAG_IOTLB (1ULL << 32) 333 - #define DTE_FLAG_SA (1ULL << 34) 334 335 #define DTE_FLAG_GV (1ULL << 55) 335 336 #define DTE_FLAG_MASK (0x3ffULL << 32) 336 337 #define DTE_GLX_SHIFT (56) 337 338 #define DTE_GLX_MASK (3) 339 + #define DEV_DOMID_MASK 0xffffULL 338 340 339 341 #define DTE_GCR3_VAL_A(x) (((x) >> 12) & 0x00007ULL) 340 342 #define DTE_GCR3_VAL_B(x) (((x) >> 15) & 0x0ffffULL) ··· 361 343 #define GCR3_VALID 0x01ULL 362 344 363 345 #define IOMMU_PAGE_MASK (((1ULL << 52) - 1) & ~0xfffULL) 364 - #define IOMMU_PTE_PRESENT(pte) ((pte) & IOMMU_PTE_P) 346 + #define IOMMU_PTE_PRESENT(pte) ((pte) & IOMMU_PTE_PR) 365 347 #define IOMMU_PTE_PAGE(pte) (iommu_phys_to_virt((pte) & IOMMU_PAGE_MASK)) 366 348 #define IOMMU_PTE_MODE(pte) (((pte) >> 9) & 0x07) 367 349 ··· 452 434 struct iommu_domain; 453 435 struct irq_domain; 454 436 struct amd_irte_ops; 437 + 438 + #define AMD_IOMMU_FLAG_TRANS_PRE_ENABLED (1 << 0) 455 439 456 440 /* 457 441 * This structure contains generic data for IOMMU protection domains ··· 589 569 struct amd_irte_ops *irte_ops; 590 570 #endif 591 571 572 + u32 flags; 592 573 volatile u64 __aligned(8) cmd_sem; 593 574 }; 594 575 ··· 618 597 u8 id; 619 598 u16 devid; 620 599 bool cmd_line; 600 + }; 601 + 602 + /* 603 + * This struct contains device specific data for the IOMMU 604 + */ 605 + struct iommu_dev_data { 606 + struct list_head list; /* For domain->dev_list */ 607 + struct list_head dev_data_list; /* For global dev_data_list */ 608 + struct protection_domain *domain; /* Domain the device is bound to */ 609 + u16 devid; /* PCI Device ID */ 610 + u16 alias; /* Alias Device ID */ 611 + bool iommu_v2; /* Device can make use of IOMMUv2 */ 612 + bool passthrough; /* Device is identity mapped */ 613 + struct { 614 + bool enabled; 615 + int qdep; 616 + } ats; /* ATS state */ 617 + bool pri_tlp; /* PASID TLB required for 618 + PPR completions */ 619 + u32 errata; /* Bitmap for errata to apply */ 620 + bool use_vapic; /* Enable device to use vapic mode */ 621 + bool defer_attach; 622 + 623 + struct ratelimit_state rs; /* Ratelimit IOPF messages */ 621 624 }; 622 625 623 626 /* Map HPET and IOAPIC ids to the devid used by the IOMMU */
+17 -1
drivers/iommu/amd_iommu_v2.c
··· 554 554 unsigned long flags; 555 555 struct fault *fault; 556 556 bool finish; 557 - u16 tag; 557 + u16 tag, devid; 558 558 int ret; 559 + struct iommu_dev_data *dev_data; 560 + struct pci_dev *pdev = NULL; 559 561 560 562 iommu_fault = data; 561 563 tag = iommu_fault->tag & 0x1ff; 562 564 finish = (iommu_fault->tag >> 9) & 1; 563 565 566 + devid = iommu_fault->device_id; 567 + pdev = pci_get_bus_and_slot(PCI_BUS_NUM(devid), devid & 0xff); 568 + if (!pdev) 569 + return -ENODEV; 570 + dev_data = get_dev_data(&pdev->dev); 571 + 572 + /* In kdump kernel pci dev is not initialized yet -> send INVALID */ 564 573 ret = NOTIFY_DONE; 574 + if (translation_pre_enabled(amd_iommu_rlookup_table[devid]) 575 + && dev_data->defer_attach) { 576 + amd_iommu_complete_ppr(pdev, iommu_fault->pasid, 577 + PPR_INVALID, tag); 578 + goto out; 579 + } 580 + 565 581 dev_state = get_device_state(iommu_fault->device_id); 566 582 if (dev_state == NULL) 567 583 goto out;
+220
drivers/iommu/arm-smmu-regs.h
··· 1 + /* 2 + * IOMMU API for ARM architected SMMU implementations. 3 + * 4 + * This program is free software; you can redistribute it and/or modify 5 + * it under the terms of the GNU General Public License version 2 as 6 + * published by the Free Software Foundation. 7 + * 8 + * This program is distributed in the hope that it will be useful, 9 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 + * GNU General Public License for more details. 12 + * 13 + * You should have received a copy of the GNU General Public License 14 + * along with this program; if not, write to the Free Software 15 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16 + * 17 + * Copyright (C) 2013 ARM Limited 18 + * 19 + * Author: Will Deacon <will.deacon@arm.com> 20 + */ 21 + 22 + #ifndef _ARM_SMMU_REGS_H 23 + #define _ARM_SMMU_REGS_H 24 + 25 + /* Configuration registers */ 26 + #define ARM_SMMU_GR0_sCR0 0x0 27 + #define sCR0_CLIENTPD (1 << 0) 28 + #define sCR0_GFRE (1 << 1) 29 + #define sCR0_GFIE (1 << 2) 30 + #define sCR0_EXIDENABLE (1 << 3) 31 + #define sCR0_GCFGFRE (1 << 4) 32 + #define sCR0_GCFGFIE (1 << 5) 33 + #define sCR0_USFCFG (1 << 10) 34 + #define sCR0_VMIDPNE (1 << 11) 35 + #define sCR0_PTM (1 << 12) 36 + #define sCR0_FB (1 << 13) 37 + #define sCR0_VMID16EN (1 << 31) 38 + #define sCR0_BSU_SHIFT 14 39 + #define sCR0_BSU_MASK 0x3 40 + 41 + /* Auxiliary Configuration register */ 42 + #define ARM_SMMU_GR0_sACR 0x10 43 + 44 + /* Identification registers */ 45 + #define ARM_SMMU_GR0_ID0 0x20 46 + #define ARM_SMMU_GR0_ID1 0x24 47 + #define ARM_SMMU_GR0_ID2 0x28 48 + #define ARM_SMMU_GR0_ID3 0x2c 49 + #define ARM_SMMU_GR0_ID4 0x30 50 + #define ARM_SMMU_GR0_ID5 0x34 51 + #define ARM_SMMU_GR0_ID6 0x38 52 + #define ARM_SMMU_GR0_ID7 0x3c 53 + #define ARM_SMMU_GR0_sGFSR 0x48 54 + #define ARM_SMMU_GR0_sGFSYNR0 0x50 55 + #define ARM_SMMU_GR0_sGFSYNR1 0x54 56 + #define ARM_SMMU_GR0_sGFSYNR2 0x58 57 + 58 + #define ID0_S1TS (1 << 30) 59 + #define ID0_S2TS (1 << 29) 60 + #define ID0_NTS (1 << 28) 61 + #define ID0_SMS (1 << 27) 62 + #define ID0_ATOSNS (1 << 26) 63 + #define ID0_PTFS_NO_AARCH32 (1 << 25) 64 + #define ID0_PTFS_NO_AARCH32S (1 << 24) 65 + #define ID0_CTTW (1 << 14) 66 + #define ID0_NUMIRPT_SHIFT 16 67 + #define ID0_NUMIRPT_MASK 0xff 68 + #define ID0_NUMSIDB_SHIFT 9 69 + #define ID0_NUMSIDB_MASK 0xf 70 + #define ID0_EXIDS (1 << 8) 71 + #define ID0_NUMSMRG_SHIFT 0 72 + #define ID0_NUMSMRG_MASK 0xff 73 + 74 + #define ID1_PAGESIZE (1 << 31) 75 + #define ID1_NUMPAGENDXB_SHIFT 28 76 + #define ID1_NUMPAGENDXB_MASK 7 77 + #define ID1_NUMS2CB_SHIFT 16 78 + #define ID1_NUMS2CB_MASK 0xff 79 + #define ID1_NUMCB_SHIFT 0 80 + #define ID1_NUMCB_MASK 0xff 81 + 82 + #define ID2_OAS_SHIFT 4 83 + #define ID2_OAS_MASK 0xf 84 + #define ID2_IAS_SHIFT 0 85 + #define ID2_IAS_MASK 0xf 86 + #define ID2_UBS_SHIFT 8 87 + #define ID2_UBS_MASK 0xf 88 + #define ID2_PTFS_4K (1 << 12) 89 + #define ID2_PTFS_16K (1 << 13) 90 + #define ID2_PTFS_64K (1 << 14) 91 + #define ID2_VMID16 (1 << 15) 92 + 93 + #define ID7_MAJOR_SHIFT 4 94 + #define ID7_MAJOR_MASK 0xf 95 + 96 + /* Global TLB invalidation */ 97 + #define ARM_SMMU_GR0_TLBIVMID 0x64 98 + #define ARM_SMMU_GR0_TLBIALLNSNH 0x68 99 + #define ARM_SMMU_GR0_TLBIALLH 0x6c 100 + #define ARM_SMMU_GR0_sTLBGSYNC 0x70 101 + #define ARM_SMMU_GR0_sTLBGSTATUS 0x74 102 + #define sTLBGSTATUS_GSACTIVE (1 << 0) 103 + 104 + /* Stream mapping registers */ 105 + #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2)) 106 + #define SMR_VALID (1 << 31) 107 + #define SMR_MASK_SHIFT 16 108 + #define SMR_ID_SHIFT 0 109 + 110 + #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2)) 111 + #define S2CR_CBNDX_SHIFT 0 112 + #define S2CR_CBNDX_MASK 0xff 113 + #define S2CR_EXIDVALID (1 << 10) 114 + #define S2CR_TYPE_SHIFT 16 115 + #define S2CR_TYPE_MASK 0x3 116 + enum arm_smmu_s2cr_type { 117 + S2CR_TYPE_TRANS, 118 + S2CR_TYPE_BYPASS, 119 + S2CR_TYPE_FAULT, 120 + }; 121 + 122 + #define S2CR_PRIVCFG_SHIFT 24 123 + #define S2CR_PRIVCFG_MASK 0x3 124 + enum arm_smmu_s2cr_privcfg { 125 + S2CR_PRIVCFG_DEFAULT, 126 + S2CR_PRIVCFG_DIPAN, 127 + S2CR_PRIVCFG_UNPRIV, 128 + S2CR_PRIVCFG_PRIV, 129 + }; 130 + 131 + /* Context bank attribute registers */ 132 + #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2)) 133 + #define CBAR_VMID_SHIFT 0 134 + #define CBAR_VMID_MASK 0xff 135 + #define CBAR_S1_BPSHCFG_SHIFT 8 136 + #define CBAR_S1_BPSHCFG_MASK 3 137 + #define CBAR_S1_BPSHCFG_NSH 3 138 + #define CBAR_S1_MEMATTR_SHIFT 12 139 + #define CBAR_S1_MEMATTR_MASK 0xf 140 + #define CBAR_S1_MEMATTR_WB 0xf 141 + #define CBAR_TYPE_SHIFT 16 142 + #define CBAR_TYPE_MASK 0x3 143 + #define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT) 144 + #define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT) 145 + #define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT) 146 + #define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT) 147 + #define CBAR_IRPTNDX_SHIFT 24 148 + #define CBAR_IRPTNDX_MASK 0xff 149 + 150 + #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2)) 151 + #define CBA2R_RW64_32BIT (0 << 0) 152 + #define CBA2R_RW64_64BIT (1 << 0) 153 + #define CBA2R_VMID_SHIFT 16 154 + #define CBA2R_VMID_MASK 0xffff 155 + 156 + #define ARM_SMMU_CB_SCTLR 0x0 157 + #define ARM_SMMU_CB_ACTLR 0x4 158 + #define ARM_SMMU_CB_RESUME 0x8 159 + #define ARM_SMMU_CB_TTBCR2 0x10 160 + #define ARM_SMMU_CB_TTBR0 0x20 161 + #define ARM_SMMU_CB_TTBR1 0x28 162 + #define ARM_SMMU_CB_TTBCR 0x30 163 + #define ARM_SMMU_CB_CONTEXTIDR 0x34 164 + #define ARM_SMMU_CB_S1_MAIR0 0x38 165 + #define ARM_SMMU_CB_S1_MAIR1 0x3c 166 + #define ARM_SMMU_CB_PAR 0x50 167 + #define ARM_SMMU_CB_FSR 0x58 168 + #define ARM_SMMU_CB_FAR 0x60 169 + #define ARM_SMMU_CB_FSYNR0 0x68 170 + #define ARM_SMMU_CB_S1_TLBIVA 0x600 171 + #define ARM_SMMU_CB_S1_TLBIASID 0x610 172 + #define ARM_SMMU_CB_S1_TLBIVAL 0x620 173 + #define ARM_SMMU_CB_S2_TLBIIPAS2 0x630 174 + #define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638 175 + #define ARM_SMMU_CB_TLBSYNC 0x7f0 176 + #define ARM_SMMU_CB_TLBSTATUS 0x7f4 177 + #define ARM_SMMU_CB_ATS1PR 0x800 178 + #define ARM_SMMU_CB_ATSR 0x8f0 179 + 180 + #define SCTLR_S1_ASIDPNE (1 << 12) 181 + #define SCTLR_CFCFG (1 << 7) 182 + #define SCTLR_CFIE (1 << 6) 183 + #define SCTLR_CFRE (1 << 5) 184 + #define SCTLR_E (1 << 4) 185 + #define SCTLR_AFE (1 << 2) 186 + #define SCTLR_TRE (1 << 1) 187 + #define SCTLR_M (1 << 0) 188 + 189 + #define CB_PAR_F (1 << 0) 190 + 191 + #define ATSR_ACTIVE (1 << 0) 192 + 193 + #define RESUME_RETRY (0 << 0) 194 + #define RESUME_TERMINATE (1 << 0) 195 + 196 + #define TTBCR2_SEP_SHIFT 15 197 + #define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT) 198 + #define TTBCR2_AS (1 << 4) 199 + 200 + #define TTBRn_ASID_SHIFT 48 201 + 202 + #define FSR_MULTI (1 << 31) 203 + #define FSR_SS (1 << 30) 204 + #define FSR_UUT (1 << 8) 205 + #define FSR_ASF (1 << 7) 206 + #define FSR_TLBLKF (1 << 6) 207 + #define FSR_TLBMCF (1 << 5) 208 + #define FSR_EF (1 << 4) 209 + #define FSR_PF (1 << 3) 210 + #define FSR_AFF (1 << 2) 211 + #define FSR_TF (1 << 1) 212 + 213 + #define FSR_IGN (FSR_AFF | FSR_ASF | \ 214 + FSR_TLBMCF | FSR_TLBLKF) 215 + #define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \ 216 + FSR_EF | FSR_PF | FSR_TF | FSR_IGN) 217 + 218 + #define FSYNR0_WNR (1 << 4) 219 + 220 + #endif /* _ARM_SMMU_REGS_H */
+7
drivers/iommu/arm-smmu-v3.c
··· 2852 2852 struct arm_smmu_device *smmu = platform_get_drvdata(pdev); 2853 2853 2854 2854 arm_smmu_device_disable(smmu); 2855 + 2855 2856 return 0; 2857 + } 2858 + 2859 + static void arm_smmu_device_shutdown(struct platform_device *pdev) 2860 + { 2861 + arm_smmu_device_remove(pdev); 2856 2862 } 2857 2863 2858 2864 static const struct of_device_id arm_smmu_of_match[] = { ··· 2874 2868 }, 2875 2869 .probe = arm_smmu_device_probe, 2876 2870 .remove = arm_smmu_device_remove, 2871 + .shutdown = arm_smmu_device_shutdown, 2877 2872 }; 2878 2873 module_platform_driver(arm_smmu_driver); 2879 2874
+123 -261
drivers/iommu/arm-smmu.c
··· 54 54 #include <linux/amba/bus.h> 55 55 56 56 #include "io-pgtable.h" 57 + #include "arm-smmu-regs.h" 58 + 59 + #define ARM_MMU500_ACTLR_CPRE (1 << 1) 60 + 61 + #define ARM_MMU500_ACR_CACHE_LOCK (1 << 26) 62 + #define ARM_MMU500_ACR_SMTNMB_TLBEN (1 << 8) 63 + 64 + #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */ 65 + #define TLB_SPIN_COUNT 10 57 66 58 67 /* Maximum number of context banks per SMMU */ 59 68 #define ARM_SMMU_MAX_CBS 128 ··· 92 83 #define smmu_write_atomic_lq writel_relaxed 93 84 #endif 94 85 95 - /* Configuration registers */ 96 - #define ARM_SMMU_GR0_sCR0 0x0 97 - #define sCR0_CLIENTPD (1 << 0) 98 - #define sCR0_GFRE (1 << 1) 99 - #define sCR0_GFIE (1 << 2) 100 - #define sCR0_EXIDENABLE (1 << 3) 101 - #define sCR0_GCFGFRE (1 << 4) 102 - #define sCR0_GCFGFIE (1 << 5) 103 - #define sCR0_USFCFG (1 << 10) 104 - #define sCR0_VMIDPNE (1 << 11) 105 - #define sCR0_PTM (1 << 12) 106 - #define sCR0_FB (1 << 13) 107 - #define sCR0_VMID16EN (1 << 31) 108 - #define sCR0_BSU_SHIFT 14 109 - #define sCR0_BSU_MASK 0x3 110 - 111 - /* Auxiliary Configuration register */ 112 - #define ARM_SMMU_GR0_sACR 0x10 113 - 114 - /* Identification registers */ 115 - #define ARM_SMMU_GR0_ID0 0x20 116 - #define ARM_SMMU_GR0_ID1 0x24 117 - #define ARM_SMMU_GR0_ID2 0x28 118 - #define ARM_SMMU_GR0_ID3 0x2c 119 - #define ARM_SMMU_GR0_ID4 0x30 120 - #define ARM_SMMU_GR0_ID5 0x34 121 - #define ARM_SMMU_GR0_ID6 0x38 122 - #define ARM_SMMU_GR0_ID7 0x3c 123 - #define ARM_SMMU_GR0_sGFSR 0x48 124 - #define ARM_SMMU_GR0_sGFSYNR0 0x50 125 - #define ARM_SMMU_GR0_sGFSYNR1 0x54 126 - #define ARM_SMMU_GR0_sGFSYNR2 0x58 127 - 128 - #define ID0_S1TS (1 << 30) 129 - #define ID0_S2TS (1 << 29) 130 - #define ID0_NTS (1 << 28) 131 - #define ID0_SMS (1 << 27) 132 - #define ID0_ATOSNS (1 << 26) 133 - #define ID0_PTFS_NO_AARCH32 (1 << 25) 134 - #define ID0_PTFS_NO_AARCH32S (1 << 24) 135 - #define ID0_CTTW (1 << 14) 136 - #define ID0_NUMIRPT_SHIFT 16 137 - #define ID0_NUMIRPT_MASK 0xff 138 - #define ID0_NUMSIDB_SHIFT 9 139 - #define ID0_NUMSIDB_MASK 0xf 140 - #define ID0_EXIDS (1 << 8) 141 - #define ID0_NUMSMRG_SHIFT 0 142 - #define ID0_NUMSMRG_MASK 0xff 143 - 144 - #define ID1_PAGESIZE (1 << 31) 145 - #define ID1_NUMPAGENDXB_SHIFT 28 146 - #define ID1_NUMPAGENDXB_MASK 7 147 - #define ID1_NUMS2CB_SHIFT 16 148 - #define ID1_NUMS2CB_MASK 0xff 149 - #define ID1_NUMCB_SHIFT 0 150 - #define ID1_NUMCB_MASK 0xff 151 - 152 - #define ID2_OAS_SHIFT 4 153 - #define ID2_OAS_MASK 0xf 154 - #define ID2_IAS_SHIFT 0 155 - #define ID2_IAS_MASK 0xf 156 - #define ID2_UBS_SHIFT 8 157 - #define ID2_UBS_MASK 0xf 158 - #define ID2_PTFS_4K (1 << 12) 159 - #define ID2_PTFS_16K (1 << 13) 160 - #define ID2_PTFS_64K (1 << 14) 161 - #define ID2_VMID16 (1 << 15) 162 - 163 - #define ID7_MAJOR_SHIFT 4 164 - #define ID7_MAJOR_MASK 0xf 165 - 166 - /* Global TLB invalidation */ 167 - #define ARM_SMMU_GR0_TLBIVMID 0x64 168 - #define ARM_SMMU_GR0_TLBIALLNSNH 0x68 169 - #define ARM_SMMU_GR0_TLBIALLH 0x6c 170 - #define ARM_SMMU_GR0_sTLBGSYNC 0x70 171 - #define ARM_SMMU_GR0_sTLBGSTATUS 0x74 172 - #define sTLBGSTATUS_GSACTIVE (1 << 0) 173 - #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */ 174 - #define TLB_SPIN_COUNT 10 175 - 176 - /* Stream mapping registers */ 177 - #define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2)) 178 - #define SMR_VALID (1 << 31) 179 - #define SMR_MASK_SHIFT 16 180 - #define SMR_ID_SHIFT 0 181 - 182 - #define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2)) 183 - #define S2CR_CBNDX_SHIFT 0 184 - #define S2CR_CBNDX_MASK 0xff 185 - #define S2CR_EXIDVALID (1 << 10) 186 - #define S2CR_TYPE_SHIFT 16 187 - #define S2CR_TYPE_MASK 0x3 188 - enum arm_smmu_s2cr_type { 189 - S2CR_TYPE_TRANS, 190 - S2CR_TYPE_BYPASS, 191 - S2CR_TYPE_FAULT, 192 - }; 193 - 194 - #define S2CR_PRIVCFG_SHIFT 24 195 - #define S2CR_PRIVCFG_MASK 0x3 196 - enum arm_smmu_s2cr_privcfg { 197 - S2CR_PRIVCFG_DEFAULT, 198 - S2CR_PRIVCFG_DIPAN, 199 - S2CR_PRIVCFG_UNPRIV, 200 - S2CR_PRIVCFG_PRIV, 201 - }; 202 - 203 - /* Context bank attribute registers */ 204 - #define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2)) 205 - #define CBAR_VMID_SHIFT 0 206 - #define CBAR_VMID_MASK 0xff 207 - #define CBAR_S1_BPSHCFG_SHIFT 8 208 - #define CBAR_S1_BPSHCFG_MASK 3 209 - #define CBAR_S1_BPSHCFG_NSH 3 210 - #define CBAR_S1_MEMATTR_SHIFT 12 211 - #define CBAR_S1_MEMATTR_MASK 0xf 212 - #define CBAR_S1_MEMATTR_WB 0xf 213 - #define CBAR_TYPE_SHIFT 16 214 - #define CBAR_TYPE_MASK 0x3 215 - #define CBAR_TYPE_S2_TRANS (0 << CBAR_TYPE_SHIFT) 216 - #define CBAR_TYPE_S1_TRANS_S2_BYPASS (1 << CBAR_TYPE_SHIFT) 217 - #define CBAR_TYPE_S1_TRANS_S2_FAULT (2 << CBAR_TYPE_SHIFT) 218 - #define CBAR_TYPE_S1_TRANS_S2_TRANS (3 << CBAR_TYPE_SHIFT) 219 - #define CBAR_IRPTNDX_SHIFT 24 220 - #define CBAR_IRPTNDX_MASK 0xff 221 - 222 - #define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2)) 223 - #define CBA2R_RW64_32BIT (0 << 0) 224 - #define CBA2R_RW64_64BIT (1 << 0) 225 - #define CBA2R_VMID_SHIFT 16 226 - #define CBA2R_VMID_MASK 0xffff 227 - 228 86 /* Translation context bank */ 229 87 #define ARM_SMMU_CB(smmu, n) ((smmu)->cb_base + ((n) << (smmu)->pgshift)) 230 - 231 - #define ARM_SMMU_CB_SCTLR 0x0 232 - #define ARM_SMMU_CB_ACTLR 0x4 233 - #define ARM_SMMU_CB_RESUME 0x8 234 - #define ARM_SMMU_CB_TTBCR2 0x10 235 - #define ARM_SMMU_CB_TTBR0 0x20 236 - #define ARM_SMMU_CB_TTBR1 0x28 237 - #define ARM_SMMU_CB_TTBCR 0x30 238 - #define ARM_SMMU_CB_CONTEXTIDR 0x34 239 - #define ARM_SMMU_CB_S1_MAIR0 0x38 240 - #define ARM_SMMU_CB_S1_MAIR1 0x3c 241 - #define ARM_SMMU_CB_PAR 0x50 242 - #define ARM_SMMU_CB_FSR 0x58 243 - #define ARM_SMMU_CB_FAR 0x60 244 - #define ARM_SMMU_CB_FSYNR0 0x68 245 - #define ARM_SMMU_CB_S1_TLBIVA 0x600 246 - #define ARM_SMMU_CB_S1_TLBIASID 0x610 247 - #define ARM_SMMU_CB_S1_TLBIVAL 0x620 248 - #define ARM_SMMU_CB_S2_TLBIIPAS2 0x630 249 - #define ARM_SMMU_CB_S2_TLBIIPAS2L 0x638 250 - #define ARM_SMMU_CB_TLBSYNC 0x7f0 251 - #define ARM_SMMU_CB_TLBSTATUS 0x7f4 252 - #define ARM_SMMU_CB_ATS1PR 0x800 253 - #define ARM_SMMU_CB_ATSR 0x8f0 254 - 255 - #define SCTLR_S1_ASIDPNE (1 << 12) 256 - #define SCTLR_CFCFG (1 << 7) 257 - #define SCTLR_CFIE (1 << 6) 258 - #define SCTLR_CFRE (1 << 5) 259 - #define SCTLR_E (1 << 4) 260 - #define SCTLR_AFE (1 << 2) 261 - #define SCTLR_TRE (1 << 1) 262 - #define SCTLR_M (1 << 0) 263 - 264 - #define ARM_MMU500_ACTLR_CPRE (1 << 1) 265 - 266 - #define ARM_MMU500_ACR_CACHE_LOCK (1 << 26) 267 - #define ARM_MMU500_ACR_SMTNMB_TLBEN (1 << 8) 268 - 269 - #define CB_PAR_F (1 << 0) 270 - 271 - #define ATSR_ACTIVE (1 << 0) 272 - 273 - #define RESUME_RETRY (0 << 0) 274 - #define RESUME_TERMINATE (1 << 0) 275 - 276 - #define TTBCR2_SEP_SHIFT 15 277 - #define TTBCR2_SEP_UPSTREAM (0x7 << TTBCR2_SEP_SHIFT) 278 - #define TTBCR2_AS (1 << 4) 279 - 280 - #define TTBRn_ASID_SHIFT 48 281 - 282 - #define FSR_MULTI (1 << 31) 283 - #define FSR_SS (1 << 30) 284 - #define FSR_UUT (1 << 8) 285 - #define FSR_ASF (1 << 7) 286 - #define FSR_TLBLKF (1 << 6) 287 - #define FSR_TLBMCF (1 << 5) 288 - #define FSR_EF (1 << 4) 289 - #define FSR_PF (1 << 3) 290 - #define FSR_AFF (1 << 2) 291 - #define FSR_TF (1 << 1) 292 - 293 - #define FSR_IGN (FSR_AFF | FSR_ASF | \ 294 - FSR_TLBMCF | FSR_TLBLKF) 295 - #define FSR_FAULT (FSR_MULTI | FSR_SS | FSR_UUT | \ 296 - FSR_EF | FSR_PF | FSR_TF | FSR_IGN) 297 - 298 - #define FSYNR0_WNR (1 << 4) 299 88 300 89 #define MSI_IOVA_BASE 0x8000000 301 90 #define MSI_IOVA_LENGTH 0x100000 ··· 145 338 bool valid; 146 339 }; 147 340 341 + struct arm_smmu_cb { 342 + u64 ttbr[2]; 343 + u32 tcr[2]; 344 + u32 mair[2]; 345 + struct arm_smmu_cfg *cfg; 346 + }; 347 + 148 348 struct arm_smmu_master_cfg { 149 349 struct arm_smmu_device *smmu; 150 350 s16 smendx[]; ··· 194 380 u32 num_context_banks; 195 381 u32 num_s2_context_banks; 196 382 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS); 383 + struct arm_smmu_cb *cbs; 197 384 atomic_t irptndx; 198 385 199 386 u32 num_mapping_groups; ··· 591 776 static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain, 592 777 struct io_pgtable_cfg *pgtbl_cfg) 593 778 { 594 - u32 reg, reg2; 595 - u64 reg64; 596 - bool stage1; 597 779 struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 598 - struct arm_smmu_device *smmu = smmu_domain->smmu; 780 + struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx]; 781 + bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS; 782 + 783 + cb->cfg = cfg; 784 + 785 + /* TTBCR */ 786 + if (stage1) { 787 + if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) { 788 + cb->tcr[0] = pgtbl_cfg->arm_v7s_cfg.tcr; 789 + } else { 790 + cb->tcr[0] = pgtbl_cfg->arm_lpae_s1_cfg.tcr; 791 + cb->tcr[1] = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32; 792 + cb->tcr[1] |= TTBCR2_SEP_UPSTREAM; 793 + if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) 794 + cb->tcr[1] |= TTBCR2_AS; 795 + } 796 + } else { 797 + cb->tcr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vtcr; 798 + } 799 + 800 + /* TTBRs */ 801 + if (stage1) { 802 + if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) { 803 + cb->ttbr[0] = pgtbl_cfg->arm_v7s_cfg.ttbr[0]; 804 + cb->ttbr[1] = pgtbl_cfg->arm_v7s_cfg.ttbr[1]; 805 + } else { 806 + cb->ttbr[0] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0]; 807 + cb->ttbr[0] |= (u64)cfg->asid << TTBRn_ASID_SHIFT; 808 + cb->ttbr[1] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1]; 809 + cb->ttbr[1] |= (u64)cfg->asid << TTBRn_ASID_SHIFT; 810 + } 811 + } else { 812 + cb->ttbr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vttbr; 813 + } 814 + 815 + /* MAIRs (stage-1 only) */ 816 + if (stage1) { 817 + if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) { 818 + cb->mair[0] = pgtbl_cfg->arm_v7s_cfg.prrr; 819 + cb->mair[1] = pgtbl_cfg->arm_v7s_cfg.nmrr; 820 + } else { 821 + cb->mair[0] = pgtbl_cfg->arm_lpae_s1_cfg.mair[0]; 822 + cb->mair[1] = pgtbl_cfg->arm_lpae_s1_cfg.mair[1]; 823 + } 824 + } 825 + } 826 + 827 + static void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx) 828 + { 829 + u32 reg; 830 + bool stage1; 831 + struct arm_smmu_cb *cb = &smmu->cbs[idx]; 832 + struct arm_smmu_cfg *cfg = cb->cfg; 599 833 void __iomem *cb_base, *gr1_base; 834 + 835 + cb_base = ARM_SMMU_CB(smmu, idx); 836 + 837 + /* Unassigned context banks only need disabling */ 838 + if (!cfg) { 839 + writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR); 840 + return; 841 + } 600 842 601 843 gr1_base = ARM_SMMU_GR1(smmu); 602 844 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS; 603 - cb_base = ARM_SMMU_CB(smmu, cfg->cbndx); 604 845 846 + /* CBA2R */ 605 847 if (smmu->version > ARM_SMMU_V1) { 606 848 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) 607 849 reg = CBA2R_RW64_64BIT; ··· 668 796 if (smmu->features & ARM_SMMU_FEAT_VMID16) 669 797 reg |= cfg->vmid << CBA2R_VMID_SHIFT; 670 798 671 - writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx)); 799 + writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(idx)); 672 800 } 673 801 674 802 /* CBAR */ ··· 687 815 /* 8-bit VMIDs live in CBAR */ 688 816 reg |= cfg->vmid << CBAR_VMID_SHIFT; 689 817 } 690 - writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(cfg->cbndx)); 818 + writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(idx)); 691 819 692 820 /* 693 821 * TTBCR 694 822 * We must write this before the TTBRs, since it determines the 695 823 * access behaviour of some fields (in particular, ASID[15:8]). 696 824 */ 697 - if (stage1) { 698 - if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) { 699 - reg = pgtbl_cfg->arm_v7s_cfg.tcr; 700 - reg2 = 0; 701 - } else { 702 - reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr; 703 - reg2 = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32; 704 - reg2 |= TTBCR2_SEP_UPSTREAM; 705 - if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) 706 - reg2 |= TTBCR2_AS; 707 - } 708 - if (smmu->version > ARM_SMMU_V1) 709 - writel_relaxed(reg2, cb_base + ARM_SMMU_CB_TTBCR2); 710 - } else { 711 - reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr; 712 - } 713 - writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR); 825 + if (stage1 && smmu->version > ARM_SMMU_V1) 826 + writel_relaxed(cb->tcr[1], cb_base + ARM_SMMU_CB_TTBCR2); 827 + writel_relaxed(cb->tcr[0], cb_base + ARM_SMMU_CB_TTBCR); 714 828 715 829 /* TTBRs */ 716 - if (stage1) { 717 - if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) { 718 - reg = pgtbl_cfg->arm_v7s_cfg.ttbr[0]; 719 - writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0); 720 - reg = pgtbl_cfg->arm_v7s_cfg.ttbr[1]; 721 - writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1); 722 - writel_relaxed(cfg->asid, cb_base + ARM_SMMU_CB_CONTEXTIDR); 723 - } else { 724 - reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0]; 725 - reg64 |= (u64)cfg->asid << TTBRn_ASID_SHIFT; 726 - writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0); 727 - reg64 = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1]; 728 - reg64 |= (u64)cfg->asid << TTBRn_ASID_SHIFT; 729 - writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR1); 730 - } 830 + if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) { 831 + writel_relaxed(cfg->asid, cb_base + ARM_SMMU_CB_CONTEXTIDR); 832 + writel_relaxed(cb->ttbr[0], cb_base + ARM_SMMU_CB_TTBR0); 833 + writel_relaxed(cb->ttbr[1], cb_base + ARM_SMMU_CB_TTBR1); 731 834 } else { 732 - reg64 = pgtbl_cfg->arm_lpae_s2_cfg.vttbr; 733 - writeq_relaxed(reg64, cb_base + ARM_SMMU_CB_TTBR0); 835 + writeq_relaxed(cb->ttbr[0], cb_base + ARM_SMMU_CB_TTBR0); 836 + if (stage1) 837 + writeq_relaxed(cb->ttbr[1], cb_base + ARM_SMMU_CB_TTBR1); 734 838 } 735 839 736 840 /* MAIRs (stage-1 only) */ 737 841 if (stage1) { 738 - if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) { 739 - reg = pgtbl_cfg->arm_v7s_cfg.prrr; 740 - reg2 = pgtbl_cfg->arm_v7s_cfg.nmrr; 741 - } else { 742 - reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0]; 743 - reg2 = pgtbl_cfg->arm_lpae_s1_cfg.mair[1]; 744 - } 745 - writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0); 746 - writel_relaxed(reg2, cb_base + ARM_SMMU_CB_S1_MAIR1); 842 + writel_relaxed(cb->mair[0], cb_base + ARM_SMMU_CB_S1_MAIR0); 843 + writel_relaxed(cb->mair[1], cb_base + ARM_SMMU_CB_S1_MAIR1); 747 844 } 748 845 749 846 /* SCTLR */ 750 847 reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE | SCTLR_M; 751 848 if (stage1) 752 849 reg |= SCTLR_S1_ASIDPNE; 753 - #ifdef __BIG_ENDIAN 754 - reg |= SCTLR_E; 755 - #endif 850 + if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) 851 + reg |= SCTLR_E; 852 + 756 853 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR); 757 854 } 758 855 ··· 884 1043 885 1044 /* Initialise the context bank with our page table cfg */ 886 1045 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg); 1046 + arm_smmu_write_context_bank(smmu, cfg->cbndx); 887 1047 888 1048 /* 889 1049 * Request context fault interrupt. Do this last to avoid the ··· 917 1075 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); 918 1076 struct arm_smmu_device *smmu = smmu_domain->smmu; 919 1077 struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 920 - void __iomem *cb_base; 921 1078 int irq; 922 1079 923 1080 if (!smmu || domain->type == IOMMU_DOMAIN_IDENTITY) ··· 926 1085 * Disable the context bank and free the page tables before freeing 927 1086 * it. 928 1087 */ 929 - cb_base = ARM_SMMU_CB(smmu, cfg->cbndx); 930 - writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR); 1088 + smmu->cbs[cfg->cbndx].cfg = NULL; 1089 + arm_smmu_write_context_bank(smmu, cfg->cbndx); 931 1090 932 1091 if (cfg->irptndx != INVALID_IRPTNDX) { 933 1092 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx]; ··· 1577 1736 static void arm_smmu_device_reset(struct arm_smmu_device *smmu) 1578 1737 { 1579 1738 void __iomem *gr0_base = ARM_SMMU_GR0(smmu); 1580 - void __iomem *cb_base; 1581 1739 int i; 1582 1740 u32 reg, major; 1583 1741 ··· 1612 1772 1613 1773 /* Make sure all context banks are disabled and clear CB_FSR */ 1614 1774 for (i = 0; i < smmu->num_context_banks; ++i) { 1615 - cb_base = ARM_SMMU_CB(smmu, i); 1616 - writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR); 1775 + void __iomem *cb_base = ARM_SMMU_CB(smmu, i); 1776 + 1777 + arm_smmu_write_context_bank(smmu, i); 1617 1778 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR); 1618 1779 /* 1619 1780 * Disable MMU-500's not-particularly-beneficial next-page ··· 1820 1979 smmu->cavium_id_base -= smmu->num_context_banks; 1821 1980 dev_notice(smmu->dev, "\tenabling workaround for Cavium erratum 27704\n"); 1822 1981 } 1982 + smmu->cbs = devm_kcalloc(smmu->dev, smmu->num_context_banks, 1983 + sizeof(*smmu->cbs), GFP_KERNEL); 1984 + if (!smmu->cbs) 1985 + return -ENOMEM; 1823 1986 1824 1987 /* ID2 */ 1825 1988 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2); ··· 2181 2336 return 0; 2182 2337 } 2183 2338 2339 + static void arm_smmu_device_shutdown(struct platform_device *pdev) 2340 + { 2341 + arm_smmu_device_remove(pdev); 2342 + } 2343 + 2344 + static int __maybe_unused arm_smmu_pm_resume(struct device *dev) 2345 + { 2346 + struct arm_smmu_device *smmu = dev_get_drvdata(dev); 2347 + 2348 + arm_smmu_device_reset(smmu); 2349 + return 0; 2350 + } 2351 + 2352 + static SIMPLE_DEV_PM_OPS(arm_smmu_pm_ops, NULL, arm_smmu_pm_resume); 2353 + 2184 2354 static struct platform_driver arm_smmu_driver = { 2185 2355 .driver = { 2186 2356 .name = "arm-smmu", 2187 2357 .of_match_table = of_match_ptr(arm_smmu_of_match), 2358 + .pm = &arm_smmu_pm_ops, 2188 2359 }, 2189 2360 .probe = arm_smmu_device_probe, 2190 2361 .remove = arm_smmu_device_remove, 2362 + .shutdown = arm_smmu_device_shutdown, 2191 2363 }; 2192 2364 module_platform_driver(arm_smmu_driver); 2193 2365
+1 -1
drivers/iommu/dmar.c
··· 1343 1343 1344 1344 if (mask) { 1345 1345 BUG_ON(addr & ((1 << (VTD_PAGE_SHIFT + mask)) - 1)); 1346 - addr |= (1 << (VTD_PAGE_SHIFT + mask - 1)) - 1; 1346 + addr |= (1ULL << (VTD_PAGE_SHIFT + mask - 1)) - 1; 1347 1347 desc.high = QI_DEV_IOTLB_ADDR(addr) | QI_DEV_IOTLB_SIZE; 1348 1348 } else 1349 1349 desc.high = QI_DEV_IOTLB_ADDR(addr);
+11 -33
drivers/iommu/exynos-iommu.c
··· 54 54 #define lv2ent_small(pent) ((*(pent) & 2) == 2) 55 55 #define lv2ent_large(pent) ((*(pent) & 3) == 1) 56 56 57 - #ifdef CONFIG_BIG_ENDIAN 58 - #warning "revisit driver if we can enable big-endian ptes" 59 - #endif 60 - 61 57 /* 62 58 * v1.x - v3.x SYSMMU supports 32bit physical and 32bit virtual address spaces 63 59 * v5.0 introduced support for 36bit physical address space by shifting ··· 565 569 spin_unlock_irqrestore(&data->lock, flags); 566 570 } 567 571 568 - static struct iommu_ops exynos_iommu_ops; 572 + static const struct iommu_ops exynos_iommu_ops; 569 573 570 574 static int __init exynos_sysmmu_probe(struct platform_device *pdev) 571 575 { ··· 654 658 LV2_PROT = SYSMMU_V5_LV2_PROT; 655 659 } 656 660 } 661 + 662 + /* 663 + * use the first registered sysmmu device for performing 664 + * dma mapping operations on iommu page tables (cpu cache flush) 665 + */ 666 + if (!dma_dev) 667 + dma_dev = &pdev->dev; 657 668 658 669 pm_runtime_enable(dev); 659 670 ··· 1326 1323 return 0; 1327 1324 } 1328 1325 1329 - static struct iommu_ops exynos_iommu_ops = { 1326 + static const struct iommu_ops exynos_iommu_ops = { 1330 1327 .domain_alloc = exynos_iommu_domain_alloc, 1331 1328 .domain_free = exynos_iommu_domain_free, 1332 1329 .attach_dev = exynos_iommu_attach_device, ··· 1341 1338 .pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE, 1342 1339 .of_xlate = exynos_iommu_of_xlate, 1343 1340 }; 1344 - 1345 - static bool init_done; 1346 1341 1347 1342 static int __init exynos_iommu_init(void) 1348 1343 { ··· 1374 1373 goto err_set_iommu; 1375 1374 } 1376 1375 1377 - init_done = true; 1378 - 1379 1376 return 0; 1380 1377 err_set_iommu: 1381 1378 kmem_cache_free(lv2table_kmem_cache, zero_lv2_table); ··· 1383 1384 kmem_cache_destroy(lv2table_kmem_cache); 1384 1385 return ret; 1385 1386 } 1387 + core_initcall(exynos_iommu_init); 1386 1388 1387 - static int __init exynos_iommu_of_setup(struct device_node *np) 1388 - { 1389 - struct platform_device *pdev; 1390 - 1391 - if (!init_done) 1392 - exynos_iommu_init(); 1393 - 1394 - pdev = of_platform_device_create(np, NULL, platform_bus_type.dev_root); 1395 - if (!pdev) 1396 - return -ENODEV; 1397 - 1398 - /* 1399 - * use the first registered sysmmu device for performing 1400 - * dma mapping operations on iommu page tables (cpu cache flush) 1401 - */ 1402 - if (!dma_dev) 1403 - dma_dev = &pdev->dev; 1404 - 1405 - return 0; 1406 - } 1407 - 1408 - IOMMU_OF_DECLARE(exynos_iommu_of, "samsung,exynos-sysmmu", 1409 - exynos_iommu_of_setup); 1389 + IOMMU_OF_DECLARE(exynos_iommu_of, "samsung,exynos-sysmmu", NULL);
+15 -12
drivers/iommu/fsl_pamu.c
··· 42 42 static struct paace *ppaact; 43 43 static struct paace *spaact; 44 44 45 + static bool probed; /* Has PAMU been probed? */ 46 + 45 47 /* 46 48 * Table for matching compatible strings, for device tree 47 49 * guts node, for QorIQ SOCs. ··· 532 530 if (node) { 533 531 prop = of_get_property(node, "cache-stash-id", NULL); 534 532 if (!prop) { 535 - pr_debug("missing cache-stash-id at %s\n", 536 - node->full_name); 533 + pr_debug("missing cache-stash-id at %pOF\n", 534 + node); 537 535 of_node_put(node); 538 536 return ~(u32)0; 539 537 } ··· 559 557 if (stash_dest_hint == cache_level) { 560 558 prop = of_get_property(node, "cache-stash-id", NULL); 561 559 if (!prop) { 562 - pr_debug("missing cache-stash-id at %s\n", 563 - node->full_name); 560 + pr_debug("missing cache-stash-id at %pOF\n", 561 + node); 564 562 of_node_put(node); 565 563 return ~(u32)0; 566 564 } ··· 570 568 571 569 prop = of_get_property(node, "next-level-cache", NULL); 572 570 if (!prop) { 573 - pr_debug("can't find next-level-cache at %s\n", 574 - node->full_name); 571 + pr_debug("can't find next-level-cache at %pOF\n", node); 575 572 of_node_put(node); 576 573 return ~(u32)0; /* can't traverse any further */ 577 574 } ··· 1034 1033 * NOTE : All PAMUs share the same LIODN tables. 1035 1034 */ 1036 1035 1036 + if (WARN_ON(probed)) 1037 + return -EBUSY; 1038 + 1037 1039 pamu_regs = of_iomap(dev->of_node, 0); 1038 1040 if (!pamu_regs) { 1039 1041 dev_err(dev, "ioremap of PAMU node failed\n"); ··· 1067 1063 1068 1064 guts_node = of_find_matching_node(NULL, guts_device_ids); 1069 1065 if (!guts_node) { 1070 - dev_err(dev, "could not find GUTS node %s\n", 1071 - dev->of_node->full_name); 1066 + dev_err(dev, "could not find GUTS node %pOF\n", dev->of_node); 1072 1067 ret = -ENODEV; 1073 1068 goto error; 1074 1069 } ··· 1175 1172 1176 1173 setup_liodns(); 1177 1174 1175 + probed = true; 1176 + 1178 1177 return 0; 1179 1178 1180 1179 error_genpool: ··· 1251 1246 1252 1247 pdev = platform_device_alloc("fsl-of-pamu", 0); 1253 1248 if (!pdev) { 1254 - pr_err("could not allocate device %s\n", 1255 - np->full_name); 1249 + pr_err("could not allocate device %pOF\n", np); 1256 1250 ret = -ENOMEM; 1257 1251 goto error_device_alloc; 1258 1252 } ··· 1263 1259 1264 1260 ret = platform_device_add(pdev); 1265 1261 if (ret) { 1266 - pr_err("could not add device %s (err=%i)\n", 1267 - np->full_name, ret); 1262 + pr_err("could not add device %pOF (err=%i)\n", np, ret); 1268 1263 goto error_device_add; 1269 1264 } 1270 1265
+22 -6
drivers/iommu/fsl_pamu_domain.c
··· 33 33 static struct kmem_cache *iommu_devinfo_cache; 34 34 static DEFINE_SPINLOCK(device_domain_lock); 35 35 36 + struct iommu_device pamu_iommu; /* IOMMU core code handle */ 37 + 36 38 static struct fsl_dma_domain *to_fsl_dma_domain(struct iommu_domain *dom) 37 39 { 38 40 return container_of(dom, struct fsl_dma_domain, iommu_domain); ··· 621 619 for (i = 0; i < num; i++) { 622 620 /* Ensure that LIODN value is valid */ 623 621 if (liodn[i] >= PAACE_NUMBER_ENTRIES) { 624 - pr_debug("Invalid liodn %d, attach device failed for %s\n", 625 - liodn[i], dev->of_node->full_name); 622 + pr_debug("Invalid liodn %d, attach device failed for %pOF\n", 623 + liodn[i], dev->of_node); 626 624 ret = -EINVAL; 627 625 break; 628 626 } ··· 686 684 liodn_cnt = len / sizeof(u32); 687 685 ret = handle_attach_device(dma_domain, dev, liodn, liodn_cnt); 688 686 } else { 689 - pr_debug("missing fsl,liodn property at %s\n", 690 - dev->of_node->full_name); 687 + pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node); 691 688 ret = -EINVAL; 692 689 } 693 690 ··· 721 720 if (prop) 722 721 detach_device(dev, dma_domain); 723 722 else 724 - pr_debug("missing fsl,liodn property at %s\n", 725 - dev->of_node->full_name); 723 + pr_debug("missing fsl,liodn property at %pOF\n", dev->of_node); 726 724 } 727 725 728 726 static int configure_domain_geometry(struct iommu_domain *domain, void *data) ··· 983 983 984 984 iommu_group_put(group); 985 985 986 + iommu_device_link(&pamu_iommu, dev); 987 + 986 988 return 0; 987 989 } 988 990 989 991 static void fsl_pamu_remove_device(struct device *dev) 990 992 { 993 + iommu_device_unlink(&pamu_iommu, dev); 991 994 iommu_group_remove_device(dev); 992 995 } 993 996 ··· 1075 1072 ret = iommu_init_mempool(); 1076 1073 if (ret) 1077 1074 return ret; 1075 + 1076 + ret = iommu_device_sysfs_add(&pamu_iommu, NULL, NULL, "iommu0"); 1077 + if (ret) 1078 + return ret; 1079 + 1080 + iommu_device_set_ops(&pamu_iommu, &fsl_pamu_ops); 1081 + 1082 + ret = iommu_device_register(&pamu_iommu); 1083 + if (ret) { 1084 + iommu_device_sysfs_remove(&pamu_iommu); 1085 + pr_err("Can't register iommu device\n"); 1086 + return ret; 1087 + } 1078 1088 1079 1089 bus_set_iommu(&platform_bus_type, &fsl_pamu_ops); 1080 1090 bus_set_iommu(&pci_bus_type, &fsl_pamu_ops);
+90 -190
drivers/iommu/intel-iommu.c
··· 458 458 #define for_each_rmrr_units(rmrr) \ 459 459 list_for_each_entry(rmrr, &dmar_rmrr_units, list) 460 460 461 - static void flush_unmaps_timeout(unsigned long data); 462 - 463 - struct deferred_flush_entry { 464 - unsigned long iova_pfn; 465 - unsigned long nrpages; 466 - struct dmar_domain *domain; 467 - struct page *freelist; 468 - }; 469 - 470 - #define HIGH_WATER_MARK 250 471 - struct deferred_flush_table { 472 - int next; 473 - struct deferred_flush_entry entries[HIGH_WATER_MARK]; 474 - }; 475 - 476 - struct deferred_flush_data { 477 - spinlock_t lock; 478 - int timer_on; 479 - struct timer_list timer; 480 - long size; 481 - struct deferred_flush_table *tables; 482 - }; 483 - 484 - static DEFINE_PER_CPU(struct deferred_flush_data, deferred_flush); 485 - 486 461 /* bitmap for indexing intel_iommus */ 487 462 static int g_num_of_iommus; 488 463 ··· 956 981 return ret; 957 982 } 958 983 959 - static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn) 960 - { 961 - struct context_entry *context; 962 - unsigned long flags; 963 - 964 - spin_lock_irqsave(&iommu->lock, flags); 965 - context = iommu_context_addr(iommu, bus, devfn, 0); 966 - if (context) { 967 - context_clear_entry(context); 968 - __iommu_flush_cache(iommu, context, sizeof(*context)); 969 - } 970 - spin_unlock_irqrestore(&iommu->lock, flags); 971 - } 972 - 973 984 static void free_context_table(struct intel_iommu *iommu) 974 985 { 975 986 int i; ··· 1105 1144 } 1106 1145 1107 1146 static void dma_pte_free_level(struct dmar_domain *domain, int level, 1108 - struct dma_pte *pte, unsigned long pfn, 1109 - unsigned long start_pfn, unsigned long last_pfn) 1147 + int retain_level, struct dma_pte *pte, 1148 + unsigned long pfn, unsigned long start_pfn, 1149 + unsigned long last_pfn) 1110 1150 { 1111 1151 pfn = max(start_pfn, pfn); 1112 1152 pte = &pte[pfn_level_offset(pfn, level)]; ··· 1122 1160 level_pfn = pfn & level_mask(level); 1123 1161 level_pte = phys_to_virt(dma_pte_addr(pte)); 1124 1162 1125 - if (level > 2) 1126 - dma_pte_free_level(domain, level - 1, level_pte, 1127 - level_pfn, start_pfn, last_pfn); 1163 + if (level > 2) { 1164 + dma_pte_free_level(domain, level - 1, retain_level, 1165 + level_pte, level_pfn, start_pfn, 1166 + last_pfn); 1167 + } 1128 1168 1129 - /* If range covers entire pagetable, free it */ 1130 - if (!(start_pfn > level_pfn || 1169 + /* 1170 + * Free the page table if we're below the level we want to 1171 + * retain and the range covers the entire table. 1172 + */ 1173 + if (level < retain_level && !(start_pfn > level_pfn || 1131 1174 last_pfn < level_pfn + level_size(level) - 1)) { 1132 1175 dma_clear_pte(pte); 1133 1176 domain_flush_cache(domain, pte, sizeof(*pte)); ··· 1143 1176 } while (!first_pte_in_page(++pte) && pfn <= last_pfn); 1144 1177 } 1145 1178 1146 - /* clear last level (leaf) ptes and free page table pages. */ 1179 + /* 1180 + * clear last level (leaf) ptes and free page table pages below the 1181 + * level we wish to keep intact. 1182 + */ 1147 1183 static void dma_pte_free_pagetable(struct dmar_domain *domain, 1148 1184 unsigned long start_pfn, 1149 - unsigned long last_pfn) 1185 + unsigned long last_pfn, 1186 + int retain_level) 1150 1187 { 1151 1188 BUG_ON(!domain_pfn_supported(domain, start_pfn)); 1152 1189 BUG_ON(!domain_pfn_supported(domain, last_pfn)); ··· 1159 1188 dma_pte_clear_range(domain, start_pfn, last_pfn); 1160 1189 1161 1190 /* We don't need lock here; nobody else touches the iova range */ 1162 - dma_pte_free_level(domain, agaw_to_level(domain->agaw), 1191 + dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level, 1163 1192 domain->pgd, 0, start_pfn, last_pfn); 1164 1193 1165 1194 /* free pgd */ ··· 1285 1314 freelist = pg->freelist; 1286 1315 free_pgtable_page(page_address(pg)); 1287 1316 } 1317 + } 1318 + 1319 + static void iova_entry_free(unsigned long data) 1320 + { 1321 + struct page *freelist = (struct page *)data; 1322 + 1323 + dma_free_pagelist(freelist); 1288 1324 } 1289 1325 1290 1326 /* iommu handling */ ··· 1607 1629 addr, mask); 1608 1630 } 1609 1631 1632 + static void iommu_flush_iova(struct iova_domain *iovad) 1633 + { 1634 + struct dmar_domain *domain; 1635 + int idx; 1636 + 1637 + domain = container_of(iovad, struct dmar_domain, iovad); 1638 + 1639 + for_each_domain_iommu(idx, domain) { 1640 + struct intel_iommu *iommu = g_iommus[idx]; 1641 + u16 did = domain->iommu_did[iommu->seq_id]; 1642 + 1643 + iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); 1644 + 1645 + if (!cap_caching_mode(iommu->cap)) 1646 + iommu_flush_dev_iotlb(get_iommu_domain(iommu, did), 1647 + 0, MAX_AGAW_PFN_WIDTH); 1648 + } 1649 + } 1650 + 1610 1651 static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu) 1611 1652 { 1612 1653 u32 pmen; ··· 1936 1939 { 1937 1940 int adjust_width, agaw; 1938 1941 unsigned long sagaw; 1942 + int err; 1939 1943 1940 1944 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN, 1941 1945 DMA_32BIT_PFN); 1946 + 1947 + err = init_iova_flush_queue(&domain->iovad, 1948 + iommu_flush_iova, iova_entry_free); 1949 + if (err) 1950 + return err; 1951 + 1942 1952 domain_reserve_special_ranges(domain); 1943 1953 1944 1954 /* calculate AGAW */ ··· 1996 1992 /* Domain 0 is reserved, so dont process it */ 1997 1993 if (!domain) 1998 1994 return; 1999 - 2000 - /* Flush any lazy unmaps that may reference this domain */ 2001 - if (!intel_iommu_strict) { 2002 - int cpu; 2003 - 2004 - for_each_possible_cpu(cpu) 2005 - flush_unmaps_timeout(cpu); 2006 - } 2007 1995 2008 1996 /* Remove associated devices and clear attached or cached domains */ 2009 1997 rcu_read_lock(); ··· 2280 2284 /* 2281 2285 * Ensure that old small page tables are 2282 2286 * removed to make room for superpage(s). 2287 + * We're adding new large pages, so make sure 2288 + * we don't remove their parent tables. 2283 2289 */ 2284 - dma_pte_free_pagetable(domain, iov_pfn, end_pfn); 2290 + dma_pte_free_pagetable(domain, iov_pfn, end_pfn, 2291 + largepage_lvl + 1); 2285 2292 } else { 2286 2293 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE; 2287 2294 } ··· 2357 2358 2358 2359 static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn) 2359 2360 { 2361 + unsigned long flags; 2362 + struct context_entry *context; 2363 + u16 did_old; 2364 + 2360 2365 if (!iommu) 2361 2366 return; 2362 2367 2363 - clear_context_table(iommu, bus, devfn); 2364 - iommu->flush.flush_context(iommu, 0, 0, 0, 2365 - DMA_CCMD_GLOBAL_INVL); 2366 - iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); 2368 + spin_lock_irqsave(&iommu->lock, flags); 2369 + context = iommu_context_addr(iommu, bus, devfn, 0); 2370 + if (!context) { 2371 + spin_unlock_irqrestore(&iommu->lock, flags); 2372 + return; 2373 + } 2374 + did_old = context_domain_id(context); 2375 + context_clear_entry(context); 2376 + __iommu_flush_cache(iommu, context, sizeof(*context)); 2377 + spin_unlock_irqrestore(&iommu->lock, flags); 2378 + iommu->flush.flush_context(iommu, 2379 + did_old, 2380 + (((u16)bus) << 8) | devfn, 2381 + DMA_CCMD_MASK_NOBIT, 2382 + DMA_CCMD_DEVICE_INVL); 2383 + iommu->flush.flush_iotlb(iommu, 2384 + did_old, 2385 + 0, 2386 + 0, 2387 + DMA_TLB_DSI_FLUSH); 2367 2388 } 2368 2389 2369 2390 static inline void unlink_domain_info(struct device_domain_info *info) ··· 3232 3213 bool copied_tables = false; 3233 3214 struct device *dev; 3234 3215 struct intel_iommu *iommu; 3235 - int i, ret, cpu; 3216 + int i, ret; 3236 3217 3237 3218 /* 3238 3219 * for each drhd ··· 3263 3244 pr_err("Allocating global iommu array failed\n"); 3264 3245 ret = -ENOMEM; 3265 3246 goto error; 3266 - } 3267 - 3268 - for_each_possible_cpu(cpu) { 3269 - struct deferred_flush_data *dfd = per_cpu_ptr(&deferred_flush, 3270 - cpu); 3271 - 3272 - dfd->tables = kzalloc(g_num_of_iommus * 3273 - sizeof(struct deferred_flush_table), 3274 - GFP_KERNEL); 3275 - if (!dfd->tables) { 3276 - ret = -ENOMEM; 3277 - goto free_g_iommus; 3278 - } 3279 - 3280 - spin_lock_init(&dfd->lock); 3281 - setup_timer(&dfd->timer, flush_unmaps_timeout, cpu); 3282 3247 } 3283 3248 3284 3249 for_each_active_iommu(iommu, drhd) { ··· 3447 3444 disable_dmar_iommu(iommu); 3448 3445 free_dmar_iommu(iommu); 3449 3446 } 3450 - free_g_iommus: 3451 - for_each_possible_cpu(cpu) 3452 - kfree(per_cpu_ptr(&deferred_flush, cpu)->tables); 3447 + 3453 3448 kfree(g_iommus); 3449 + 3454 3450 error: 3455 3451 return ret; 3456 3452 } ··· 3654 3652 dir, *dev->dma_mask); 3655 3653 } 3656 3654 3657 - static void flush_unmaps(struct deferred_flush_data *flush_data) 3658 - { 3659 - int i, j; 3660 - 3661 - flush_data->timer_on = 0; 3662 - 3663 - /* just flush them all */ 3664 - for (i = 0; i < g_num_of_iommus; i++) { 3665 - struct intel_iommu *iommu = g_iommus[i]; 3666 - struct deferred_flush_table *flush_table = 3667 - &flush_data->tables[i]; 3668 - if (!iommu) 3669 - continue; 3670 - 3671 - if (!flush_table->next) 3672 - continue; 3673 - 3674 - /* In caching mode, global flushes turn emulation expensive */ 3675 - if (!cap_caching_mode(iommu->cap)) 3676 - iommu->flush.flush_iotlb(iommu, 0, 0, 0, 3677 - DMA_TLB_GLOBAL_FLUSH); 3678 - for (j = 0; j < flush_table->next; j++) { 3679 - unsigned long mask; 3680 - struct deferred_flush_entry *entry = 3681 - &flush_table->entries[j]; 3682 - unsigned long iova_pfn = entry->iova_pfn; 3683 - unsigned long nrpages = entry->nrpages; 3684 - struct dmar_domain *domain = entry->domain; 3685 - struct page *freelist = entry->freelist; 3686 - 3687 - /* On real hardware multiple invalidations are expensive */ 3688 - if (cap_caching_mode(iommu->cap)) 3689 - iommu_flush_iotlb_psi(iommu, domain, 3690 - mm_to_dma_pfn(iova_pfn), 3691 - nrpages, !freelist, 0); 3692 - else { 3693 - mask = ilog2(nrpages); 3694 - iommu_flush_dev_iotlb(domain, 3695 - (uint64_t)iova_pfn << PAGE_SHIFT, mask); 3696 - } 3697 - free_iova_fast(&domain->iovad, iova_pfn, nrpages); 3698 - if (freelist) 3699 - dma_free_pagelist(freelist); 3700 - } 3701 - flush_table->next = 0; 3702 - } 3703 - 3704 - flush_data->size = 0; 3705 - } 3706 - 3707 - static void flush_unmaps_timeout(unsigned long cpuid) 3708 - { 3709 - struct deferred_flush_data *flush_data = per_cpu_ptr(&deferred_flush, cpuid); 3710 - unsigned long flags; 3711 - 3712 - spin_lock_irqsave(&flush_data->lock, flags); 3713 - flush_unmaps(flush_data); 3714 - spin_unlock_irqrestore(&flush_data->lock, flags); 3715 - } 3716 - 3717 - static void add_unmap(struct dmar_domain *dom, unsigned long iova_pfn, 3718 - unsigned long nrpages, struct page *freelist) 3719 - { 3720 - unsigned long flags; 3721 - int entry_id, iommu_id; 3722 - struct intel_iommu *iommu; 3723 - struct deferred_flush_entry *entry; 3724 - struct deferred_flush_data *flush_data; 3725 - 3726 - flush_data = raw_cpu_ptr(&deferred_flush); 3727 - 3728 - /* Flush all CPUs' entries to avoid deferring too much. If 3729 - * this becomes a bottleneck, can just flush us, and rely on 3730 - * flush timer for the rest. 3731 - */ 3732 - if (flush_data->size == HIGH_WATER_MARK) { 3733 - int cpu; 3734 - 3735 - for_each_online_cpu(cpu) 3736 - flush_unmaps_timeout(cpu); 3737 - } 3738 - 3739 - spin_lock_irqsave(&flush_data->lock, flags); 3740 - 3741 - iommu = domain_get_iommu(dom); 3742 - iommu_id = iommu->seq_id; 3743 - 3744 - entry_id = flush_data->tables[iommu_id].next; 3745 - ++(flush_data->tables[iommu_id].next); 3746 - 3747 - entry = &flush_data->tables[iommu_id].entries[entry_id]; 3748 - entry->domain = dom; 3749 - entry->iova_pfn = iova_pfn; 3750 - entry->nrpages = nrpages; 3751 - entry->freelist = freelist; 3752 - 3753 - if (!flush_data->timer_on) { 3754 - mod_timer(&flush_data->timer, jiffies + msecs_to_jiffies(10)); 3755 - flush_data->timer_on = 1; 3756 - } 3757 - flush_data->size++; 3758 - spin_unlock_irqrestore(&flush_data->lock, flags); 3759 - } 3760 - 3761 3655 static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size) 3762 3656 { 3763 3657 struct dmar_domain *domain; ··· 3689 3791 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages)); 3690 3792 dma_free_pagelist(freelist); 3691 3793 } else { 3692 - add_unmap(domain, iova_pfn, nrpages, freelist); 3794 + queue_iova(&domain->iovad, iova_pfn, nrpages, 3795 + (unsigned long)freelist); 3693 3796 /* 3694 3797 * queue up the release of the unmap to save the 1/6th of the 3695 3798 * cpu used up by the iotlb flush operation... ··· 3844 3945 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot); 3845 3946 if (unlikely(ret)) { 3846 3947 dma_pte_free_pagetable(domain, start_vpfn, 3847 - start_vpfn + size - 1); 3948 + start_vpfn + size - 1, 3949 + agaw_to_level(domain->agaw) + 1); 3848 3950 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size)); 3849 3951 return 0; 3850 3952 } ··· 4628 4728 static int intel_iommu_cpu_dead(unsigned int cpu) 4629 4729 { 4630 4730 free_all_cpu_cached_iovas(cpu); 4631 - flush_unmaps_timeout(cpu); 4632 4731 return 0; 4633 4732 } 4634 4733 ··· 5249 5350 sdev->sid = PCI_DEVID(info->bus, info->devfn); 5250 5351 5251 5352 if (!(ctx_lo & CONTEXT_PASIDE)) { 5252 - context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table); 5353 + if (iommu->pasid_state_table) 5354 + context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table); 5253 5355 context[1].lo = (u64)virt_to_phys(iommu->pasid_table) | 5254 5356 intel_iommu_get_pts(iommu); 5255 5357
+14
drivers/iommu/intel-svm.c
··· 24 24 #include <linux/pci-ats.h> 25 25 #include <linux/dmar.h> 26 26 #include <linux/interrupt.h> 27 + #include <asm/page.h> 27 28 28 29 static irqreturn_t prq_event_thread(int irq, void *d); 29 30 ··· 547 546 return (requested & ~vma->vm_flags) != 0; 548 547 } 549 548 549 + static bool is_canonical_address(u64 addr) 550 + { 551 + int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1); 552 + long saddr = (long) addr; 553 + 554 + return (((saddr << shift) >> shift) == saddr); 555 + } 556 + 550 557 static irqreturn_t prq_event_thread(int irq, void *d) 551 558 { 552 559 struct intel_iommu *iommu = d; ··· 612 603 /* If the mm is already defunct, don't handle faults. */ 613 604 if (!mmget_not_zero(svm->mm)) 614 605 goto bad_req; 606 + 607 + /* If address is not canonical, return invalid response */ 608 + if (!is_canonical_address(address)) 609 + goto bad_req; 610 + 615 611 down_read(&svm->mm->mmap_sem); 616 612 vma = find_extend_vma(svm->mm, address); 617 613 if (!vma || address < vma->vm_start)
+41 -18
drivers/iommu/iommu.c
··· 527 527 528 528 } 529 529 530 + iommu_flush_tlb_all(domain); 531 + 530 532 out: 531 533 iommu_put_resv_regions(dev, &mappings); 532 534 ··· 1007 1005 if (group) 1008 1006 return group; 1009 1007 1010 - group = ERR_PTR(-EINVAL); 1008 + if (!ops) 1009 + return ERR_PTR(-EINVAL); 1011 1010 1012 - if (ops && ops->device_group) 1013 - group = ops->device_group(dev); 1014 - 1011 + group = ops->device_group(dev); 1015 1012 if (WARN_ON_ONCE(group == NULL)) 1016 1013 return ERR_PTR(-EINVAL); 1017 1014 ··· 1284 1283 struct device *dev) 1285 1284 { 1286 1285 int ret; 1286 + if ((domain->ops->is_attach_deferred != NULL) && 1287 + domain->ops->is_attach_deferred(domain, dev)) 1288 + return 0; 1289 + 1287 1290 if (unlikely(domain->ops->attach_dev == NULL)) 1288 1291 return -ENODEV; 1289 1292 ··· 1303 1298 int ret; 1304 1299 1305 1300 group = iommu_group_get(dev); 1306 - /* FIXME: Remove this when groups a mandatory for iommu drivers */ 1307 - if (group == NULL) 1308 - return __iommu_attach_device(domain, dev); 1309 - 1310 1301 /* 1311 - * We have a group - lock it to make sure the device-count doesn't 1302 + * Lock the group to make sure the device-count doesn't 1312 1303 * change while we are attaching 1313 1304 */ 1314 1305 mutex_lock(&group->mutex); ··· 1325 1324 static void __iommu_detach_device(struct iommu_domain *domain, 1326 1325 struct device *dev) 1327 1326 { 1327 + if ((domain->ops->is_attach_deferred != NULL) && 1328 + domain->ops->is_attach_deferred(domain, dev)) 1329 + return; 1330 + 1328 1331 if (unlikely(domain->ops->detach_dev == NULL)) 1329 1332 return; 1330 1333 ··· 1341 1336 struct iommu_group *group; 1342 1337 1343 1338 group = iommu_group_get(dev); 1344 - /* FIXME: Remove this when groups a mandatory for iommu drivers */ 1345 - if (group == NULL) 1346 - return __iommu_detach_device(domain, dev); 1347 1339 1348 1340 mutex_lock(&group->mutex); 1349 1341 if (iommu_group_device_count(group) != 1) { ··· 1362 1360 struct iommu_group *group; 1363 1361 1364 1362 group = iommu_group_get(dev); 1365 - /* FIXME: Remove this when groups a mandatory for iommu drivers */ 1366 - if (group == NULL) 1363 + if (!group) 1367 1364 return NULL; 1368 1365 1369 1366 domain = group->domain; ··· 1557 1556 } 1558 1557 EXPORT_SYMBOL_GPL(iommu_map); 1559 1558 1560 - size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size) 1559 + static size_t __iommu_unmap(struct iommu_domain *domain, 1560 + unsigned long iova, size_t size, 1561 + bool sync) 1561 1562 { 1563 + const struct iommu_ops *ops = domain->ops; 1562 1564 size_t unmapped_page, unmapped = 0; 1563 - unsigned int min_pagesz; 1564 1565 unsigned long orig_iova = iova; 1566 + unsigned int min_pagesz; 1565 1567 1566 - if (unlikely(domain->ops->unmap == NULL || 1568 + if (unlikely(ops->unmap == NULL || 1567 1569 domain->pgsize_bitmap == 0UL)) 1568 1570 return -ENODEV; 1569 1571 ··· 1596 1592 while (unmapped < size) { 1597 1593 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped); 1598 1594 1599 - unmapped_page = domain->ops->unmap(domain, iova, pgsize); 1595 + unmapped_page = ops->unmap(domain, iova, pgsize); 1600 1596 if (!unmapped_page) 1601 1597 break; 1598 + 1599 + if (sync && ops->iotlb_range_add) 1600 + ops->iotlb_range_add(domain, iova, pgsize); 1602 1601 1603 1602 pr_debug("unmapped: iova 0x%lx size 0x%zx\n", 1604 1603 iova, unmapped_page); ··· 1610 1603 unmapped += unmapped_page; 1611 1604 } 1612 1605 1606 + if (sync && ops->iotlb_sync) 1607 + ops->iotlb_sync(domain); 1608 + 1613 1609 trace_unmap(orig_iova, size, unmapped); 1614 1610 return unmapped; 1615 1611 } 1612 + 1613 + size_t iommu_unmap(struct iommu_domain *domain, 1614 + unsigned long iova, size_t size) 1615 + { 1616 + return __iommu_unmap(domain, iova, size, true); 1617 + } 1616 1618 EXPORT_SYMBOL_GPL(iommu_unmap); 1619 + 1620 + size_t iommu_unmap_fast(struct iommu_domain *domain, 1621 + unsigned long iova, size_t size) 1622 + { 1623 + return __iommu_unmap(domain, iova, size, false); 1624 + } 1625 + EXPORT_SYMBOL_GPL(iommu_unmap_fast); 1617 1626 1618 1627 size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, 1619 1628 struct scatterlist *sg, unsigned int nents, int prot)
+183
drivers/iommu/iova.c
··· 32 32 unsigned long limit_pfn); 33 33 static void init_iova_rcaches(struct iova_domain *iovad); 34 34 static void free_iova_rcaches(struct iova_domain *iovad); 35 + static void fq_destroy_all_entries(struct iova_domain *iovad); 36 + static void fq_flush_timeout(unsigned long data); 35 37 36 38 void 37 39 init_iova_domain(struct iova_domain *iovad, unsigned long granule, ··· 52 50 iovad->granule = granule; 53 51 iovad->start_pfn = start_pfn; 54 52 iovad->dma_32bit_pfn = pfn_32bit + 1; 53 + iovad->flush_cb = NULL; 54 + iovad->fq = NULL; 55 55 init_iova_rcaches(iovad); 56 56 } 57 57 EXPORT_SYMBOL_GPL(init_iova_domain); 58 + 59 + static void free_iova_flush_queue(struct iova_domain *iovad) 60 + { 61 + if (!iovad->fq) 62 + return; 63 + 64 + if (timer_pending(&iovad->fq_timer)) 65 + del_timer(&iovad->fq_timer); 66 + 67 + fq_destroy_all_entries(iovad); 68 + 69 + free_percpu(iovad->fq); 70 + 71 + iovad->fq = NULL; 72 + iovad->flush_cb = NULL; 73 + iovad->entry_dtor = NULL; 74 + } 75 + 76 + int init_iova_flush_queue(struct iova_domain *iovad, 77 + iova_flush_cb flush_cb, iova_entry_dtor entry_dtor) 78 + { 79 + int cpu; 80 + 81 + atomic64_set(&iovad->fq_flush_start_cnt, 0); 82 + atomic64_set(&iovad->fq_flush_finish_cnt, 0); 83 + 84 + iovad->fq = alloc_percpu(struct iova_fq); 85 + if (!iovad->fq) 86 + return -ENOMEM; 87 + 88 + iovad->flush_cb = flush_cb; 89 + iovad->entry_dtor = entry_dtor; 90 + 91 + for_each_possible_cpu(cpu) { 92 + struct iova_fq *fq; 93 + 94 + fq = per_cpu_ptr(iovad->fq, cpu); 95 + fq->head = 0; 96 + fq->tail = 0; 97 + 98 + spin_lock_init(&fq->lock); 99 + } 100 + 101 + setup_timer(&iovad->fq_timer, fq_flush_timeout, (unsigned long)iovad); 102 + atomic_set(&iovad->fq_timer_on, 0); 103 + 104 + return 0; 105 + } 106 + EXPORT_SYMBOL_GPL(init_iova_flush_queue); 58 107 59 108 static struct rb_node * 60 109 __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn) ··· 476 423 } 477 424 EXPORT_SYMBOL_GPL(free_iova_fast); 478 425 426 + #define fq_ring_for_each(i, fq) \ 427 + for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE) 428 + 429 + static inline bool fq_full(struct iova_fq *fq) 430 + { 431 + assert_spin_locked(&fq->lock); 432 + return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head); 433 + } 434 + 435 + static inline unsigned fq_ring_add(struct iova_fq *fq) 436 + { 437 + unsigned idx = fq->tail; 438 + 439 + assert_spin_locked(&fq->lock); 440 + 441 + fq->tail = (idx + 1) % IOVA_FQ_SIZE; 442 + 443 + return idx; 444 + } 445 + 446 + static void fq_ring_free(struct iova_domain *iovad, struct iova_fq *fq) 447 + { 448 + u64 counter = atomic64_read(&iovad->fq_flush_finish_cnt); 449 + unsigned idx; 450 + 451 + assert_spin_locked(&fq->lock); 452 + 453 + fq_ring_for_each(idx, fq) { 454 + 455 + if (fq->entries[idx].counter >= counter) 456 + break; 457 + 458 + if (iovad->entry_dtor) 459 + iovad->entry_dtor(fq->entries[idx].data); 460 + 461 + free_iova_fast(iovad, 462 + fq->entries[idx].iova_pfn, 463 + fq->entries[idx].pages); 464 + 465 + fq->head = (fq->head + 1) % IOVA_FQ_SIZE; 466 + } 467 + } 468 + 469 + static void iova_domain_flush(struct iova_domain *iovad) 470 + { 471 + atomic64_inc(&iovad->fq_flush_start_cnt); 472 + iovad->flush_cb(iovad); 473 + atomic64_inc(&iovad->fq_flush_finish_cnt); 474 + } 475 + 476 + static void fq_destroy_all_entries(struct iova_domain *iovad) 477 + { 478 + int cpu; 479 + 480 + /* 481 + * This code runs when the iova_domain is being detroyed, so don't 482 + * bother to free iovas, just call the entry_dtor on all remaining 483 + * entries. 484 + */ 485 + if (!iovad->entry_dtor) 486 + return; 487 + 488 + for_each_possible_cpu(cpu) { 489 + struct iova_fq *fq = per_cpu_ptr(iovad->fq, cpu); 490 + int idx; 491 + 492 + fq_ring_for_each(idx, fq) 493 + iovad->entry_dtor(fq->entries[idx].data); 494 + } 495 + } 496 + 497 + static void fq_flush_timeout(unsigned long data) 498 + { 499 + struct iova_domain *iovad = (struct iova_domain *)data; 500 + int cpu; 501 + 502 + atomic_set(&iovad->fq_timer_on, 0); 503 + iova_domain_flush(iovad); 504 + 505 + for_each_possible_cpu(cpu) { 506 + unsigned long flags; 507 + struct iova_fq *fq; 508 + 509 + fq = per_cpu_ptr(iovad->fq, cpu); 510 + spin_lock_irqsave(&fq->lock, flags); 511 + fq_ring_free(iovad, fq); 512 + spin_unlock_irqrestore(&fq->lock, flags); 513 + } 514 + } 515 + 516 + void queue_iova(struct iova_domain *iovad, 517 + unsigned long pfn, unsigned long pages, 518 + unsigned long data) 519 + { 520 + struct iova_fq *fq = get_cpu_ptr(iovad->fq); 521 + unsigned long flags; 522 + unsigned idx; 523 + 524 + spin_lock_irqsave(&fq->lock, flags); 525 + 526 + /* 527 + * First remove all entries from the flush queue that have already been 528 + * flushed out on another CPU. This makes the fq_full() check below less 529 + * likely to be true. 530 + */ 531 + fq_ring_free(iovad, fq); 532 + 533 + if (fq_full(fq)) { 534 + iova_domain_flush(iovad); 535 + fq_ring_free(iovad, fq); 536 + } 537 + 538 + idx = fq_ring_add(fq); 539 + 540 + fq->entries[idx].iova_pfn = pfn; 541 + fq->entries[idx].pages = pages; 542 + fq->entries[idx].data = data; 543 + fq->entries[idx].counter = atomic64_read(&iovad->fq_flush_start_cnt); 544 + 545 + spin_unlock_irqrestore(&fq->lock, flags); 546 + 547 + if (atomic_cmpxchg(&iovad->fq_timer_on, 0, 1) == 0) 548 + mod_timer(&iovad->fq_timer, 549 + jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT)); 550 + 551 + put_cpu_ptr(iovad->fq); 552 + } 553 + EXPORT_SYMBOL_GPL(queue_iova); 554 + 479 555 /** 480 556 * put_iova_domain - destroys the iova doamin 481 557 * @iovad: - iova domain in question. ··· 615 433 struct rb_node *node; 616 434 unsigned long flags; 617 435 436 + free_iova_flush_queue(iovad); 618 437 free_iova_rcaches(iovad); 619 438 spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); 620 439 node = rb_first(&iovad->rbroot);
+74 -168
drivers/iommu/ipmmu-vmsa.c
··· 19 19 #include <linux/iommu.h> 20 20 #include <linux/module.h> 21 21 #include <linux/of.h> 22 + #include <linux/of_platform.h> 22 23 #include <linux/platform_device.h> 23 24 #include <linux/sizes.h> 24 25 #include <linux/slab.h> ··· 36 35 struct ipmmu_vmsa_device { 37 36 struct device *dev; 38 37 void __iomem *base; 39 - struct list_head list; 38 + struct iommu_device iommu; 40 39 41 40 unsigned int num_utlbs; 42 41 spinlock_t lock; /* Protects ctx and domains[] */ ··· 59 58 60 59 struct ipmmu_vmsa_iommu_priv { 61 60 struct ipmmu_vmsa_device *mmu; 62 - unsigned int *utlbs; 63 - unsigned int num_utlbs; 64 61 struct device *dev; 65 62 struct list_head list; 66 63 }; 67 - 68 - static DEFINE_SPINLOCK(ipmmu_devices_lock); 69 - static LIST_HEAD(ipmmu_devices); 70 64 71 65 static struct ipmmu_vmsa_domain *to_vmsa_domain(struct iommu_domain *dom) 72 66 { 73 67 return container_of(dom, struct ipmmu_vmsa_domain, io_domain); 74 68 } 75 69 76 - 77 70 static struct ipmmu_vmsa_iommu_priv *to_priv(struct device *dev) 78 71 { 79 - #if defined(CONFIG_ARM) 80 - return dev->archdata.iommu; 81 - #else 82 - return dev->iommu_fwspec->iommu_priv; 83 - #endif 84 - } 85 - static void set_priv(struct device *dev, struct ipmmu_vmsa_iommu_priv *p) 86 - { 87 - #if defined(CONFIG_ARM) 88 - dev->archdata.iommu = p; 89 - #else 90 - dev->iommu_fwspec->iommu_priv = p; 91 - #endif 72 + return dev->iommu_fwspec ? dev->iommu_fwspec->iommu_priv : NULL; 92 73 } 93 74 94 75 #define TLB_LOOP_TIMEOUT 100 /* 100us */ ··· 295 312 /* The hardware doesn't support selective TLB flush. */ 296 313 } 297 314 298 - static struct iommu_gather_ops ipmmu_gather_ops = { 315 + static const struct iommu_gather_ops ipmmu_gather_ops = { 299 316 .tlb_flush_all = ipmmu_tlb_flush_all, 300 317 .tlb_add_flush = ipmmu_tlb_add_flush, 301 318 .tlb_sync = ipmmu_tlb_flush_all, ··· 322 339 spin_unlock_irqrestore(&mmu->lock, flags); 323 340 324 341 return ret; 342 + } 343 + 344 + static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu, 345 + unsigned int context_id) 346 + { 347 + unsigned long flags; 348 + 349 + spin_lock_irqsave(&mmu->lock, flags); 350 + 351 + clear_bit(context_id, mmu->ctx); 352 + mmu->domains[context_id] = NULL; 353 + 354 + spin_unlock_irqrestore(&mmu->lock, flags); 325 355 } 326 356 327 357 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain) ··· 366 370 */ 367 371 domain->cfg.iommu_dev = domain->mmu->dev; 368 372 369 - domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg, 370 - domain); 371 - if (!domain->iop) 372 - return -EINVAL; 373 - 374 373 /* 375 374 * Find an unused context. 376 375 */ 377 376 ret = ipmmu_domain_allocate_context(domain->mmu, domain); 378 - if (ret == IPMMU_CTX_MAX) { 379 - free_io_pgtable_ops(domain->iop); 377 + if (ret == IPMMU_CTX_MAX) 380 378 return -EBUSY; 381 - } 382 379 383 380 domain->context_id = ret; 381 + 382 + domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg, 383 + domain); 384 + if (!domain->iop) { 385 + ipmmu_domain_free_context(domain->mmu, domain->context_id); 386 + return -EINVAL; 387 + } 384 388 385 389 /* TTBR0 */ 386 390 ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0]; ··· 420 424 ipmmu_ctx_write(domain, IMCTR, IMCTR_INTEN | IMCTR_FLUSH | IMCTR_MMUEN); 421 425 422 426 return 0; 423 - } 424 - 425 - static void ipmmu_domain_free_context(struct ipmmu_vmsa_device *mmu, 426 - unsigned int context_id) 427 - { 428 - unsigned long flags; 429 - 430 - spin_lock_irqsave(&mmu->lock, flags); 431 - 432 - clear_bit(context_id, mmu->ctx); 433 - mmu->domains[context_id] = NULL; 434 - 435 - spin_unlock_irqrestore(&mmu->lock, flags); 436 427 } 437 428 438 429 static void ipmmu_domain_destroy_context(struct ipmmu_vmsa_domain *domain) ··· 545 562 struct device *dev) 546 563 { 547 564 struct ipmmu_vmsa_iommu_priv *priv = to_priv(dev); 565 + struct iommu_fwspec *fwspec = dev->iommu_fwspec; 548 566 struct ipmmu_vmsa_device *mmu = priv->mmu; 549 567 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); 550 568 unsigned long flags; 551 569 unsigned int i; 552 570 int ret = 0; 553 571 554 - if (!mmu) { 572 + if (!priv || !priv->mmu) { 555 573 dev_err(dev, "Cannot attach to IPMMU\n"); 556 574 return -ENXIO; 557 575 } ··· 579 595 if (ret < 0) 580 596 return ret; 581 597 582 - for (i = 0; i < priv->num_utlbs; ++i) 583 - ipmmu_utlb_enable(domain, priv->utlbs[i]); 598 + for (i = 0; i < fwspec->num_ids; ++i) 599 + ipmmu_utlb_enable(domain, fwspec->ids[i]); 584 600 585 601 return 0; 586 602 } ··· 588 604 static void ipmmu_detach_device(struct iommu_domain *io_domain, 589 605 struct device *dev) 590 606 { 591 - struct ipmmu_vmsa_iommu_priv *priv = to_priv(dev); 607 + struct iommu_fwspec *fwspec = dev->iommu_fwspec; 592 608 struct ipmmu_vmsa_domain *domain = to_vmsa_domain(io_domain); 593 609 unsigned int i; 594 610 595 - for (i = 0; i < priv->num_utlbs; ++i) 596 - ipmmu_utlb_disable(domain, priv->utlbs[i]); 611 + for (i = 0; i < fwspec->num_ids; ++i) 612 + ipmmu_utlb_disable(domain, fwspec->ids[i]); 597 613 598 614 /* 599 615 * TODO: Optimize by disabling the context when no device is attached. ··· 629 645 return domain->iop->iova_to_phys(domain->iop, iova); 630 646 } 631 647 632 - static int ipmmu_find_utlbs(struct ipmmu_vmsa_device *mmu, struct device *dev, 633 - unsigned int *utlbs, unsigned int num_utlbs) 648 + static int ipmmu_init_platform_device(struct device *dev, 649 + struct of_phandle_args *args) 634 650 { 635 - unsigned int i; 651 + struct platform_device *ipmmu_pdev; 652 + struct ipmmu_vmsa_iommu_priv *priv; 636 653 637 - for (i = 0; i < num_utlbs; ++i) { 638 - struct of_phandle_args args; 639 - int ret; 654 + ipmmu_pdev = of_find_device_by_node(args->np); 655 + if (!ipmmu_pdev) 656 + return -ENODEV; 640 657 641 - ret = of_parse_phandle_with_args(dev->of_node, "iommus", 642 - "#iommu-cells", i, &args); 643 - if (ret < 0) 644 - return ret; 658 + priv = kzalloc(sizeof(*priv), GFP_KERNEL); 659 + if (!priv) 660 + return -ENOMEM; 645 661 646 - of_node_put(args.np); 647 - 648 - if (args.np != mmu->dev->of_node || args.args_count != 1) 649 - return -EINVAL; 650 - 651 - utlbs[i] = args.args[0]; 652 - } 653 - 662 + priv->mmu = platform_get_drvdata(ipmmu_pdev); 663 + priv->dev = dev; 664 + dev->iommu_fwspec->iommu_priv = priv; 654 665 return 0; 655 666 } 656 667 657 - static int ipmmu_init_platform_device(struct device *dev) 668 + static int ipmmu_of_xlate(struct device *dev, 669 + struct of_phandle_args *spec) 658 670 { 659 - struct ipmmu_vmsa_iommu_priv *priv; 660 - struct ipmmu_vmsa_device *mmu; 661 - unsigned int *utlbs; 662 - unsigned int i; 663 - int num_utlbs; 664 - int ret = -ENODEV; 671 + iommu_fwspec_add_ids(dev, spec->args, 1); 665 672 666 - /* Find the master corresponding to the device. */ 673 + /* Initialize once - xlate() will call multiple times */ 674 + if (to_priv(dev)) 675 + return 0; 667 676 668 - num_utlbs = of_count_phandle_with_args(dev->of_node, "iommus", 669 - "#iommu-cells"); 670 - if (num_utlbs < 0) 671 - return -ENODEV; 672 - 673 - utlbs = kcalloc(num_utlbs, sizeof(*utlbs), GFP_KERNEL); 674 - if (!utlbs) 675 - return -ENOMEM; 676 - 677 - spin_lock(&ipmmu_devices_lock); 678 - 679 - list_for_each_entry(mmu, &ipmmu_devices, list) { 680 - ret = ipmmu_find_utlbs(mmu, dev, utlbs, num_utlbs); 681 - if (!ret) { 682 - /* 683 - * TODO Take a reference to the MMU to protect 684 - * against device removal. 685 - */ 686 - break; 687 - } 688 - } 689 - 690 - spin_unlock(&ipmmu_devices_lock); 691 - 692 - if (ret < 0) 693 - goto error; 694 - 695 - for (i = 0; i < num_utlbs; ++i) { 696 - if (utlbs[i] >= mmu->num_utlbs) { 697 - ret = -EINVAL; 698 - goto error; 699 - } 700 - } 701 - 702 - priv = kzalloc(sizeof(*priv), GFP_KERNEL); 703 - if (!priv) { 704 - ret = -ENOMEM; 705 - goto error; 706 - } 707 - 708 - priv->mmu = mmu; 709 - priv->utlbs = utlbs; 710 - priv->num_utlbs = num_utlbs; 711 - priv->dev = dev; 712 - set_priv(dev, priv); 713 - return 0; 714 - 715 - error: 716 - kfree(utlbs); 717 - return ret; 677 + return ipmmu_init_platform_device(dev, spec); 718 678 } 719 679 720 680 #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA) ··· 677 749 struct iommu_group *group; 678 750 int ret; 679 751 680 - if (to_priv(dev)) { 681 - dev_warn(dev, "IOMMU driver already assigned to device %s\n", 682 - dev_name(dev)); 683 - return -EINVAL; 684 - } 752 + /* 753 + * Only let through devices that have been verified in xlate() 754 + */ 755 + if (!to_priv(dev)) 756 + return -ENODEV; 685 757 686 758 /* Create a device group and add the device to it. */ 687 759 group = iommu_group_alloc(); ··· 699 771 group = NULL; 700 772 goto error; 701 773 } 702 - 703 - ret = ipmmu_init_platform_device(dev); 704 - if (ret < 0) 705 - goto error; 706 774 707 775 /* 708 776 * Create the ARM mapping, used by the ARM DMA mapping core to allocate ··· 740 816 if (!IS_ERR_OR_NULL(group)) 741 817 iommu_group_remove_device(dev); 742 818 743 - kfree(to_priv(dev)->utlbs); 744 - kfree(to_priv(dev)); 745 - set_priv(dev, NULL); 746 - 747 819 return ret; 748 820 } 749 821 750 822 static void ipmmu_remove_device(struct device *dev) 751 823 { 752 - struct ipmmu_vmsa_iommu_priv *priv = to_priv(dev); 753 - 754 824 arm_iommu_detach_device(dev); 755 825 iommu_group_remove_device(dev); 756 - 757 - kfree(priv->utlbs); 758 - kfree(priv); 759 - 760 - set_priv(dev, NULL); 761 826 } 762 827 763 828 static const struct iommu_ops ipmmu_ops = { ··· 761 848 .add_device = ipmmu_add_device, 762 849 .remove_device = ipmmu_remove_device, 763 850 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K, 851 + .of_xlate = ipmmu_of_xlate, 764 852 }; 765 853 766 854 #endif /* !CONFIG_ARM && CONFIG_IOMMU_DMA */ ··· 804 890 805 891 static int ipmmu_add_device_dma(struct device *dev) 806 892 { 807 - struct iommu_fwspec *fwspec = dev->iommu_fwspec; 808 893 struct iommu_group *group; 809 894 810 895 /* 811 896 * Only let through devices that have been verified in xlate() 812 - * We may get called with dev->iommu_fwspec set to NULL. 813 897 */ 814 - if (!fwspec || !fwspec->iommu_priv) 898 + if (!to_priv(dev)) 815 899 return -ENODEV; 816 900 817 901 group = iommu_group_get_for_dev(dev); ··· 869 957 return group; 870 958 } 871 959 872 - static int ipmmu_of_xlate_dma(struct device *dev, 873 - struct of_phandle_args *spec) 874 - { 875 - /* If the IPMMU device is disabled in DT then return error 876 - * to make sure the of_iommu code does not install ops 877 - * even though the iommu device is disabled 878 - */ 879 - if (!of_device_is_available(spec->np)) 880 - return -ENODEV; 881 - 882 - return ipmmu_init_platform_device(dev); 883 - } 884 - 885 960 static const struct iommu_ops ipmmu_ops = { 886 961 .domain_alloc = ipmmu_domain_alloc_dma, 887 962 .domain_free = ipmmu_domain_free_dma, ··· 882 983 .remove_device = ipmmu_remove_device_dma, 883 984 .device_group = ipmmu_find_group_dma, 884 985 .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K, 885 - .of_xlate = ipmmu_of_xlate_dma, 986 + .of_xlate = ipmmu_of_xlate, 886 987 }; 887 988 888 989 #endif /* CONFIG_IOMMU_DMA */ ··· 953 1054 954 1055 ipmmu_device_reset(mmu); 955 1056 1057 + ret = iommu_device_sysfs_add(&mmu->iommu, &pdev->dev, NULL, 1058 + dev_name(&pdev->dev)); 1059 + if (ret) 1060 + return ret; 1061 + 1062 + iommu_device_set_ops(&mmu->iommu, &ipmmu_ops); 1063 + iommu_device_set_fwnode(&mmu->iommu, &pdev->dev.of_node->fwnode); 1064 + 1065 + ret = iommu_device_register(&mmu->iommu); 1066 + if (ret) 1067 + return ret; 1068 + 956 1069 /* 957 1070 * We can't create the ARM mapping here as it requires the bus to have 958 1071 * an IOMMU, which only happens when bus_set_iommu() is called in 959 1072 * ipmmu_init() after the probe function returns. 960 1073 */ 961 - 962 - spin_lock(&ipmmu_devices_lock); 963 - list_add(&mmu->list, &ipmmu_devices); 964 - spin_unlock(&ipmmu_devices_lock); 965 1074 966 1075 platform_set_drvdata(pdev, mmu); 967 1076 ··· 980 1073 { 981 1074 struct ipmmu_vmsa_device *mmu = platform_get_drvdata(pdev); 982 1075 983 - spin_lock(&ipmmu_devices_lock); 984 - list_del(&mmu->list); 985 - spin_unlock(&ipmmu_devices_lock); 1076 + iommu_device_sysfs_remove(&mmu->iommu); 1077 + iommu_device_unregister(&mmu->iommu); 986 1078 987 1079 #if defined(CONFIG_ARM) && !defined(CONFIG_IOMMU_DMA) 988 1080 arm_iommu_release_mapping(mmu->mapping);
+14 -1
drivers/iommu/msm_iommu.c
··· 393 393 static int msm_iommu_add_device(struct device *dev) 394 394 { 395 395 struct msm_iommu_dev *iommu; 396 + struct iommu_group *group; 396 397 unsigned long flags; 397 398 int ret = 0; 398 399 ··· 407 406 408 407 spin_unlock_irqrestore(&msm_iommu_lock, flags); 409 408 410 - return ret; 409 + if (ret) 410 + return ret; 411 + 412 + group = iommu_group_get_for_dev(dev); 413 + if (IS_ERR(group)) 414 + return PTR_ERR(group); 415 + 416 + iommu_group_put(group); 417 + 418 + return 0; 411 419 } 412 420 413 421 static void msm_iommu_remove_device(struct device *dev) ··· 431 421 iommu_device_unlink(&iommu->iommu, dev); 432 422 433 423 spin_unlock_irqrestore(&msm_iommu_lock, flags); 424 + 425 + iommu_group_remove_device(dev); 434 426 } 435 427 436 428 static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) ··· 712 700 .iova_to_phys = msm_iommu_iova_to_phys, 713 701 .add_device = msm_iommu_add_device, 714 702 .remove_device = msm_iommu_remove_device, 703 + .device_group = generic_device_group, 715 704 .pgsize_bitmap = MSM_IOMMU_PGSIZES, 716 705 .of_xlate = qcom_iommu_of_xlate, 717 706 };
+136 -78
drivers/iommu/mtk_iommu.c
··· 31 31 #include <linux/slab.h> 32 32 #include <linux/spinlock.h> 33 33 #include <asm/barrier.h> 34 - #include <dt-bindings/memory/mt8173-larb-port.h> 35 34 #include <soc/mediatek/smi.h> 36 35 37 36 #include "mtk_iommu.h" ··· 53 54 54 55 #define REG_MMU_CTRL_REG 0x110 55 56 #define F_MMU_PREFETCH_RT_REPLACE_MOD BIT(4) 56 - #define F_MMU_TF_PROTECT_SEL(prot) (((prot) & 0x3) << 5) 57 + #define F_MMU_TF_PROTECT_SEL_SHIFT(data) \ 58 + ((data)->m4u_plat == M4U_MT2712 ? 4 : 5) 59 + /* It's named by F_MMU_TF_PROT_SEL in mt2712. */ 60 + #define F_MMU_TF_PROTECT_SEL(prot, data) \ 61 + (((prot) & 0x3) << F_MMU_TF_PROTECT_SEL_SHIFT(data)) 57 62 58 63 #define REG_MMU_IVRP_PADDR 0x114 59 64 #define F_MMU_IVRP_PA_SET(pa, ext) (((pa) >> 1) | ((!!(ext)) << 31)) 65 + #define REG_MMU_VLD_PA_RNG 0x118 66 + #define F_MMU_VLD_PA_RNG(EA, SA) (((EA) << 8) | (SA)) 60 67 61 68 #define REG_MMU_INT_CONTROL0 0x120 62 69 #define F_L2_MULIT_HIT_EN BIT(0) ··· 87 82 #define REG_MMU_FAULT_ST1 0x134 88 83 89 84 #define REG_MMU_FAULT_VA 0x13c 90 - #define F_MMU_FAULT_VA_MSK 0xfffff000 91 85 #define F_MMU_FAULT_VA_WRITE_BIT BIT(1) 92 86 #define F_MMU_FAULT_VA_LAYER_BIT BIT(0) 93 87 ··· 96 92 #define F_MMU0_INT_ID_PORT_ID(a) (((a) >> 2) & 0x1f) 97 93 98 94 #define MTK_PROTECT_PA_ALIGN 128 95 + 96 + /* 97 + * Get the local arbiter ID and the portid within the larb arbiter 98 + * from mtk_m4u_id which is defined by MTK_M4U_ID. 99 + */ 100 + #define MTK_M4U_TO_LARB(id) (((id) >> 5) & 0xf) 101 + #define MTK_M4U_TO_PORT(id) ((id) & 0x1f) 99 102 100 103 struct mtk_iommu_domain { 101 104 spinlock_t pgtlock; /* lock for page table */ ··· 115 104 116 105 static struct iommu_ops mtk_iommu_ops; 117 106 107 + static LIST_HEAD(m4ulist); /* List all the M4U HWs */ 108 + 109 + #define for_each_m4u(data) list_for_each_entry(data, &m4ulist, list) 110 + 111 + /* 112 + * There may be 1 or 2 M4U HWs, But we always expect they are in the same domain 113 + * for the performance. 114 + * 115 + * Here always return the mtk_iommu_data of the first probed M4U where the 116 + * iommu domain information is recorded. 117 + */ 118 + static struct mtk_iommu_data *mtk_iommu_get_m4u_data(void) 119 + { 120 + struct mtk_iommu_data *data; 121 + 122 + for_each_m4u(data) 123 + return data; 124 + 125 + return NULL; 126 + } 127 + 118 128 static struct mtk_iommu_domain *to_mtk_domain(struct iommu_domain *dom) 119 129 { 120 130 return container_of(dom, struct mtk_iommu_domain, domain); ··· 145 113 { 146 114 struct mtk_iommu_data *data = cookie; 147 115 148 - writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, data->base + REG_MMU_INV_SEL); 149 - writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE); 150 - wmb(); /* Make sure the tlb flush all done */ 116 + for_each_m4u(data) { 117 + writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, 118 + data->base + REG_MMU_INV_SEL); 119 + writel_relaxed(F_ALL_INVLD, data->base + REG_MMU_INVALIDATE); 120 + wmb(); /* Make sure the tlb flush all done */ 121 + } 151 122 } 152 123 153 124 static void mtk_iommu_tlb_add_flush_nosync(unsigned long iova, size_t size, ··· 159 124 { 160 125 struct mtk_iommu_data *data = cookie; 161 126 162 - writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, data->base + REG_MMU_INV_SEL); 127 + for_each_m4u(data) { 128 + writel_relaxed(F_INVLD_EN1 | F_INVLD_EN0, 129 + data->base + REG_MMU_INV_SEL); 163 130 164 - writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A); 165 - writel_relaxed(iova + size - 1, data->base + REG_MMU_INVLD_END_A); 166 - writel_relaxed(F_MMU_INV_RANGE, data->base + REG_MMU_INVALIDATE); 167 - data->tlb_flush_active = true; 131 + writel_relaxed(iova, data->base + REG_MMU_INVLD_START_A); 132 + writel_relaxed(iova + size - 1, 133 + data->base + REG_MMU_INVLD_END_A); 134 + writel_relaxed(F_MMU_INV_RANGE, 135 + data->base + REG_MMU_INVALIDATE); 136 + data->tlb_flush_active = true; 137 + } 168 138 } 169 139 170 140 static void mtk_iommu_tlb_sync(void *cookie) ··· 178 138 int ret; 179 139 u32 tmp; 180 140 181 - /* Avoid timing out if there's nothing to wait for */ 182 - if (!data->tlb_flush_active) 183 - return; 141 + for_each_m4u(data) { 142 + /* Avoid timing out if there's nothing to wait for */ 143 + if (!data->tlb_flush_active) 144 + return; 184 145 185 - ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE, tmp, 186 - tmp != 0, 10, 100000); 187 - if (ret) { 188 - dev_warn(data->dev, 189 - "Partial TLB flush timed out, falling back to full flush\n"); 190 - mtk_iommu_tlb_flush_all(cookie); 146 + ret = readl_poll_timeout_atomic(data->base + REG_MMU_CPE_DONE, 147 + tmp, tmp != 0, 10, 100000); 148 + if (ret) { 149 + dev_warn(data->dev, 150 + "Partial TLB flush timed out, falling back to full flush\n"); 151 + mtk_iommu_tlb_flush_all(cookie); 152 + } 153 + /* Clear the CPE status */ 154 + writel_relaxed(0, data->base + REG_MMU_CPE_DONE); 155 + data->tlb_flush_active = false; 191 156 } 192 - /* Clear the CPE status */ 193 - writel_relaxed(0, data->base + REG_MMU_CPE_DONE); 194 - data->tlb_flush_active = false; 195 157 } 196 158 197 159 static const struct iommu_gather_ops mtk_iommu_gather_ops = { ··· 215 173 fault_iova = readl_relaxed(data->base + REG_MMU_FAULT_VA); 216 174 layer = fault_iova & F_MMU_FAULT_VA_LAYER_BIT; 217 175 write = fault_iova & F_MMU_FAULT_VA_WRITE_BIT; 218 - fault_iova &= F_MMU_FAULT_VA_MSK; 219 176 fault_pa = readl_relaxed(data->base + REG_MMU_INVLD_PA); 220 177 regval = readl_relaxed(data->base + REG_MMU_INT_ID); 221 178 fault_larb = F_MMU0_INT_ID_LARB_ID(regval); ··· 262 221 } 263 222 } 264 223 265 - static int mtk_iommu_domain_finalise(struct mtk_iommu_data *data) 224 + static int mtk_iommu_domain_finalise(struct mtk_iommu_domain *dom) 266 225 { 267 - struct mtk_iommu_domain *dom = data->m4u_dom; 226 + struct mtk_iommu_data *data = mtk_iommu_get_m4u_data(); 268 227 269 228 spin_lock_init(&dom->pgtlock); 270 229 ··· 290 249 291 250 /* Update our support page sizes bitmap */ 292 251 dom->domain.pgsize_bitmap = dom->cfg.pgsize_bitmap; 293 - 294 - writel(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0], 295 - data->base + REG_MMU_PT_BASE_ADDR); 296 252 return 0; 297 253 } 298 254 ··· 304 266 if (!dom) 305 267 return NULL; 306 268 307 - if (iommu_get_dma_cookie(&dom->domain)) { 308 - kfree(dom); 309 - return NULL; 310 - } 269 + if (iommu_get_dma_cookie(&dom->domain)) 270 + goto free_dom; 271 + 272 + if (mtk_iommu_domain_finalise(dom)) 273 + goto put_dma_cookie; 311 274 312 275 dom->domain.geometry.aperture_start = 0; 313 276 dom->domain.geometry.aperture_end = DMA_BIT_MASK(32); 314 277 dom->domain.geometry.force_aperture = true; 315 278 316 279 return &dom->domain; 280 + 281 + put_dma_cookie: 282 + iommu_put_dma_cookie(&dom->domain); 283 + free_dom: 284 + kfree(dom); 285 + return NULL; 317 286 } 318 287 319 288 static void mtk_iommu_domain_free(struct iommu_domain *domain) 320 289 { 290 + struct mtk_iommu_domain *dom = to_mtk_domain(domain); 291 + 292 + free_io_pgtable_ops(dom->iop); 321 293 iommu_put_dma_cookie(domain); 322 294 kfree(to_mtk_domain(domain)); 323 295 } ··· 337 289 { 338 290 struct mtk_iommu_domain *dom = to_mtk_domain(domain); 339 291 struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv; 340 - int ret; 341 292 342 293 if (!data) 343 294 return -ENODEV; 344 295 296 + /* Update the pgtable base address register of the M4U HW */ 345 297 if (!data->m4u_dom) { 346 298 data->m4u_dom = dom; 347 - ret = mtk_iommu_domain_finalise(data); 348 - if (ret) { 349 - data->m4u_dom = NULL; 350 - return ret; 351 - } 352 - } else if (data->m4u_dom != dom) { 353 - /* All the client devices should be in the same m4u domain */ 354 - dev_err(dev, "try to attach into the error iommu domain\n"); 355 - return -EPERM; 299 + writel(dom->cfg.arm_v7s_cfg.ttbr[0], 300 + data->base + REG_MMU_PT_BASE_ADDR); 356 301 } 357 302 358 303 mtk_iommu_config(data, dev, true); ··· 395 354 dma_addr_t iova) 396 355 { 397 356 struct mtk_iommu_domain *dom = to_mtk_domain(domain); 357 + struct mtk_iommu_data *data = mtk_iommu_get_m4u_data(); 398 358 unsigned long flags; 399 359 phys_addr_t pa; 400 360 401 361 spin_lock_irqsave(&dom->pgtlock, flags); 402 362 pa = dom->iop->iova_to_phys(dom->iop, iova); 403 363 spin_unlock_irqrestore(&dom->pgtlock, flags); 364 + 365 + if (data->enable_4GB) 366 + pa |= BIT_ULL(32); 404 367 405 368 return pa; 406 369 } ··· 444 399 445 400 static struct iommu_group *mtk_iommu_device_group(struct device *dev) 446 401 { 447 - struct mtk_iommu_data *data = dev->iommu_fwspec->iommu_priv; 402 + struct mtk_iommu_data *data = mtk_iommu_get_m4u_data(); 448 403 449 404 if (!data) 450 405 return ERR_PTR(-ENODEV); ··· 509 464 return ret; 510 465 } 511 466 512 - regval = F_MMU_PREFETCH_RT_REPLACE_MOD | 513 - F_MMU_TF_PROTECT_SEL(2); 467 + regval = F_MMU_TF_PROTECT_SEL(2, data); 468 + if (data->m4u_plat == M4U_MT8173) 469 + regval |= F_MMU_PREFETCH_RT_REPLACE_MOD; 514 470 writel_relaxed(regval, data->base + REG_MMU_CTRL_REG); 515 471 516 472 regval = F_L2_MULIT_HIT_EN | ··· 533 487 534 488 writel_relaxed(F_MMU_IVRP_PA_SET(data->protect_base, data->enable_4GB), 535 489 data->base + REG_MMU_IVRP_PADDR); 536 - 490 + if (data->enable_4GB && data->m4u_plat != M4U_MT8173) { 491 + /* 492 + * If 4GB mode is enabled, the validate PA range is from 493 + * 0x1_0000_0000 to 0x1_ffff_ffff. here record bit[32:30]. 494 + */ 495 + regval = F_MMU_VLD_PA_RNG(7, 4); 496 + writel_relaxed(regval, data->base + REG_MMU_VLD_PA_RNG); 497 + } 537 498 writel_relaxed(0, data->base + REG_MMU_DCM_DIS); 538 - writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE); 499 + 500 + /* It's MISC control register whose default value is ok except mt8173.*/ 501 + if (data->m4u_plat == M4U_MT8173) 502 + writel_relaxed(0, data->base + REG_MMU_STANDARD_AXI_MODE); 539 503 540 504 if (devm_request_irq(data->dev, data->irq, mtk_iommu_isr, 0, 541 505 dev_name(data->dev), (void *)data)) { ··· 577 521 if (!data) 578 522 return -ENOMEM; 579 523 data->dev = dev; 524 + data->m4u_plat = (enum mtk_iommu_plat)of_device_get_match_data(dev); 580 525 581 526 /* Protect memory. HW will access here while translation fault.*/ 582 527 protect = devm_kzalloc(dev, MTK_PROTECT_PA_ALIGN * 2, GFP_KERNEL); ··· 586 529 data->protect_base = ALIGN(virt_to_phys(protect), MTK_PROTECT_PA_ALIGN); 587 530 588 531 /* Whether the current dram is over 4GB */ 589 - data->enable_4GB = !!(max_pfn > (0xffffffffUL >> PAGE_SHIFT)); 532 + data->enable_4GB = !!(max_pfn > (BIT_ULL(32) >> PAGE_SHIFT)); 590 533 591 534 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 592 535 data->base = devm_ioremap_resource(dev, res); ··· 611 554 for (i = 0; i < larb_nr; i++) { 612 555 struct device_node *larbnode; 613 556 struct platform_device *plarbdev; 557 + u32 id; 614 558 615 559 larbnode = of_parse_phandle(dev->of_node, "mediatek,larbs", i); 616 560 if (!larbnode) ··· 620 562 if (!of_device_is_available(larbnode)) 621 563 continue; 622 564 565 + ret = of_property_read_u32(larbnode, "mediatek,larb-id", &id); 566 + if (ret)/* The id is consecutive if there is no this property */ 567 + id = i; 568 + 623 569 plarbdev = of_find_device_by_node(larbnode); 624 - if (!plarbdev) { 625 - plarbdev = of_platform_device_create( 626 - larbnode, NULL, 627 - platform_bus_type.dev_root); 628 - if (!plarbdev) { 629 - of_node_put(larbnode); 630 - return -EPROBE_DEFER; 631 - } 632 - } 633 - data->smi_imu.larb_imu[i].dev = &plarbdev->dev; 570 + if (!plarbdev) 571 + return -EPROBE_DEFER; 572 + data->smi_imu.larb_imu[id].dev = &plarbdev->dev; 634 573 635 574 component_match_add_release(dev, &match, release_of, 636 575 compare_of, larbnode); ··· 651 596 if (ret) 652 597 return ret; 653 598 599 + list_add_tail(&data->list, &m4ulist); 600 + 654 601 if (!iommu_present(&platform_bus_type)) 655 602 bus_set_iommu(&platform_bus_type, &mtk_iommu_ops); 656 603 ··· 669 612 if (iommu_present(&platform_bus_type)) 670 613 bus_set_iommu(&platform_bus_type, NULL); 671 614 672 - free_io_pgtable_ops(data->m4u_dom->iop); 673 615 clk_disable_unprepare(data->bclk); 674 616 devm_free_irq(&pdev->dev, data->irq, data); 675 617 component_master_del(&pdev->dev, &mtk_iommu_com_ops); ··· 687 631 reg->ctrl_reg = readl_relaxed(base + REG_MMU_CTRL_REG); 688 632 reg->int_control0 = readl_relaxed(base + REG_MMU_INT_CONTROL0); 689 633 reg->int_main_control = readl_relaxed(base + REG_MMU_INT_MAIN_CONTROL); 634 + clk_disable_unprepare(data->bclk); 690 635 return 0; 691 636 } 692 637 ··· 696 639 struct mtk_iommu_data *data = dev_get_drvdata(dev); 697 640 struct mtk_iommu_suspend_reg *reg = &data->reg; 698 641 void __iomem *base = data->base; 642 + int ret; 699 643 700 - writel_relaxed(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0], 701 - base + REG_MMU_PT_BASE_ADDR); 644 + ret = clk_prepare_enable(data->bclk); 645 + if (ret) { 646 + dev_err(data->dev, "Failed to enable clk(%d) in resume\n", ret); 647 + return ret; 648 + } 702 649 writel_relaxed(reg->standard_axi_mode, 703 650 base + REG_MMU_STANDARD_AXI_MODE); 704 651 writel_relaxed(reg->dcm_dis, base + REG_MMU_DCM_DIS); ··· 711 650 writel_relaxed(reg->int_main_control, base + REG_MMU_INT_MAIN_CONTROL); 712 651 writel_relaxed(F_MMU_IVRP_PA_SET(data->protect_base, data->enable_4GB), 713 652 base + REG_MMU_IVRP_PADDR); 653 + if (data->m4u_dom) 654 + writel(data->m4u_dom->cfg.arm_v7s_cfg.ttbr[0], 655 + base + REG_MMU_PT_BASE_ADDR); 714 656 return 0; 715 657 } 716 658 717 - const struct dev_pm_ops mtk_iommu_pm_ops = { 718 - SET_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume) 659 + static const struct dev_pm_ops mtk_iommu_pm_ops = { 660 + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_iommu_suspend, mtk_iommu_resume) 719 661 }; 720 662 721 663 static const struct of_device_id mtk_iommu_of_ids[] = { 722 - { .compatible = "mediatek,mt8173-m4u", }, 664 + { .compatible = "mediatek,mt2712-m4u", .data = (void *)M4U_MT2712}, 665 + { .compatible = "mediatek,mt8173-m4u", .data = (void *)M4U_MT8173}, 723 666 {} 724 667 }; 725 668 ··· 732 667 .remove = mtk_iommu_remove, 733 668 .driver = { 734 669 .name = "mtk-iommu", 735 - .of_match_table = mtk_iommu_of_ids, 670 + .of_match_table = of_match_ptr(mtk_iommu_of_ids), 736 671 .pm = &mtk_iommu_pm_ops, 737 672 } 738 673 }; 739 674 740 - static int mtk_iommu_init_fn(struct device_node *np) 675 + static int __init mtk_iommu_init(void) 741 676 { 742 677 int ret; 743 - struct platform_device *pdev; 744 - 745 - pdev = of_platform_device_create(np, NULL, platform_bus_type.dev_root); 746 - if (!pdev) 747 - return -ENOMEM; 748 678 749 679 ret = platform_driver_register(&mtk_iommu_driver); 750 - if (ret) { 751 - pr_err("%s: Failed to register driver\n", __func__); 752 - return ret; 753 - } 680 + if (ret != 0) 681 + pr_err("Failed to register MTK IOMMU driver\n"); 754 682 755 - return 0; 683 + return ret; 756 684 } 757 685 758 - IOMMU_OF_DECLARE(mtkm4u, "mediatek,mt8173-m4u", mtk_iommu_init_fn); 686 + subsys_initcall(mtk_iommu_init)
+9
drivers/iommu/mtk_iommu.h
··· 34 34 u32 int_main_control; 35 35 }; 36 36 37 + enum mtk_iommu_plat { 38 + M4U_MT2701, 39 + M4U_MT2712, 40 + M4U_MT8173, 41 + }; 42 + 37 43 struct mtk_iommu_domain; 38 44 39 45 struct mtk_iommu_data { ··· 56 50 bool tlb_flush_active; 57 51 58 52 struct iommu_device iommu; 53 + enum mtk_iommu_plat m4u_plat; 54 + 55 + struct list_head list; 59 56 }; 60 57 61 58 static inline int compare_of(struct device *dev, void *data)
+66 -78
drivers/iommu/of_iommu.c
··· 25 25 #include <linux/of_pci.h> 26 26 #include <linux/slab.h> 27 27 28 + #define NO_IOMMU 1 29 + 28 30 static const struct of_device_id __iommu_of_table_sentinel 29 31 __used __section(__iommu_of_table_end); 30 32 ··· 111 109 return of_match_node(&__iommu_of_table, np); 112 110 } 113 111 114 - static const struct iommu_ops 115 - *of_iommu_xlate(struct device *dev, struct of_phandle_args *iommu_spec) 112 + static int of_iommu_xlate(struct device *dev, 113 + struct of_phandle_args *iommu_spec) 116 114 { 117 115 const struct iommu_ops *ops; 118 116 struct fwnode_handle *fwnode = &iommu_spec->np->fwnode; ··· 122 120 if ((ops && !ops->of_xlate) || 123 121 !of_device_is_available(iommu_spec->np) || 124 122 (!ops && !of_iommu_driver_present(iommu_spec->np))) 125 - return NULL; 123 + return NO_IOMMU; 126 124 127 125 err = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops); 128 126 if (err) 129 - return ERR_PTR(err); 127 + return err; 130 128 /* 131 129 * The otherwise-empty fwspec handily serves to indicate the specific 132 130 * IOMMU device we're waiting for, which will be useful if we ever get 133 131 * a proper probe-ordering dependency mechanism in future. 134 132 */ 135 133 if (!ops) 136 - return ERR_PTR(-EPROBE_DEFER); 134 + return -EPROBE_DEFER; 137 135 138 - err = ops->of_xlate(dev, iommu_spec); 139 - if (err) 140 - return ERR_PTR(err); 141 - 142 - return ops; 136 + return ops->of_xlate(dev, iommu_spec); 143 137 } 144 138 145 - static int __get_pci_rid(struct pci_dev *pdev, u16 alias, void *data) 146 - { 147 - struct of_phandle_args *iommu_spec = data; 139 + struct of_pci_iommu_alias_info { 140 + struct device *dev; 141 + struct device_node *np; 142 + }; 148 143 149 - iommu_spec->args[0] = alias; 150 - return iommu_spec->np == pdev->bus->dev.of_node; 151 - } 152 - 153 - static const struct iommu_ops 154 - *of_pci_iommu_init(struct pci_dev *pdev, struct device_node *bridge_np) 144 + static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) 155 145 { 156 - const struct iommu_ops *ops; 157 - struct of_phandle_args iommu_spec; 146 + struct of_pci_iommu_alias_info *info = data; 147 + struct of_phandle_args iommu_spec = { .args_count = 1 }; 158 148 int err; 159 149 160 - /* 161 - * Start by tracing the RID alias down the PCI topology as 162 - * far as the host bridge whose OF node we have... 163 - * (we're not even attempting to handle multi-alias devices yet) 164 - */ 165 - iommu_spec.args_count = 1; 166 - iommu_spec.np = bridge_np; 167 - pci_for_each_dma_alias(pdev, __get_pci_rid, &iommu_spec); 168 - /* 169 - * ...then find out what that becomes once it escapes the PCI 170 - * bus into the system beyond, and which IOMMU it ends up at. 171 - */ 172 - iommu_spec.np = NULL; 173 - err = of_pci_map_rid(bridge_np, iommu_spec.args[0], "iommu-map", 150 + err = of_pci_map_rid(info->np, alias, "iommu-map", 174 151 "iommu-map-mask", &iommu_spec.np, 175 152 iommu_spec.args); 176 153 if (err) 177 - return err == -ENODEV ? NULL : ERR_PTR(err); 154 + return err == -ENODEV ? NO_IOMMU : err; 178 155 179 - ops = of_iommu_xlate(&pdev->dev, &iommu_spec); 180 - 156 + err = of_iommu_xlate(info->dev, &iommu_spec); 181 157 of_node_put(iommu_spec.np); 182 - return ops; 183 - } 158 + if (err) 159 + return err; 184 160 185 - static const struct iommu_ops 186 - *of_platform_iommu_init(struct device *dev, struct device_node *np) 187 - { 188 - struct of_phandle_args iommu_spec; 189 - const struct iommu_ops *ops = NULL; 190 - int idx = 0; 191 - 192 - /* 193 - * We don't currently walk up the tree looking for a parent IOMMU. 194 - * See the `Notes:' section of 195 - * Documentation/devicetree/bindings/iommu/iommu.txt 196 - */ 197 - while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", 198 - idx, &iommu_spec)) { 199 - ops = of_iommu_xlate(dev, &iommu_spec); 200 - of_node_put(iommu_spec.np); 201 - idx++; 202 - if (IS_ERR_OR_NULL(ops)) 203 - break; 204 - } 205 - 206 - return ops; 161 + return info->np == pdev->bus->dev.of_node; 207 162 } 208 163 209 164 const struct iommu_ops *of_iommu_configure(struct device *dev, 210 165 struct device_node *master_np) 211 166 { 212 - const struct iommu_ops *ops; 167 + const struct iommu_ops *ops = NULL; 213 168 struct iommu_fwspec *fwspec = dev->iommu_fwspec; 169 + int err = NO_IOMMU; 214 170 215 171 if (!master_np) 216 172 return NULL; ··· 181 221 iommu_fwspec_free(dev); 182 222 } 183 223 184 - if (dev_is_pci(dev)) 185 - ops = of_pci_iommu_init(to_pci_dev(dev), master_np); 186 - else 187 - ops = of_platform_iommu_init(dev, master_np); 224 + /* 225 + * We don't currently walk up the tree looking for a parent IOMMU. 226 + * See the `Notes:' section of 227 + * Documentation/devicetree/bindings/iommu/iommu.txt 228 + */ 229 + if (dev_is_pci(dev)) { 230 + struct of_pci_iommu_alias_info info = { 231 + .dev = dev, 232 + .np = master_np, 233 + }; 234 + 235 + err = pci_for_each_dma_alias(to_pci_dev(dev), 236 + of_pci_iommu_init, &info); 237 + } else { 238 + struct of_phandle_args iommu_spec; 239 + int idx = 0; 240 + 241 + while (!of_parse_phandle_with_args(master_np, "iommus", 242 + "#iommu-cells", 243 + idx, &iommu_spec)) { 244 + err = of_iommu_xlate(dev, &iommu_spec); 245 + of_node_put(iommu_spec.np); 246 + idx++; 247 + if (err) 248 + break; 249 + } 250 + } 251 + 252 + /* 253 + * Two success conditions can be represented by non-negative err here: 254 + * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons 255 + * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately 256 + * <0 : any actual error 257 + */ 258 + if (!err) 259 + ops = dev->iommu_fwspec->ops; 188 260 /* 189 261 * If we have reason to believe the IOMMU driver missed the initial 190 262 * add_device callback for dev, replay it to get things in order. 191 263 */ 192 - if (!IS_ERR_OR_NULL(ops) && ops->add_device && 193 - dev->bus && !dev->iommu_group) { 194 - int err = ops->add_device(dev); 195 - 196 - if (err) 197 - ops = ERR_PTR(err); 198 - } 264 + if (ops && ops->add_device && dev->bus && !dev->iommu_group) 265 + err = ops->add_device(dev); 199 266 200 267 /* Ignore all other errors apart from EPROBE_DEFER */ 201 - if (IS_ERR(ops) && (PTR_ERR(ops) != -EPROBE_DEFER)) { 202 - dev_dbg(dev, "Adding to IOMMU failed: %ld\n", PTR_ERR(ops)); 268 + if (err == -EPROBE_DEFER) { 269 + ops = ERR_PTR(err); 270 + } else if (err < 0) { 271 + dev_dbg(dev, "Adding to IOMMU failed: %d\n", err); 203 272 ops = NULL; 204 273 } 205 274 ··· 244 255 const of_iommu_init_fn init_fn = match->data; 245 256 246 257 if (init_fn && init_fn(np)) 247 - pr_err("Failed to initialise IOMMU %s\n", 248 - of_node_full_name(np)); 258 + pr_err("Failed to initialise IOMMU %pOF\n", np); 249 259 } 250 260 251 261 return 0;
+81 -46
drivers/iommu/omap-iommu.c
··· 11 11 * published by the Free Software Foundation. 12 12 */ 13 13 14 + #include <linux/dma-mapping.h> 14 15 #include <linux/err.h> 15 16 #include <linux/slab.h> 16 17 #include <linux/interrupt.h> ··· 29 28 #include <linux/of_platform.h> 30 29 #include <linux/regmap.h> 31 30 #include <linux/mfd/syscon.h> 32 - 33 - #include <asm/cacheflush.h> 34 31 35 32 #include <linux/platform_data/iommu-omap.h> 36 33 ··· 453 454 /* 454 455 * H/W pagetable operations 455 456 */ 456 - static void flush_iopgd_range(u32 *first, u32 *last) 457 + static void flush_iopte_range(struct device *dev, dma_addr_t dma, 458 + unsigned long offset, int num_entries) 457 459 { 458 - /* FIXME: L2 cache should be taken care of if it exists */ 459 - do { 460 - asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pgd" 461 - : : "r" (first)); 462 - first += L1_CACHE_BYTES / sizeof(*first); 463 - } while (first <= last); 460 + size_t size = num_entries * sizeof(u32); 461 + 462 + dma_sync_single_range_for_device(dev, dma, offset, size, DMA_TO_DEVICE); 464 463 } 465 464 466 - static void flush_iopte_range(u32 *first, u32 *last) 465 + static void iopte_free(struct omap_iommu *obj, u32 *iopte, bool dma_valid) 467 466 { 468 - /* FIXME: L2 cache should be taken care of if it exists */ 469 - do { 470 - asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pte" 471 - : : "r" (first)); 472 - first += L1_CACHE_BYTES / sizeof(*first); 473 - } while (first <= last); 474 - } 467 + dma_addr_t pt_dma; 475 468 476 - static void iopte_free(u32 *iopte) 477 - { 478 469 /* Note: freed iopte's must be clean ready for re-use */ 479 - if (iopte) 470 + if (iopte) { 471 + if (dma_valid) { 472 + pt_dma = virt_to_phys(iopte); 473 + dma_unmap_single(obj->dev, pt_dma, IOPTE_TABLE_SIZE, 474 + DMA_TO_DEVICE); 475 + } 476 + 480 477 kmem_cache_free(iopte_cachep, iopte); 478 + } 481 479 } 482 480 483 - static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da) 481 + static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, 482 + dma_addr_t *pt_dma, u32 da) 484 483 { 485 484 u32 *iopte; 485 + unsigned long offset = iopgd_index(da) * sizeof(da); 486 486 487 487 /* a table has already existed */ 488 488 if (*iopgd) ··· 498 500 if (!iopte) 499 501 return ERR_PTR(-ENOMEM); 500 502 501 - *iopgd = virt_to_phys(iopte) | IOPGD_TABLE; 502 - flush_iopgd_range(iopgd, iopgd); 503 + *pt_dma = dma_map_single(obj->dev, iopte, IOPTE_TABLE_SIZE, 504 + DMA_TO_DEVICE); 505 + if (dma_mapping_error(obj->dev, *pt_dma)) { 506 + dev_err(obj->dev, "DMA map error for L2 table\n"); 507 + iopte_free(obj, iopte, false); 508 + return ERR_PTR(-ENOMEM); 509 + } 503 510 511 + /* 512 + * we rely on dma address and the physical address to be 513 + * the same for mapping the L2 table 514 + */ 515 + if (WARN_ON(*pt_dma != virt_to_phys(iopte))) { 516 + dev_err(obj->dev, "DMA translation error for L2 table\n"); 517 + dma_unmap_single(obj->dev, *pt_dma, IOPTE_TABLE_SIZE, 518 + DMA_TO_DEVICE); 519 + iopte_free(obj, iopte, false); 520 + return ERR_PTR(-ENOMEM); 521 + } 522 + 523 + *iopgd = virt_to_phys(iopte) | IOPGD_TABLE; 524 + 525 + flush_iopte_range(obj->dev, obj->pd_dma, offset, 1); 504 526 dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte); 505 527 } else { 506 528 /* We raced, free the reduniovant table */ 507 - iopte_free(iopte); 529 + iopte_free(obj, iopte, false); 508 530 } 509 531 510 532 pte_ready: 511 533 iopte = iopte_offset(iopgd, da); 512 - 534 + *pt_dma = virt_to_phys(iopte); 513 535 dev_vdbg(obj->dev, 514 536 "%s: da:%08x pgd:%p *pgd:%08x pte:%p *pte:%08x\n", 515 537 __func__, da, iopgd, *iopgd, iopte, *iopte); ··· 540 522 static int iopgd_alloc_section(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) 541 523 { 542 524 u32 *iopgd = iopgd_offset(obj, da); 525 + unsigned long offset = iopgd_index(da) * sizeof(da); 543 526 544 527 if ((da | pa) & ~IOSECTION_MASK) { 545 528 dev_err(obj->dev, "%s: %08x:%08x should aligned on %08lx\n", ··· 549 530 } 550 531 551 532 *iopgd = (pa & IOSECTION_MASK) | prot | IOPGD_SECTION; 552 - flush_iopgd_range(iopgd, iopgd); 533 + flush_iopte_range(obj->dev, obj->pd_dma, offset, 1); 553 534 return 0; 554 535 } 555 536 556 537 static int iopgd_alloc_super(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) 557 538 { 558 539 u32 *iopgd = iopgd_offset(obj, da); 540 + unsigned long offset = iopgd_index(da) * sizeof(da); 559 541 int i; 560 542 561 543 if ((da | pa) & ~IOSUPER_MASK) { ··· 567 547 568 548 for (i = 0; i < 16; i++) 569 549 *(iopgd + i) = (pa & IOSUPER_MASK) | prot | IOPGD_SUPER; 570 - flush_iopgd_range(iopgd, iopgd + 15); 550 + flush_iopte_range(obj->dev, obj->pd_dma, offset, 16); 571 551 return 0; 572 552 } 573 553 574 554 static int iopte_alloc_page(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) 575 555 { 576 556 u32 *iopgd = iopgd_offset(obj, da); 577 - u32 *iopte = iopte_alloc(obj, iopgd, da); 557 + dma_addr_t pt_dma; 558 + u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da); 559 + unsigned long offset = iopte_index(da) * sizeof(da); 578 560 579 561 if (IS_ERR(iopte)) 580 562 return PTR_ERR(iopte); 581 563 582 564 *iopte = (pa & IOPAGE_MASK) | prot | IOPTE_SMALL; 583 - flush_iopte_range(iopte, iopte); 565 + flush_iopte_range(obj->dev, pt_dma, offset, 1); 584 566 585 567 dev_vdbg(obj->dev, "%s: da:%08x pa:%08x pte:%p *pte:%08x\n", 586 568 __func__, da, pa, iopte, *iopte); ··· 593 571 static int iopte_alloc_large(struct omap_iommu *obj, u32 da, u32 pa, u32 prot) 594 572 { 595 573 u32 *iopgd = iopgd_offset(obj, da); 596 - u32 *iopte = iopte_alloc(obj, iopgd, da); 574 + dma_addr_t pt_dma; 575 + u32 *iopte = iopte_alloc(obj, iopgd, &pt_dma, da); 576 + unsigned long offset = iopte_index(da) * sizeof(da); 597 577 int i; 598 578 599 579 if ((da | pa) & ~IOLARGE_MASK) { ··· 609 585 610 586 for (i = 0; i < 16; i++) 611 587 *(iopte + i) = (pa & IOLARGE_MASK) | prot | IOPTE_LARGE; 612 - flush_iopte_range(iopte, iopte + 15); 588 + flush_iopte_range(obj->dev, pt_dma, offset, 16); 613 589 return 0; 614 590 } 615 591 ··· 698 674 size_t bytes; 699 675 u32 *iopgd = iopgd_offset(obj, da); 700 676 int nent = 1; 677 + dma_addr_t pt_dma; 678 + unsigned long pd_offset = iopgd_index(da) * sizeof(da); 679 + unsigned long pt_offset = iopte_index(da) * sizeof(da); 701 680 702 681 if (!*iopgd) 703 682 return 0; ··· 717 690 } 718 691 bytes *= nent; 719 692 memset(iopte, 0, nent * sizeof(*iopte)); 720 - flush_iopte_range(iopte, iopte + (nent - 1) * sizeof(*iopte)); 693 + pt_dma = virt_to_phys(iopte); 694 + flush_iopte_range(obj->dev, pt_dma, pt_offset, nent); 721 695 722 696 /* 723 697 * do table walk to check if this table is necessary or not ··· 728 700 if (iopte[i]) 729 701 goto out; 730 702 731 - iopte_free(iopte); 703 + iopte_free(obj, iopte, true); 732 704 nent = 1; /* for the next L1 entry */ 733 705 } else { 734 706 bytes = IOPGD_SIZE; ··· 740 712 bytes *= nent; 741 713 } 742 714 memset(iopgd, 0, nent * sizeof(*iopgd)); 743 - flush_iopgd_range(iopgd, iopgd + (nent - 1) * sizeof(*iopgd)); 715 + flush_iopte_range(obj->dev, obj->pd_dma, pd_offset, nent); 744 716 out: 745 717 return bytes; 746 718 } ··· 766 738 767 739 static void iopgtable_clear_entry_all(struct omap_iommu *obj) 768 740 { 741 + unsigned long offset; 769 742 int i; 770 743 771 744 spin_lock(&obj->page_table_lock); ··· 777 748 778 749 da = i << IOPGD_SHIFT; 779 750 iopgd = iopgd_offset(obj, da); 751 + offset = iopgd_index(da) * sizeof(da); 780 752 781 753 if (!*iopgd) 782 754 continue; 783 755 784 756 if (iopgd_is_table(*iopgd)) 785 - iopte_free(iopte_offset(iopgd, 0)); 757 + iopte_free(obj, iopte_offset(iopgd, 0), true); 786 758 787 759 *iopgd = 0; 788 - flush_iopgd_range(iopgd, iopgd); 760 + flush_iopte_range(obj->dev, obj->pd_dma, offset, 1); 789 761 } 790 762 791 763 flush_iotlb_all(obj); ··· 816 786 if (!report_iommu_fault(domain, obj->dev, da, 0)) 817 787 return IRQ_HANDLED; 818 788 819 - iommu_disable(obj); 789 + iommu_write_reg(obj, 0, MMU_IRQENABLE); 820 790 821 791 iopgd = iopgd_offset(obj, da); 822 792 ··· 845 815 846 816 spin_lock(&obj->iommu_lock); 847 817 818 + obj->pd_dma = dma_map_single(obj->dev, iopgd, IOPGD_TABLE_SIZE, 819 + DMA_TO_DEVICE); 820 + if (dma_mapping_error(obj->dev, obj->pd_dma)) { 821 + dev_err(obj->dev, "DMA map error for L1 table\n"); 822 + err = -ENOMEM; 823 + goto out_err; 824 + } 825 + 848 826 obj->iopgd = iopgd; 849 827 err = iommu_enable(obj); 850 828 if (err) 851 - goto err_enable; 829 + goto out_err; 852 830 flush_iotlb_all(obj); 853 831 854 832 spin_unlock(&obj->iommu_lock); ··· 865 827 866 828 return 0; 867 829 868 - err_enable: 830 + out_err: 869 831 spin_unlock(&obj->iommu_lock); 870 832 871 833 return err; ··· 882 844 883 845 spin_lock(&obj->iommu_lock); 884 846 847 + dma_unmap_single(obj->dev, obj->pd_dma, IOPGD_TABLE_SIZE, 848 + DMA_TO_DEVICE); 885 849 iommu_disable(obj); 850 + obj->pd_dma = 0; 886 851 obj->iopgd = NULL; 887 852 888 853 spin_unlock(&obj->iommu_lock); ··· 1049 1008 }, 1050 1009 }; 1051 1010 1052 - static void iopte_cachep_ctor(void *iopte) 1053 - { 1054 - clean_dcache_area(iopte, IOPTE_TABLE_SIZE); 1055 - } 1056 - 1057 1011 static u32 iotlb_init_entry(struct iotlb_entry *e, u32 da, u32 pa, int pgsz) 1058 1012 { 1059 1013 memset(e, 0, sizeof(*e)); ··· 1195 1159 if (WARN_ON(!IS_ALIGNED((long)omap_domain->pgtable, IOPGD_TABLE_SIZE))) 1196 1160 goto fail_align; 1197 1161 1198 - clean_dcache_area(omap_domain->pgtable, IOPGD_TABLE_SIZE); 1199 1162 spin_lock_init(&omap_domain->lock); 1200 1163 1201 1164 omap_domain->domain.geometry.aperture_start = 0; ··· 1382 1347 of_node_put(np); 1383 1348 1384 1349 p = kmem_cache_create("iopte_cache", IOPTE_TABLE_SIZE, align, flags, 1385 - iopte_cachep_ctor); 1350 + NULL); 1386 1351 if (!p) 1387 1352 return -ENOMEM; 1388 1353 iopte_cachep = p;
+1
drivers/iommu/omap-iommu.h
··· 61 61 */ 62 62 u32 *iopgd; 63 63 spinlock_t page_table_lock; /* protect iopgd */ 64 + dma_addr_t pd_dma; 64 65 65 66 int nr_tlb_entries; 66 67
+930
drivers/iommu/qcom_iommu.c
··· 1 + /* 2 + * IOMMU API for QCOM secure IOMMUs. Somewhat based on arm-smmu.c 3 + * 4 + * This program is free software; you can redistribute it and/or modify 5 + * it under the terms of the GNU General Public License version 2 as 6 + * published by the Free Software Foundation. 7 + * 8 + * This program is distributed in the hope that it will be useful, 9 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 + * GNU General Public License for more details. 12 + * 13 + * You should have received a copy of the GNU General Public License 14 + * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 + * 16 + * Copyright (C) 2013 ARM Limited 17 + * Copyright (C) 2017 Red Hat 18 + */ 19 + 20 + #include <linux/atomic.h> 21 + #include <linux/clk.h> 22 + #include <linux/delay.h> 23 + #include <linux/dma-iommu.h> 24 + #include <linux/dma-mapping.h> 25 + #include <linux/err.h> 26 + #include <linux/interrupt.h> 27 + #include <linux/io.h> 28 + #include <linux/io-64-nonatomic-hi-lo.h> 29 + #include <linux/iommu.h> 30 + #include <linux/iopoll.h> 31 + #include <linux/kconfig.h> 32 + #include <linux/module.h> 33 + #include <linux/mutex.h> 34 + #include <linux/of.h> 35 + #include <linux/of_address.h> 36 + #include <linux/of_device.h> 37 + #include <linux/of_iommu.h> 38 + #include <linux/platform_device.h> 39 + #include <linux/pm.h> 40 + #include <linux/pm_runtime.h> 41 + #include <linux/qcom_scm.h> 42 + #include <linux/slab.h> 43 + #include <linux/spinlock.h> 44 + 45 + #include "io-pgtable.h" 46 + #include "arm-smmu-regs.h" 47 + 48 + #define SMMU_INTR_SEL_NS 0x2000 49 + 50 + struct qcom_iommu_ctx; 51 + 52 + struct qcom_iommu_dev { 53 + /* IOMMU core code handle */ 54 + struct iommu_device iommu; 55 + struct device *dev; 56 + struct clk *iface_clk; 57 + struct clk *bus_clk; 58 + void __iomem *local_base; 59 + u32 sec_id; 60 + u8 num_ctxs; 61 + struct qcom_iommu_ctx *ctxs[0]; /* indexed by asid-1 */ 62 + }; 63 + 64 + struct qcom_iommu_ctx { 65 + struct device *dev; 66 + void __iomem *base; 67 + bool secure_init; 68 + u8 asid; /* asid and ctx bank # are 1:1 */ 69 + }; 70 + 71 + struct qcom_iommu_domain { 72 + struct io_pgtable_ops *pgtbl_ops; 73 + spinlock_t pgtbl_lock; 74 + struct mutex init_mutex; /* Protects iommu pointer */ 75 + struct iommu_domain domain; 76 + struct qcom_iommu_dev *iommu; 77 + }; 78 + 79 + static struct qcom_iommu_domain *to_qcom_iommu_domain(struct iommu_domain *dom) 80 + { 81 + return container_of(dom, struct qcom_iommu_domain, domain); 82 + } 83 + 84 + static const struct iommu_ops qcom_iommu_ops; 85 + 86 + static struct qcom_iommu_dev * to_iommu(struct iommu_fwspec *fwspec) 87 + { 88 + if (!fwspec || fwspec->ops != &qcom_iommu_ops) 89 + return NULL; 90 + return fwspec->iommu_priv; 91 + } 92 + 93 + static struct qcom_iommu_ctx * to_ctx(struct iommu_fwspec *fwspec, unsigned asid) 94 + { 95 + struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec); 96 + if (!qcom_iommu) 97 + return NULL; 98 + return qcom_iommu->ctxs[asid - 1]; 99 + } 100 + 101 + static inline void 102 + iommu_writel(struct qcom_iommu_ctx *ctx, unsigned reg, u32 val) 103 + { 104 + writel_relaxed(val, ctx->base + reg); 105 + } 106 + 107 + static inline void 108 + iommu_writeq(struct qcom_iommu_ctx *ctx, unsigned reg, u64 val) 109 + { 110 + writeq_relaxed(val, ctx->base + reg); 111 + } 112 + 113 + static inline u32 114 + iommu_readl(struct qcom_iommu_ctx *ctx, unsigned reg) 115 + { 116 + return readl_relaxed(ctx->base + reg); 117 + } 118 + 119 + static inline u64 120 + iommu_readq(struct qcom_iommu_ctx *ctx, unsigned reg) 121 + { 122 + return readq_relaxed(ctx->base + reg); 123 + } 124 + 125 + static void qcom_iommu_tlb_sync(void *cookie) 126 + { 127 + struct iommu_fwspec *fwspec = cookie; 128 + unsigned i; 129 + 130 + for (i = 0; i < fwspec->num_ids; i++) { 131 + struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]); 132 + unsigned int val, ret; 133 + 134 + iommu_writel(ctx, ARM_SMMU_CB_TLBSYNC, 0); 135 + 136 + ret = readl_poll_timeout(ctx->base + ARM_SMMU_CB_TLBSTATUS, val, 137 + (val & 0x1) == 0, 0, 5000000); 138 + if (ret) 139 + dev_err(ctx->dev, "timeout waiting for TLB SYNC\n"); 140 + } 141 + } 142 + 143 + static void qcom_iommu_tlb_inv_context(void *cookie) 144 + { 145 + struct iommu_fwspec *fwspec = cookie; 146 + unsigned i; 147 + 148 + for (i = 0; i < fwspec->num_ids; i++) { 149 + struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]); 150 + iommu_writel(ctx, ARM_SMMU_CB_S1_TLBIASID, ctx->asid); 151 + } 152 + 153 + qcom_iommu_tlb_sync(cookie); 154 + } 155 + 156 + static void qcom_iommu_tlb_inv_range_nosync(unsigned long iova, size_t size, 157 + size_t granule, bool leaf, void *cookie) 158 + { 159 + struct iommu_fwspec *fwspec = cookie; 160 + unsigned i, reg; 161 + 162 + reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA; 163 + 164 + for (i = 0; i < fwspec->num_ids; i++) { 165 + struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]); 166 + size_t s = size; 167 + 168 + iova &= ~12UL; 169 + iova |= ctx->asid; 170 + do { 171 + iommu_writel(ctx, reg, iova); 172 + iova += granule; 173 + } while (s -= granule); 174 + } 175 + } 176 + 177 + static const struct iommu_gather_ops qcom_gather_ops = { 178 + .tlb_flush_all = qcom_iommu_tlb_inv_context, 179 + .tlb_add_flush = qcom_iommu_tlb_inv_range_nosync, 180 + .tlb_sync = qcom_iommu_tlb_sync, 181 + }; 182 + 183 + static irqreturn_t qcom_iommu_fault(int irq, void *dev) 184 + { 185 + struct qcom_iommu_ctx *ctx = dev; 186 + u32 fsr, fsynr; 187 + u64 iova; 188 + 189 + fsr = iommu_readl(ctx, ARM_SMMU_CB_FSR); 190 + 191 + if (!(fsr & FSR_FAULT)) 192 + return IRQ_NONE; 193 + 194 + fsynr = iommu_readl(ctx, ARM_SMMU_CB_FSYNR0); 195 + iova = iommu_readq(ctx, ARM_SMMU_CB_FAR); 196 + 197 + dev_err_ratelimited(ctx->dev, 198 + "Unhandled context fault: fsr=0x%x, " 199 + "iova=0x%016llx, fsynr=0x%x, cb=%d\n", 200 + fsr, iova, fsynr, ctx->asid); 201 + 202 + iommu_writel(ctx, ARM_SMMU_CB_FSR, fsr); 203 + 204 + return IRQ_HANDLED; 205 + } 206 + 207 + static int qcom_iommu_init_domain(struct iommu_domain *domain, 208 + struct qcom_iommu_dev *qcom_iommu, 209 + struct iommu_fwspec *fwspec) 210 + { 211 + struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); 212 + struct io_pgtable_ops *pgtbl_ops; 213 + struct io_pgtable_cfg pgtbl_cfg; 214 + int i, ret = 0; 215 + u32 reg; 216 + 217 + mutex_lock(&qcom_domain->init_mutex); 218 + if (qcom_domain->iommu) 219 + goto out_unlock; 220 + 221 + pgtbl_cfg = (struct io_pgtable_cfg) { 222 + .pgsize_bitmap = qcom_iommu_ops.pgsize_bitmap, 223 + .ias = 32, 224 + .oas = 40, 225 + .tlb = &qcom_gather_ops, 226 + .iommu_dev = qcom_iommu->dev, 227 + }; 228 + 229 + qcom_domain->iommu = qcom_iommu; 230 + pgtbl_ops = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &pgtbl_cfg, fwspec); 231 + if (!pgtbl_ops) { 232 + dev_err(qcom_iommu->dev, "failed to allocate pagetable ops\n"); 233 + ret = -ENOMEM; 234 + goto out_clear_iommu; 235 + } 236 + 237 + /* Update the domain's page sizes to reflect the page table format */ 238 + domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap; 239 + domain->geometry.aperture_end = (1ULL << pgtbl_cfg.ias) - 1; 240 + domain->geometry.force_aperture = true; 241 + 242 + for (i = 0; i < fwspec->num_ids; i++) { 243 + struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]); 244 + 245 + if (!ctx->secure_init) { 246 + ret = qcom_scm_restore_sec_cfg(qcom_iommu->sec_id, ctx->asid); 247 + if (ret) { 248 + dev_err(qcom_iommu->dev, "secure init failed: %d\n", ret); 249 + goto out_clear_iommu; 250 + } 251 + ctx->secure_init = true; 252 + } 253 + 254 + /* TTBRs */ 255 + iommu_writeq(ctx, ARM_SMMU_CB_TTBR0, 256 + pgtbl_cfg.arm_lpae_s1_cfg.ttbr[0] | 257 + ((u64)ctx->asid << TTBRn_ASID_SHIFT)); 258 + iommu_writeq(ctx, ARM_SMMU_CB_TTBR1, 259 + pgtbl_cfg.arm_lpae_s1_cfg.ttbr[1] | 260 + ((u64)ctx->asid << TTBRn_ASID_SHIFT)); 261 + 262 + /* TTBCR */ 263 + iommu_writel(ctx, ARM_SMMU_CB_TTBCR2, 264 + (pgtbl_cfg.arm_lpae_s1_cfg.tcr >> 32) | 265 + TTBCR2_SEP_UPSTREAM); 266 + iommu_writel(ctx, ARM_SMMU_CB_TTBCR, 267 + pgtbl_cfg.arm_lpae_s1_cfg.tcr); 268 + 269 + /* MAIRs (stage-1 only) */ 270 + iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR0, 271 + pgtbl_cfg.arm_lpae_s1_cfg.mair[0]); 272 + iommu_writel(ctx, ARM_SMMU_CB_S1_MAIR1, 273 + pgtbl_cfg.arm_lpae_s1_cfg.mair[1]); 274 + 275 + /* SCTLR */ 276 + reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE | 277 + SCTLR_M | SCTLR_S1_ASIDPNE; 278 + 279 + if (IS_ENABLED(CONFIG_BIG_ENDIAN)) 280 + reg |= SCTLR_E; 281 + 282 + iommu_writel(ctx, ARM_SMMU_CB_SCTLR, reg); 283 + } 284 + 285 + mutex_unlock(&qcom_domain->init_mutex); 286 + 287 + /* Publish page table ops for map/unmap */ 288 + qcom_domain->pgtbl_ops = pgtbl_ops; 289 + 290 + return 0; 291 + 292 + out_clear_iommu: 293 + qcom_domain->iommu = NULL; 294 + out_unlock: 295 + mutex_unlock(&qcom_domain->init_mutex); 296 + return ret; 297 + } 298 + 299 + static struct iommu_domain *qcom_iommu_domain_alloc(unsigned type) 300 + { 301 + struct qcom_iommu_domain *qcom_domain; 302 + 303 + if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA) 304 + return NULL; 305 + /* 306 + * Allocate the domain and initialise some of its data structures. 307 + * We can't really do anything meaningful until we've added a 308 + * master. 309 + */ 310 + qcom_domain = kzalloc(sizeof(*qcom_domain), GFP_KERNEL); 311 + if (!qcom_domain) 312 + return NULL; 313 + 314 + if (type == IOMMU_DOMAIN_DMA && 315 + iommu_get_dma_cookie(&qcom_domain->domain)) { 316 + kfree(qcom_domain); 317 + return NULL; 318 + } 319 + 320 + mutex_init(&qcom_domain->init_mutex); 321 + spin_lock_init(&qcom_domain->pgtbl_lock); 322 + 323 + return &qcom_domain->domain; 324 + } 325 + 326 + static void qcom_iommu_domain_free(struct iommu_domain *domain) 327 + { 328 + struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); 329 + 330 + if (WARN_ON(qcom_domain->iommu)) /* forgot to detach? */ 331 + return; 332 + 333 + iommu_put_dma_cookie(domain); 334 + 335 + /* NOTE: unmap can be called after client device is powered off, 336 + * for example, with GPUs or anything involving dma-buf. So we 337 + * cannot rely on the device_link. Make sure the IOMMU is on to 338 + * avoid unclocked accesses in the TLB inv path: 339 + */ 340 + pm_runtime_get_sync(qcom_domain->iommu->dev); 341 + 342 + free_io_pgtable_ops(qcom_domain->pgtbl_ops); 343 + 344 + pm_runtime_put_sync(qcom_domain->iommu->dev); 345 + 346 + kfree(qcom_domain); 347 + } 348 + 349 + static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) 350 + { 351 + struct qcom_iommu_dev *qcom_iommu = to_iommu(dev->iommu_fwspec); 352 + struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); 353 + int ret; 354 + 355 + if (!qcom_iommu) { 356 + dev_err(dev, "cannot attach to IOMMU, is it on the same bus?\n"); 357 + return -ENXIO; 358 + } 359 + 360 + /* Ensure that the domain is finalized */ 361 + pm_runtime_get_sync(qcom_iommu->dev); 362 + ret = qcom_iommu_init_domain(domain, qcom_iommu, dev->iommu_fwspec); 363 + pm_runtime_put_sync(qcom_iommu->dev); 364 + if (ret < 0) 365 + return ret; 366 + 367 + /* 368 + * Sanity check the domain. We don't support domains across 369 + * different IOMMUs. 370 + */ 371 + if (qcom_domain->iommu != qcom_iommu) { 372 + dev_err(dev, "cannot attach to IOMMU %s while already " 373 + "attached to domain on IOMMU %s\n", 374 + dev_name(qcom_domain->iommu->dev), 375 + dev_name(qcom_iommu->dev)); 376 + return -EINVAL; 377 + } 378 + 379 + return 0; 380 + } 381 + 382 + static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *dev) 383 + { 384 + struct iommu_fwspec *fwspec = dev->iommu_fwspec; 385 + struct qcom_iommu_dev *qcom_iommu = to_iommu(fwspec); 386 + struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); 387 + unsigned i; 388 + 389 + if (!qcom_domain->iommu) 390 + return; 391 + 392 + pm_runtime_get_sync(qcom_iommu->dev); 393 + for (i = 0; i < fwspec->num_ids; i++) { 394 + struct qcom_iommu_ctx *ctx = to_ctx(fwspec, fwspec->ids[i]); 395 + 396 + /* Disable the context bank: */ 397 + iommu_writel(ctx, ARM_SMMU_CB_SCTLR, 0); 398 + } 399 + pm_runtime_put_sync(qcom_iommu->dev); 400 + 401 + qcom_domain->iommu = NULL; 402 + } 403 + 404 + static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova, 405 + phys_addr_t paddr, size_t size, int prot) 406 + { 407 + int ret; 408 + unsigned long flags; 409 + struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); 410 + struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops; 411 + 412 + if (!ops) 413 + return -ENODEV; 414 + 415 + spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags); 416 + ret = ops->map(ops, iova, paddr, size, prot); 417 + spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags); 418 + return ret; 419 + } 420 + 421 + static size_t qcom_iommu_unmap(struct iommu_domain *domain, unsigned long iova, 422 + size_t size) 423 + { 424 + size_t ret; 425 + unsigned long flags; 426 + struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); 427 + struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops; 428 + 429 + if (!ops) 430 + return 0; 431 + 432 + /* NOTE: unmap can be called after client device is powered off, 433 + * for example, with GPUs or anything involving dma-buf. So we 434 + * cannot rely on the device_link. Make sure the IOMMU is on to 435 + * avoid unclocked accesses in the TLB inv path: 436 + */ 437 + pm_runtime_get_sync(qcom_domain->iommu->dev); 438 + spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags); 439 + ret = ops->unmap(ops, iova, size); 440 + spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags); 441 + pm_runtime_put_sync(qcom_domain->iommu->dev); 442 + 443 + return ret; 444 + } 445 + 446 + static phys_addr_t qcom_iommu_iova_to_phys(struct iommu_domain *domain, 447 + dma_addr_t iova) 448 + { 449 + phys_addr_t ret; 450 + unsigned long flags; 451 + struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain); 452 + struct io_pgtable_ops *ops = qcom_domain->pgtbl_ops; 453 + 454 + if (!ops) 455 + return 0; 456 + 457 + spin_lock_irqsave(&qcom_domain->pgtbl_lock, flags); 458 + ret = ops->iova_to_phys(ops, iova); 459 + spin_unlock_irqrestore(&qcom_domain->pgtbl_lock, flags); 460 + 461 + return ret; 462 + } 463 + 464 + static bool qcom_iommu_capable(enum iommu_cap cap) 465 + { 466 + switch (cap) { 467 + case IOMMU_CAP_CACHE_COHERENCY: 468 + /* 469 + * Return true here as the SMMU can always send out coherent 470 + * requests. 471 + */ 472 + return true; 473 + case IOMMU_CAP_NOEXEC: 474 + return true; 475 + default: 476 + return false; 477 + } 478 + } 479 + 480 + static int qcom_iommu_add_device(struct device *dev) 481 + { 482 + struct qcom_iommu_dev *qcom_iommu = to_iommu(dev->iommu_fwspec); 483 + struct iommu_group *group; 484 + struct device_link *link; 485 + 486 + if (!qcom_iommu) 487 + return -ENODEV; 488 + 489 + /* 490 + * Establish the link between iommu and master, so that the 491 + * iommu gets runtime enabled/disabled as per the master's 492 + * needs. 493 + */ 494 + link = device_link_add(dev, qcom_iommu->dev, DL_FLAG_PM_RUNTIME); 495 + if (!link) { 496 + dev_err(qcom_iommu->dev, "Unable to create device link between %s and %s\n", 497 + dev_name(qcom_iommu->dev), dev_name(dev)); 498 + return -ENODEV; 499 + } 500 + 501 + group = iommu_group_get_for_dev(dev); 502 + if (IS_ERR_OR_NULL(group)) 503 + return PTR_ERR_OR_ZERO(group); 504 + 505 + iommu_group_put(group); 506 + iommu_device_link(&qcom_iommu->iommu, dev); 507 + 508 + return 0; 509 + } 510 + 511 + static void qcom_iommu_remove_device(struct device *dev) 512 + { 513 + struct qcom_iommu_dev *qcom_iommu = to_iommu(dev->iommu_fwspec); 514 + 515 + if (!qcom_iommu) 516 + return; 517 + 518 + iommu_device_unlink(&qcom_iommu->iommu, dev); 519 + iommu_group_remove_device(dev); 520 + iommu_fwspec_free(dev); 521 + } 522 + 523 + static int qcom_iommu_of_xlate(struct device *dev, struct of_phandle_args *args) 524 + { 525 + struct qcom_iommu_dev *qcom_iommu; 526 + struct platform_device *iommu_pdev; 527 + unsigned asid = args->args[0]; 528 + 529 + if (args->args_count != 1) { 530 + dev_err(dev, "incorrect number of iommu params found for %s " 531 + "(found %d, expected 1)\n", 532 + args->np->full_name, args->args_count); 533 + return -EINVAL; 534 + } 535 + 536 + iommu_pdev = of_find_device_by_node(args->np); 537 + if (WARN_ON(!iommu_pdev)) 538 + return -EINVAL; 539 + 540 + qcom_iommu = platform_get_drvdata(iommu_pdev); 541 + 542 + /* make sure the asid specified in dt is valid, so we don't have 543 + * to sanity check this elsewhere, since 'asid - 1' is used to 544 + * index into qcom_iommu->ctxs: 545 + */ 546 + if (WARN_ON(asid < 1) || 547 + WARN_ON(asid > qcom_iommu->num_ctxs)) 548 + return -EINVAL; 549 + 550 + if (!dev->iommu_fwspec->iommu_priv) { 551 + dev->iommu_fwspec->iommu_priv = qcom_iommu; 552 + } else { 553 + /* make sure devices iommus dt node isn't referring to 554 + * multiple different iommu devices. Multiple context 555 + * banks are ok, but multiple devices are not: 556 + */ 557 + if (WARN_ON(qcom_iommu != dev->iommu_fwspec->iommu_priv)) 558 + return -EINVAL; 559 + } 560 + 561 + return iommu_fwspec_add_ids(dev, &asid, 1); 562 + } 563 + 564 + static const struct iommu_ops qcom_iommu_ops = { 565 + .capable = qcom_iommu_capable, 566 + .domain_alloc = qcom_iommu_domain_alloc, 567 + .domain_free = qcom_iommu_domain_free, 568 + .attach_dev = qcom_iommu_attach_dev, 569 + .detach_dev = qcom_iommu_detach_dev, 570 + .map = qcom_iommu_map, 571 + .unmap = qcom_iommu_unmap, 572 + .map_sg = default_iommu_map_sg, 573 + .iova_to_phys = qcom_iommu_iova_to_phys, 574 + .add_device = qcom_iommu_add_device, 575 + .remove_device = qcom_iommu_remove_device, 576 + .device_group = generic_device_group, 577 + .of_xlate = qcom_iommu_of_xlate, 578 + .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M, 579 + }; 580 + 581 + static int qcom_iommu_enable_clocks(struct qcom_iommu_dev *qcom_iommu) 582 + { 583 + int ret; 584 + 585 + ret = clk_prepare_enable(qcom_iommu->iface_clk); 586 + if (ret) { 587 + dev_err(qcom_iommu->dev, "Couldn't enable iface_clk\n"); 588 + return ret; 589 + } 590 + 591 + ret = clk_prepare_enable(qcom_iommu->bus_clk); 592 + if (ret) { 593 + dev_err(qcom_iommu->dev, "Couldn't enable bus_clk\n"); 594 + clk_disable_unprepare(qcom_iommu->iface_clk); 595 + return ret; 596 + } 597 + 598 + return 0; 599 + } 600 + 601 + static void qcom_iommu_disable_clocks(struct qcom_iommu_dev *qcom_iommu) 602 + { 603 + clk_disable_unprepare(qcom_iommu->bus_clk); 604 + clk_disable_unprepare(qcom_iommu->iface_clk); 605 + } 606 + 607 + static int qcom_iommu_sec_ptbl_init(struct device *dev) 608 + { 609 + size_t psize = 0; 610 + unsigned int spare = 0; 611 + void *cpu_addr; 612 + dma_addr_t paddr; 613 + unsigned long attrs; 614 + static bool allocated = false; 615 + int ret; 616 + 617 + if (allocated) 618 + return 0; 619 + 620 + ret = qcom_scm_iommu_secure_ptbl_size(spare, &psize); 621 + if (ret) { 622 + dev_err(dev, "failed to get iommu secure pgtable size (%d)\n", 623 + ret); 624 + return ret; 625 + } 626 + 627 + dev_info(dev, "iommu sec: pgtable size: %zu\n", psize); 628 + 629 + attrs = DMA_ATTR_NO_KERNEL_MAPPING; 630 + 631 + cpu_addr = dma_alloc_attrs(dev, psize, &paddr, GFP_KERNEL, attrs); 632 + if (!cpu_addr) { 633 + dev_err(dev, "failed to allocate %zu bytes for pgtable\n", 634 + psize); 635 + return -ENOMEM; 636 + } 637 + 638 + ret = qcom_scm_iommu_secure_ptbl_init(paddr, psize, spare); 639 + if (ret) { 640 + dev_err(dev, "failed to init iommu pgtable (%d)\n", ret); 641 + goto free_mem; 642 + } 643 + 644 + allocated = true; 645 + return 0; 646 + 647 + free_mem: 648 + dma_free_attrs(dev, psize, cpu_addr, paddr, attrs); 649 + return ret; 650 + } 651 + 652 + static int get_asid(const struct device_node *np) 653 + { 654 + u32 reg; 655 + 656 + /* read the "reg" property directly to get the relative address 657 + * of the context bank, and calculate the asid from that: 658 + */ 659 + if (of_property_read_u32_index(np, "reg", 0, &reg)) 660 + return -ENODEV; 661 + 662 + return reg / 0x1000; /* context banks are 0x1000 apart */ 663 + } 664 + 665 + static int qcom_iommu_ctx_probe(struct platform_device *pdev) 666 + { 667 + struct qcom_iommu_ctx *ctx; 668 + struct device *dev = &pdev->dev; 669 + struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(dev->parent); 670 + struct resource *res; 671 + int ret, irq; 672 + 673 + ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 674 + if (!ctx) 675 + return -ENOMEM; 676 + 677 + ctx->dev = dev; 678 + platform_set_drvdata(pdev, ctx); 679 + 680 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 681 + ctx->base = devm_ioremap_resource(dev, res); 682 + if (IS_ERR(ctx->base)) 683 + return PTR_ERR(ctx->base); 684 + 685 + irq = platform_get_irq(pdev, 0); 686 + if (irq < 0) { 687 + dev_err(dev, "failed to get irq\n"); 688 + return -ENODEV; 689 + } 690 + 691 + /* clear IRQs before registering fault handler, just in case the 692 + * boot-loader left us a surprise: 693 + */ 694 + iommu_writel(ctx, ARM_SMMU_CB_FSR, iommu_readl(ctx, ARM_SMMU_CB_FSR)); 695 + 696 + ret = devm_request_irq(dev, irq, 697 + qcom_iommu_fault, 698 + IRQF_SHARED, 699 + "qcom-iommu-fault", 700 + ctx); 701 + if (ret) { 702 + dev_err(dev, "failed to request IRQ %u\n", irq); 703 + return ret; 704 + } 705 + 706 + ret = get_asid(dev->of_node); 707 + if (ret < 0) { 708 + dev_err(dev, "missing reg property\n"); 709 + return ret; 710 + } 711 + 712 + ctx->asid = ret; 713 + 714 + dev_dbg(dev, "found asid %u\n", ctx->asid); 715 + 716 + qcom_iommu->ctxs[ctx->asid - 1] = ctx; 717 + 718 + return 0; 719 + } 720 + 721 + static int qcom_iommu_ctx_remove(struct platform_device *pdev) 722 + { 723 + struct qcom_iommu_dev *qcom_iommu = dev_get_drvdata(pdev->dev.parent); 724 + struct qcom_iommu_ctx *ctx = platform_get_drvdata(pdev); 725 + 726 + platform_set_drvdata(pdev, NULL); 727 + 728 + qcom_iommu->ctxs[ctx->asid - 1] = NULL; 729 + 730 + return 0; 731 + } 732 + 733 + static const struct of_device_id ctx_of_match[] = { 734 + { .compatible = "qcom,msm-iommu-v1-ns" }, 735 + { .compatible = "qcom,msm-iommu-v1-sec" }, 736 + { /* sentinel */ } 737 + }; 738 + 739 + static struct platform_driver qcom_iommu_ctx_driver = { 740 + .driver = { 741 + .name = "qcom-iommu-ctx", 742 + .of_match_table = of_match_ptr(ctx_of_match), 743 + }, 744 + .probe = qcom_iommu_ctx_probe, 745 + .remove = qcom_iommu_ctx_remove, 746 + }; 747 + 748 + static bool qcom_iommu_has_secure_context(struct qcom_iommu_dev *qcom_iommu) 749 + { 750 + struct device_node *child; 751 + 752 + for_each_child_of_node(qcom_iommu->dev->of_node, child) 753 + if (of_device_is_compatible(child, "qcom,msm-iommu-v1-sec")) 754 + return true; 755 + 756 + return false; 757 + } 758 + 759 + static int qcom_iommu_device_probe(struct platform_device *pdev) 760 + { 761 + struct device_node *child; 762 + struct qcom_iommu_dev *qcom_iommu; 763 + struct device *dev = &pdev->dev; 764 + struct resource *res; 765 + int ret, sz, max_asid = 0; 766 + 767 + /* find the max asid (which is 1:1 to ctx bank idx), so we know how 768 + * many child ctx devices we have: 769 + */ 770 + for_each_child_of_node(dev->of_node, child) 771 + max_asid = max(max_asid, get_asid(child)); 772 + 773 + sz = sizeof(*qcom_iommu) + (max_asid * sizeof(qcom_iommu->ctxs[0])); 774 + 775 + qcom_iommu = devm_kzalloc(dev, sz, GFP_KERNEL); 776 + if (!qcom_iommu) 777 + return -ENOMEM; 778 + qcom_iommu->num_ctxs = max_asid; 779 + qcom_iommu->dev = dev; 780 + 781 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 782 + if (res) 783 + qcom_iommu->local_base = devm_ioremap_resource(dev, res); 784 + 785 + qcom_iommu->iface_clk = devm_clk_get(dev, "iface"); 786 + if (IS_ERR(qcom_iommu->iface_clk)) { 787 + dev_err(dev, "failed to get iface clock\n"); 788 + return PTR_ERR(qcom_iommu->iface_clk); 789 + } 790 + 791 + qcom_iommu->bus_clk = devm_clk_get(dev, "bus"); 792 + if (IS_ERR(qcom_iommu->bus_clk)) { 793 + dev_err(dev, "failed to get bus clock\n"); 794 + return PTR_ERR(qcom_iommu->bus_clk); 795 + } 796 + 797 + if (of_property_read_u32(dev->of_node, "qcom,iommu-secure-id", 798 + &qcom_iommu->sec_id)) { 799 + dev_err(dev, "missing qcom,iommu-secure-id property\n"); 800 + return -ENODEV; 801 + } 802 + 803 + if (qcom_iommu_has_secure_context(qcom_iommu)) { 804 + ret = qcom_iommu_sec_ptbl_init(dev); 805 + if (ret) { 806 + dev_err(dev, "cannot init secure pg table(%d)\n", ret); 807 + return ret; 808 + } 809 + } 810 + 811 + platform_set_drvdata(pdev, qcom_iommu); 812 + 813 + pm_runtime_enable(dev); 814 + 815 + /* register context bank devices, which are child nodes: */ 816 + ret = devm_of_platform_populate(dev); 817 + if (ret) { 818 + dev_err(dev, "Failed to populate iommu contexts\n"); 819 + return ret; 820 + } 821 + 822 + ret = iommu_device_sysfs_add(&qcom_iommu->iommu, dev, NULL, 823 + dev_name(dev)); 824 + if (ret) { 825 + dev_err(dev, "Failed to register iommu in sysfs\n"); 826 + return ret; 827 + } 828 + 829 + iommu_device_set_ops(&qcom_iommu->iommu, &qcom_iommu_ops); 830 + iommu_device_set_fwnode(&qcom_iommu->iommu, dev->fwnode); 831 + 832 + ret = iommu_device_register(&qcom_iommu->iommu); 833 + if (ret) { 834 + dev_err(dev, "Failed to register iommu\n"); 835 + return ret; 836 + } 837 + 838 + bus_set_iommu(&platform_bus_type, &qcom_iommu_ops); 839 + 840 + if (qcom_iommu->local_base) { 841 + pm_runtime_get_sync(dev); 842 + writel_relaxed(0xffffffff, qcom_iommu->local_base + SMMU_INTR_SEL_NS); 843 + pm_runtime_put_sync(dev); 844 + } 845 + 846 + return 0; 847 + } 848 + 849 + static int qcom_iommu_device_remove(struct platform_device *pdev) 850 + { 851 + struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev); 852 + 853 + bus_set_iommu(&platform_bus_type, NULL); 854 + 855 + pm_runtime_force_suspend(&pdev->dev); 856 + platform_set_drvdata(pdev, NULL); 857 + iommu_device_sysfs_remove(&qcom_iommu->iommu); 858 + iommu_device_unregister(&qcom_iommu->iommu); 859 + 860 + return 0; 861 + } 862 + 863 + static int __maybe_unused qcom_iommu_resume(struct device *dev) 864 + { 865 + struct platform_device *pdev = to_platform_device(dev); 866 + struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev); 867 + 868 + return qcom_iommu_enable_clocks(qcom_iommu); 869 + } 870 + 871 + static int __maybe_unused qcom_iommu_suspend(struct device *dev) 872 + { 873 + struct platform_device *pdev = to_platform_device(dev); 874 + struct qcom_iommu_dev *qcom_iommu = platform_get_drvdata(pdev); 875 + 876 + qcom_iommu_disable_clocks(qcom_iommu); 877 + 878 + return 0; 879 + } 880 + 881 + static const struct dev_pm_ops qcom_iommu_pm_ops = { 882 + SET_RUNTIME_PM_OPS(qcom_iommu_suspend, qcom_iommu_resume, NULL) 883 + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 884 + pm_runtime_force_resume) 885 + }; 886 + 887 + static const struct of_device_id qcom_iommu_of_match[] = { 888 + { .compatible = "qcom,msm-iommu-v1" }, 889 + { /* sentinel */ } 890 + }; 891 + MODULE_DEVICE_TABLE(of, qcom_iommu_of_match); 892 + 893 + static struct platform_driver qcom_iommu_driver = { 894 + .driver = { 895 + .name = "qcom-iommu", 896 + .of_match_table = of_match_ptr(qcom_iommu_of_match), 897 + .pm = &qcom_iommu_pm_ops, 898 + }, 899 + .probe = qcom_iommu_device_probe, 900 + .remove = qcom_iommu_device_remove, 901 + }; 902 + 903 + static int __init qcom_iommu_init(void) 904 + { 905 + int ret; 906 + 907 + ret = platform_driver_register(&qcom_iommu_ctx_driver); 908 + if (ret) 909 + return ret; 910 + 911 + ret = platform_driver_register(&qcom_iommu_driver); 912 + if (ret) 913 + platform_driver_unregister(&qcom_iommu_ctx_driver); 914 + 915 + return ret; 916 + } 917 + 918 + static void __exit qcom_iommu_exit(void) 919 + { 920 + platform_driver_unregister(&qcom_iommu_driver); 921 + platform_driver_unregister(&qcom_iommu_ctx_driver); 922 + } 923 + 924 + module_init(qcom_iommu_init); 925 + module_exit(qcom_iommu_exit); 926 + 927 + IOMMU_OF_DECLARE(qcom_iommu_dev, "qcom,msm-iommu-v1", NULL); 928 + 929 + MODULE_DESCRIPTION("IOMMU API for QCOM IOMMU v1 implementations"); 930 + MODULE_LICENSE("GPL v2");
+38 -14
drivers/iommu/rockchip-iommu.c
··· 90 90 struct device *dev; 91 91 void __iomem **bases; 92 92 int num_mmu; 93 - int irq; 93 + int *irq; 94 + int num_irq; 95 + bool reset_disabled; 94 96 struct iommu_device iommu; 95 97 struct list_head node; /* entry in rk_iommu_domain.iommus */ 96 98 struct iommu_domain *domain; /* domain to which iommu is attached */ ··· 415 413 { 416 414 int ret, i; 417 415 u32 dte_addr; 416 + 417 + if (iommu->reset_disabled) 418 + return 0; 418 419 419 420 /* 420 421 * Check if register DTE_ADDR is working by writing DTE_ADDR_DUMMY ··· 830 825 831 826 iommu->domain = domain; 832 827 833 - ret = devm_request_irq(iommu->dev, iommu->irq, rk_iommu_irq, 834 - IRQF_SHARED, dev_name(dev), iommu); 835 - if (ret) 836 - return ret; 828 + for (i = 0; i < iommu->num_irq; i++) { 829 + ret = devm_request_irq(iommu->dev, iommu->irq[i], rk_iommu_irq, 830 + IRQF_SHARED, dev_name(dev), iommu); 831 + if (ret) 832 + return ret; 833 + } 837 834 838 835 for (i = 0; i < iommu->num_mmu; i++) { 839 836 rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, ··· 885 878 } 886 879 rk_iommu_disable_stall(iommu); 887 880 888 - devm_free_irq(iommu->dev, iommu->irq, iommu); 881 + for (i = 0; i < iommu->num_irq; i++) 882 + devm_free_irq(iommu->dev, iommu->irq[i], iommu); 889 883 890 884 iommu->domain = NULL; 891 885 ··· 1016 1008 ret = of_parse_phandle_with_args(np, "iommus", "#iommu-cells", 0, 1017 1009 &args); 1018 1010 if (ret) { 1019 - dev_err(dev, "of_parse_phandle_with_args(%s) => %d\n", 1020 - np->full_name, ret); 1011 + dev_err(dev, "of_parse_phandle_with_args(%pOF) => %d\n", 1012 + np, ret); 1021 1013 return ret; 1022 1014 } 1023 1015 if (args.args_count != 0) { 1024 - dev_err(dev, "incorrect number of iommu params found for %s (found %d, expected 0)\n", 1025 - args.np->full_name, args.args_count); 1016 + dev_err(dev, "incorrect number of iommu params found for %pOF (found %d, expected 0)\n", 1017 + args.np, args.args_count); 1026 1018 return -EINVAL; 1027 1019 } 1028 1020 1029 1021 pd = of_find_device_by_node(args.np); 1030 1022 of_node_put(args.np); 1031 1023 if (!pd) { 1032 - dev_err(dev, "iommu %s not found\n", args.np->full_name); 1024 + dev_err(dev, "iommu %pOF not found\n", args.np); 1033 1025 return -EPROBE_DEFER; 1034 1026 } 1035 1027 ··· 1165 1157 if (iommu->num_mmu == 0) 1166 1158 return PTR_ERR(iommu->bases[0]); 1167 1159 1168 - iommu->irq = platform_get_irq(pdev, 0); 1169 - if (iommu->irq < 0) { 1170 - dev_err(dev, "Failed to get IRQ, %d\n", iommu->irq); 1160 + iommu->num_irq = platform_irq_count(pdev); 1161 + if (iommu->num_irq < 0) 1162 + return iommu->num_irq; 1163 + if (iommu->num_irq == 0) 1171 1164 return -ENXIO; 1165 + 1166 + iommu->irq = devm_kcalloc(dev, iommu->num_irq, sizeof(*iommu->irq), 1167 + GFP_KERNEL); 1168 + if (!iommu->irq) 1169 + return -ENOMEM; 1170 + 1171 + for (i = 0; i < iommu->num_irq; i++) { 1172 + iommu->irq[i] = platform_get_irq(pdev, i); 1173 + if (iommu->irq[i] < 0) { 1174 + dev_err(dev, "Failed to get IRQ, %d\n", iommu->irq[i]); 1175 + return -ENXIO; 1176 + } 1172 1177 } 1178 + 1179 + iommu->reset_disabled = device_property_read_bool(dev, 1180 + "rockchip,disable-mmu-reset"); 1173 1181 1174 1182 err = iommu_device_sysfs_add(&iommu->iommu, dev, NULL, dev_name(dev)); 1175 1183 if (err)
+36 -1
drivers/iommu/s390-iommu.c
··· 18 18 */ 19 19 #define S390_IOMMU_PGSIZES (~0xFFFUL) 20 20 21 + static const struct iommu_ops s390_iommu_ops; 22 + 21 23 struct s390_domain { 22 24 struct iommu_domain domain; 23 25 struct list_head devices; ··· 168 166 static int s390_iommu_add_device(struct device *dev) 169 167 { 170 168 struct iommu_group *group = iommu_group_get_for_dev(dev); 169 + struct zpci_dev *zdev = to_pci_dev(dev)->sysdata; 171 170 172 171 if (IS_ERR(group)) 173 172 return PTR_ERR(group); 174 173 175 174 iommu_group_put(group); 175 + iommu_device_link(&zdev->iommu_dev, dev); 176 176 177 177 return 0; 178 178 } ··· 201 197 s390_iommu_detach_device(domain, dev); 202 198 } 203 199 200 + iommu_device_unlink(&zdev->iommu_dev, dev); 204 201 iommu_group_remove_device(dev); 205 202 } 206 203 ··· 332 327 return size; 333 328 } 334 329 335 - static struct iommu_ops s390_iommu_ops = { 330 + int zpci_init_iommu(struct zpci_dev *zdev) 331 + { 332 + int rc = 0; 333 + 334 + rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL, 335 + "s390-iommu.%08x", zdev->fid); 336 + if (rc) 337 + goto out_err; 338 + 339 + iommu_device_set_ops(&zdev->iommu_dev, &s390_iommu_ops); 340 + 341 + rc = iommu_device_register(&zdev->iommu_dev); 342 + if (rc) 343 + goto out_sysfs; 344 + 345 + return 0; 346 + 347 + out_sysfs: 348 + iommu_device_sysfs_remove(&zdev->iommu_dev); 349 + 350 + out_err: 351 + return rc; 352 + } 353 + 354 + void zpci_destroy_iommu(struct zpci_dev *zdev) 355 + { 356 + iommu_device_unregister(&zdev->iommu_dev); 357 + iommu_device_sysfs_remove(&zdev->iommu_dev); 358 + } 359 + 360 + static const struct iommu_ops s390_iommu_ops = { 336 361 .capable = s390_iommu_capable, 337 362 .domain_alloc = s390_domain_alloc, 338 363 .domain_free = s390_domain_free,
+45
drivers/iommu/tegra-gart.c
··· 61 61 struct list_head client; 62 62 spinlock_t client_lock; /* for client list */ 63 63 struct device *dev; 64 + 65 + struct iommu_device iommu; /* IOMMU Core handle */ 64 66 }; 65 67 66 68 struct gart_domain { ··· 336 334 return false; 337 335 } 338 336 337 + static int gart_iommu_add_device(struct device *dev) 338 + { 339 + struct iommu_group *group = iommu_group_get_for_dev(dev); 340 + 341 + if (IS_ERR(group)) 342 + return PTR_ERR(group); 343 + 344 + iommu_group_put(group); 345 + 346 + iommu_device_link(&gart_handle->iommu, dev); 347 + 348 + return 0; 349 + } 350 + 351 + static void gart_iommu_remove_device(struct device *dev) 352 + { 353 + iommu_group_remove_device(dev); 354 + iommu_device_unlink(&gart_handle->iommu, dev); 355 + } 356 + 339 357 static const struct iommu_ops gart_iommu_ops = { 340 358 .capable = gart_iommu_capable, 341 359 .domain_alloc = gart_iommu_domain_alloc, 342 360 .domain_free = gart_iommu_domain_free, 343 361 .attach_dev = gart_iommu_attach_dev, 344 362 .detach_dev = gart_iommu_detach_dev, 363 + .add_device = gart_iommu_add_device, 364 + .remove_device = gart_iommu_remove_device, 365 + .device_group = generic_device_group, 345 366 .map = gart_iommu_map, 346 367 .map_sg = default_iommu_map_sg, 347 368 .unmap = gart_iommu_unmap, ··· 403 378 struct resource *res, *res_remap; 404 379 void __iomem *gart_regs; 405 380 struct device *dev = &pdev->dev; 381 + int ret; 406 382 407 383 if (gart_handle) 408 384 return -EIO; ··· 428 402 if (!gart_regs) { 429 403 dev_err(dev, "failed to remap GART registers\n"); 430 404 return -ENXIO; 405 + } 406 + 407 + ret = iommu_device_sysfs_add(&gart->iommu, &pdev->dev, NULL, 408 + dev_name(&pdev->dev)); 409 + if (ret) { 410 + dev_err(dev, "Failed to register IOMMU in sysfs\n"); 411 + return ret; 412 + } 413 + 414 + iommu_device_set_ops(&gart->iommu, &gart_iommu_ops); 415 + 416 + ret = iommu_device_register(&gart->iommu); 417 + if (ret) { 418 + dev_err(dev, "Failed to register IOMMU\n"); 419 + iommu_device_sysfs_remove(&gart->iommu); 420 + return ret; 431 421 } 432 422 433 423 gart->dev = &pdev->dev; ··· 471 429 static int tegra_gart_remove(struct platform_device *pdev) 472 430 { 473 431 struct gart_device *gart = platform_get_drvdata(pdev); 432 + 433 + iommu_device_unregister(&gart->iommu); 434 + iommu_device_sysfs_remove(&gart->iommu); 474 435 475 436 writel(0, gart->regs + GART_CONFIG); 476 437 if (gart->savedata)
+39 -2
drivers/iommu/tegra-smmu.c
··· 36 36 struct list_head list; 37 37 38 38 struct dentry *debugfs; 39 + 40 + struct iommu_device iommu; /* IOMMU Core code handle */ 39 41 }; 40 42 41 43 struct tegra_smmu_as { ··· 706 704 static int tegra_smmu_add_device(struct device *dev) 707 705 { 708 706 struct device_node *np = dev->of_node; 707 + struct iommu_group *group; 709 708 struct of_phandle_args args; 710 709 unsigned int index = 0; 711 710 ··· 722 719 * first match. 723 720 */ 724 721 dev->archdata.iommu = smmu; 722 + 723 + iommu_device_link(&smmu->iommu, dev); 724 + 725 725 break; 726 726 } 727 727 728 728 index++; 729 729 } 730 730 731 + group = iommu_group_get_for_dev(dev); 732 + if (IS_ERR(group)) 733 + return PTR_ERR(group); 734 + 735 + iommu_group_put(group); 736 + 731 737 return 0; 732 738 } 733 739 734 740 static void tegra_smmu_remove_device(struct device *dev) 735 741 { 742 + struct tegra_smmu *smmu = dev->archdata.iommu; 743 + 744 + if (smmu) 745 + iommu_device_unlink(&smmu->iommu, dev); 746 + 736 747 dev->archdata.iommu = NULL; 748 + iommu_group_remove_device(dev); 737 749 } 738 750 739 751 static const struct iommu_ops tegra_smmu_ops = { ··· 759 741 .detach_dev = tegra_smmu_detach_dev, 760 742 .add_device = tegra_smmu_add_device, 761 743 .remove_device = tegra_smmu_remove_device, 744 + .device_group = generic_device_group, 762 745 .map = tegra_smmu_map, 763 746 .unmap = tegra_smmu_unmap, 764 747 .map_sg = default_iommu_map_sg, ··· 949 930 950 931 tegra_smmu_ahb_enable(); 951 932 952 - err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops); 953 - if (err < 0) 933 + err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev)); 934 + if (err) 954 935 return ERR_PTR(err); 936 + 937 + iommu_device_set_ops(&smmu->iommu, &tegra_smmu_ops); 938 + 939 + err = iommu_device_register(&smmu->iommu); 940 + if (err) { 941 + iommu_device_sysfs_remove(&smmu->iommu); 942 + return ERR_PTR(err); 943 + } 944 + 945 + err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops); 946 + if (err < 0) { 947 + iommu_device_unregister(&smmu->iommu); 948 + iommu_device_sysfs_remove(&smmu->iommu); 949 + return ERR_PTR(err); 950 + } 955 951 956 952 if (IS_ENABLED(CONFIG_DEBUG_FS)) 957 953 tegra_smmu_debugfs_init(smmu); ··· 976 942 977 943 void tegra_smmu_remove(struct tegra_smmu *smmu) 978 944 { 945 + iommu_device_unregister(&smmu->iommu); 946 + iommu_device_sysfs_remove(&smmu->iommu); 947 + 979 948 if (IS_ENABLED(CONFIG_DEBUG_FS)) 980 949 tegra_smmu_debugfs_exit(smmu); 981 950 }
+73 -23
drivers/memory/mtk-smi.c
··· 16 16 #include <linux/device.h> 17 17 #include <linux/err.h> 18 18 #include <linux/io.h> 19 + #include <linux/module.h> 19 20 #include <linux/of.h> 20 21 #include <linux/of_platform.h> 21 22 #include <linux/platform_device.h> ··· 24 23 #include <soc/mediatek/smi.h> 25 24 #include <dt-bindings/memory/mt2701-larb-port.h> 26 25 26 + /* mt8173 */ 27 27 #define SMI_LARB_MMU_EN 0xf00 28 + 29 + /* mt2701 */ 28 30 #define REG_SMI_SECUR_CON_BASE 0x5c0 29 31 30 32 /* every register control 8 port, register offset 0x4 */ ··· 45 41 /* mt2701 domain should be set to 3 */ 46 42 #define SMI_SECUR_CON_VAL_DOMAIN(id) (0x3 << ((((id) & 0x7) << 2) + 1)) 47 43 44 + /* mt2712 */ 45 + #define SMI_LARB_NONSEC_CON(id) (0x380 + ((id) * 4)) 46 + #define F_MMU_EN BIT(0) 47 + 48 48 struct mtk_smi_larb_gen { 49 + bool need_larbid; 49 50 int port_in_larb[MTK_LARB_NR_MAX + 1]; 50 51 void (*config_port)(struct device *); 51 52 }; ··· 157 148 struct mtk_smi_iommu *smi_iommu = data; 158 149 unsigned int i; 159 150 151 + if (larb->larb_gen->need_larbid) { 152 + larb->mmu = &smi_iommu->larb_imu[larb->larbid].mmu; 153 + return 0; 154 + } 155 + 156 + /* 157 + * If there is no larbid property, Loop to find the corresponding 158 + * iommu information. 159 + */ 160 160 for (i = 0; i < smi_iommu->larb_nr; i++) { 161 161 if (dev == smi_iommu->larb_imu[i].dev) { 162 162 /* The 'mmu' may be updated in iommu-attach/detach. */ ··· 176 158 return -ENODEV; 177 159 } 178 160 179 - static void mtk_smi_larb_config_port(struct device *dev) 161 + static void mtk_smi_larb_config_port_mt2712(struct device *dev) 162 + { 163 + struct mtk_smi_larb *larb = dev_get_drvdata(dev); 164 + u32 reg; 165 + int i; 166 + 167 + /* 168 + * larb 8/9 is the bdpsys larb, the iommu_en is enabled defaultly. 169 + * Don't need to set it again. 170 + */ 171 + if (larb->larbid == 8 || larb->larbid == 9) 172 + return; 173 + 174 + for_each_set_bit(i, (unsigned long *)larb->mmu, 32) { 175 + reg = readl_relaxed(larb->base + SMI_LARB_NONSEC_CON(i)); 176 + reg |= F_MMU_EN; 177 + writel(reg, larb->base + SMI_LARB_NONSEC_CON(i)); 178 + } 179 + } 180 + 181 + static void mtk_smi_larb_config_port_mt8173(struct device *dev) 180 182 { 181 183 struct mtk_smi_larb *larb = dev_get_drvdata(dev); 182 184 183 185 writel(*larb->mmu, larb->base + SMI_LARB_MMU_EN); 184 186 } 185 - 186 187 187 188 static void mtk_smi_larb_config_port_gen1(struct device *dev) 188 189 { ··· 247 210 248 211 static const struct mtk_smi_larb_gen mtk_smi_larb_mt8173 = { 249 212 /* mt8173 do not need the port in larb */ 250 - .config_port = mtk_smi_larb_config_port, 213 + .config_port = mtk_smi_larb_config_port_mt8173, 251 214 }; 252 215 253 216 static const struct mtk_smi_larb_gen mtk_smi_larb_mt2701 = { 217 + .need_larbid = true, 254 218 .port_in_larb = { 255 219 LARB0_PORT_OFFSET, LARB1_PORT_OFFSET, 256 220 LARB2_PORT_OFFSET, LARB3_PORT_OFFSET 257 221 }, 258 222 .config_port = mtk_smi_larb_config_port_gen1, 223 + }; 224 + 225 + static const struct mtk_smi_larb_gen mtk_smi_larb_mt2712 = { 226 + .need_larbid = true, 227 + .config_port = mtk_smi_larb_config_port_mt2712, 259 228 }; 260 229 261 230 static const struct of_device_id mtk_smi_larb_of_ids[] = { ··· 273 230 .compatible = "mediatek,mt2701-smi-larb", 274 231 .data = &mtk_smi_larb_mt2701 275 232 }, 233 + { 234 + .compatible = "mediatek,mt2712-smi-larb", 235 + .data = &mtk_smi_larb_mt2712 236 + }, 276 237 {} 277 238 }; 278 239 ··· 287 240 struct device *dev = &pdev->dev; 288 241 struct device_node *smi_node; 289 242 struct platform_device *smi_pdev; 290 - const struct of_device_id *of_id; 291 - 292 - if (!dev->pm_domain) 293 - return -EPROBE_DEFER; 294 - 295 - of_id = of_match_node(mtk_smi_larb_of_ids, pdev->dev.of_node); 296 - if (!of_id) 297 - return -EINVAL; 243 + int err; 298 244 299 245 larb = devm_kzalloc(dev, sizeof(*larb), GFP_KERNEL); 300 246 if (!larb) 301 247 return -ENOMEM; 302 248 303 - larb->larb_gen = of_id->data; 249 + larb->larb_gen = of_device_get_match_data(dev); 304 250 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 305 251 larb->base = devm_ioremap_resource(dev, res); 306 252 if (IS_ERR(larb->base)) ··· 308 268 return PTR_ERR(larb->smi.clk_smi); 309 269 larb->smi.dev = dev; 310 270 271 + if (larb->larb_gen->need_larbid) { 272 + err = of_property_read_u32(dev->of_node, "mediatek,larb-id", 273 + &larb->larbid); 274 + if (err) { 275 + dev_err(dev, "missing larbid property\n"); 276 + return err; 277 + } 278 + } 279 + 311 280 smi_node = of_parse_phandle(dev->of_node, "mediatek,smi", 0); 312 281 if (!smi_node) 313 282 return -EINVAL; ··· 324 275 smi_pdev = of_find_device_by_node(smi_node); 325 276 of_node_put(smi_node); 326 277 if (smi_pdev) { 278 + if (!platform_get_drvdata(smi_pdev)) 279 + return -EPROBE_DEFER; 327 280 larb->smi_common_dev = &smi_pdev->dev; 328 281 } else { 329 282 dev_err(dev, "Failed to get the smi_common device\n"); ··· 362 311 .compatible = "mediatek,mt2701-smi-common", 363 312 .data = (void *)MTK_SMI_GEN1 364 313 }, 314 + { 315 + .compatible = "mediatek,mt2712-smi-common", 316 + .data = (void *)MTK_SMI_GEN2 317 + }, 365 318 {} 366 319 }; 367 320 ··· 374 319 struct device *dev = &pdev->dev; 375 320 struct mtk_smi *common; 376 321 struct resource *res; 377 - const struct of_device_id *of_id; 378 322 enum mtk_smi_gen smi_gen; 379 - 380 - if (!dev->pm_domain) 381 - return -EPROBE_DEFER; 323 + int ret; 382 324 383 325 common = devm_kzalloc(dev, sizeof(*common), GFP_KERNEL); 384 326 if (!common) ··· 390 338 if (IS_ERR(common->clk_smi)) 391 339 return PTR_ERR(common->clk_smi); 392 340 393 - of_id = of_match_node(mtk_smi_common_of_ids, pdev->dev.of_node); 394 - if (!of_id) 395 - return -EINVAL; 396 - 397 341 /* 398 342 * for mtk smi gen 1, we need to get the ao(always on) base to config 399 343 * m4u port, and we need to enable the aync clock for transform the smi 400 344 * clock into emi clock domain, but for mtk smi gen2, there's no smi ao 401 345 * base. 402 346 */ 403 - smi_gen = (enum mtk_smi_gen)of_id->data; 347 + smi_gen = (enum mtk_smi_gen)of_device_get_match_data(dev); 404 348 if (smi_gen == MTK_SMI_GEN1) { 405 349 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 406 350 common->smi_ao_base = devm_ioremap_resource(dev, res); ··· 407 359 if (IS_ERR(common->clk_async)) 408 360 return PTR_ERR(common->clk_async); 409 361 410 - clk_prepare_enable(common->clk_async); 362 + ret = clk_prepare_enable(common->clk_async); 363 + if (ret) 364 + return ret; 411 365 } 412 366 pm_runtime_enable(dev); 413 367 platform_set_drvdata(pdev, common); ··· 453 403 return ret; 454 404 } 455 405 456 - subsys_initcall(mtk_smi_init); 406 + module_init(mtk_smi_init);
-4
include/dt-bindings/memory/mt8173-larb-port.h
··· 15 15 #define __DTS_IOMMU_PORT_MT8173_H 16 16 17 17 #define MTK_M4U_ID(larb, port) (((larb) << 5) | (port)) 18 - /* Local arbiter ID */ 19 - #define MTK_M4U_TO_LARB(id) (((id) >> 5) & 0x7) 20 - /* PortID within the local arbiter */ 21 - #define MTK_M4U_TO_PORT(id) ((id) & 0x1f) 22 18 23 19 #define M4U_LARB0_ID 0 24 20 #define M4U_LARB1_ID 1
+52 -3
include/linux/iommu.h
··· 167 167 * @map: map a physically contiguous memory region to an iommu domain 168 168 * @unmap: unmap a physically contiguous memory region from an iommu domain 169 169 * @map_sg: map a scatter-gather list of physically contiguous memory chunks 170 + * @flush_tlb_all: Synchronously flush all hardware TLBs for this domain 171 + * @tlb_range_add: Add a given iova range to the flush queue for this domain 172 + * @tlb_sync: Flush all queued ranges from the hardware TLBs and empty flush 173 + * queue 170 174 * to an iommu domain 171 175 * @iova_to_phys: translate iova to physical address 172 176 * @add_device: add device to iommu grouping ··· 203 199 size_t size); 204 200 size_t (*map_sg)(struct iommu_domain *domain, unsigned long iova, 205 201 struct scatterlist *sg, unsigned int nents, int prot); 202 + void (*flush_iotlb_all)(struct iommu_domain *domain); 203 + void (*iotlb_range_add)(struct iommu_domain *domain, 204 + unsigned long iova, size_t size); 205 + void (*iotlb_sync)(struct iommu_domain *domain); 206 206 phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova); 207 207 int (*add_device)(struct device *dev); 208 208 void (*remove_device)(struct device *dev); ··· 233 225 u32 (*domain_get_windows)(struct iommu_domain *domain); 234 226 235 227 int (*of_xlate)(struct device *dev, struct of_phandle_args *args); 228 + bool (*is_attach_deferred)(struct iommu_domain *domain, struct device *dev); 236 229 237 230 unsigned long pgsize_bitmap; 238 231 }; ··· 300 291 extern int iommu_map(struct iommu_domain *domain, unsigned long iova, 301 292 phys_addr_t paddr, size_t size, int prot); 302 293 extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, 303 - size_t size); 294 + size_t size); 295 + extern size_t iommu_unmap_fast(struct iommu_domain *domain, 296 + unsigned long iova, size_t size); 304 297 extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, 305 298 struct scatterlist *sg,unsigned int nents, 306 299 int prot); ··· 358 347 359 348 extern int report_iommu_fault(struct iommu_domain *domain, struct device *dev, 360 349 unsigned long iova, int flags); 350 + 351 + static inline void iommu_flush_tlb_all(struct iommu_domain *domain) 352 + { 353 + if (domain->ops->flush_iotlb_all) 354 + domain->ops->flush_iotlb_all(domain); 355 + } 356 + 357 + static inline void iommu_tlb_range_add(struct iommu_domain *domain, 358 + unsigned long iova, size_t size) 359 + { 360 + if (domain->ops->iotlb_range_add) 361 + domain->ops->iotlb_range_add(domain, iova, size); 362 + } 363 + 364 + static inline void iommu_tlb_sync(struct iommu_domain *domain) 365 + { 366 + if (domain->ops->iotlb_sync) 367 + domain->ops->iotlb_sync(domain); 368 + } 361 369 362 370 static inline size_t iommu_map_sg(struct iommu_domain *domain, 363 371 unsigned long iova, struct scatterlist *sg, ··· 460 430 } 461 431 462 432 static inline int iommu_map(struct iommu_domain *domain, unsigned long iova, 463 - phys_addr_t paddr, int gfp_order, int prot) 433 + phys_addr_t paddr, size_t size, int prot) 464 434 { 465 435 return -ENODEV; 466 436 } 467 437 468 438 static inline int iommu_unmap(struct iommu_domain *domain, unsigned long iova, 469 - int gfp_order) 439 + size_t size) 440 + { 441 + return -ENODEV; 442 + } 443 + 444 + static inline int iommu_unmap_fast(struct iommu_domain *domain, unsigned long iova, 445 + int gfp_order) 470 446 { 471 447 return -ENODEV; 472 448 } ··· 482 446 unsigned int nents, int prot) 483 447 { 484 448 return -ENODEV; 449 + } 450 + 451 + static inline void iommu_flush_tlb_all(struct iommu_domain *domain) 452 + { 453 + } 454 + 455 + static inline void iommu_tlb_range_add(struct iommu_domain *domain, 456 + unsigned long iova, size_t size) 457 + { 458 + } 459 + 460 + static inline void iommu_tlb_sync(struct iommu_domain *domain) 461 + { 485 462 } 486 463 487 464 static inline int iommu_domain_window_enable(struct iommu_domain *domain,
+67
include/linux/iova.h
··· 14 14 #include <linux/types.h> 15 15 #include <linux/kernel.h> 16 16 #include <linux/rbtree.h> 17 + #include <linux/atomic.h> 17 18 #include <linux/dma-mapping.h> 18 19 19 20 /* iova structure */ ··· 37 36 struct iova_cpu_rcache __percpu *cpu_rcaches; 38 37 }; 39 38 39 + struct iova_domain; 40 + 41 + /* Call-Back from IOVA code into IOMMU drivers */ 42 + typedef void (* iova_flush_cb)(struct iova_domain *domain); 43 + 44 + /* Destructor for per-entry data */ 45 + typedef void (* iova_entry_dtor)(unsigned long data); 46 + 47 + /* Number of entries per Flush Queue */ 48 + #define IOVA_FQ_SIZE 256 49 + 50 + /* Timeout (in ms) after which entries are flushed from the Flush-Queue */ 51 + #define IOVA_FQ_TIMEOUT 10 52 + 53 + /* Flush Queue entry for defered flushing */ 54 + struct iova_fq_entry { 55 + unsigned long iova_pfn; 56 + unsigned long pages; 57 + unsigned long data; 58 + u64 counter; /* Flush counter when this entrie was added */ 59 + }; 60 + 61 + /* Per-CPU Flush Queue structure */ 62 + struct iova_fq { 63 + struct iova_fq_entry entries[IOVA_FQ_SIZE]; 64 + unsigned head, tail; 65 + spinlock_t lock; 66 + }; 67 + 40 68 /* holds all the iova translations for a domain */ 41 69 struct iova_domain { 42 70 spinlock_t iova_rbtree_lock; /* Lock to protect update of rbtree */ ··· 75 45 unsigned long start_pfn; /* Lower limit for this domain */ 76 46 unsigned long dma_32bit_pfn; 77 47 struct iova_rcache rcaches[IOVA_RANGE_CACHE_MAX_SIZE]; /* IOVA range caches */ 48 + 49 + iova_flush_cb flush_cb; /* Call-Back function to flush IOMMU 50 + TLBs */ 51 + 52 + iova_entry_dtor entry_dtor; /* IOMMU driver specific destructor for 53 + iova entry */ 54 + 55 + struct iova_fq __percpu *fq; /* Flush Queue */ 56 + 57 + atomic64_t fq_flush_start_cnt; /* Number of TLB flushes that 58 + have been started */ 59 + 60 + atomic64_t fq_flush_finish_cnt; /* Number of TLB flushes that 61 + have been finished */ 62 + 63 + struct timer_list fq_timer; /* Timer to regularily empty the 64 + flush-queues */ 65 + atomic_t fq_timer_on; /* 1 when timer is active, 0 66 + when not */ 78 67 }; 79 68 80 69 static inline unsigned long iova_size(struct iova *iova) ··· 144 95 bool size_aligned); 145 96 void free_iova_fast(struct iova_domain *iovad, unsigned long pfn, 146 97 unsigned long size); 98 + void queue_iova(struct iova_domain *iovad, 99 + unsigned long pfn, unsigned long pages, 100 + unsigned long data); 147 101 unsigned long alloc_iova_fast(struct iova_domain *iovad, unsigned long size, 148 102 unsigned long limit_pfn); 149 103 struct iova *reserve_iova(struct iova_domain *iovad, unsigned long pfn_lo, ··· 154 102 void copy_reserved_iova(struct iova_domain *from, struct iova_domain *to); 155 103 void init_iova_domain(struct iova_domain *iovad, unsigned long granule, 156 104 unsigned long start_pfn, unsigned long pfn_32bit); 105 + int init_iova_flush_queue(struct iova_domain *iovad, 106 + iova_flush_cb flush_cb, iova_entry_dtor entry_dtor); 157 107 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn); 158 108 void put_iova_domain(struct iova_domain *iovad); 159 109 struct iova *split_and_remove_iova(struct iova_domain *iovad, ··· 202 148 { 203 149 } 204 150 151 + static inline void queue_iova(struct iova_domain *iovad, 152 + unsigned long pfn, unsigned long pages, 153 + unsigned long data) 154 + { 155 + } 156 + 205 157 static inline unsigned long alloc_iova_fast(struct iova_domain *iovad, 206 158 unsigned long size, 207 159 unsigned long limit_pfn) ··· 232 172 unsigned long start_pfn, 233 173 unsigned long pfn_32bit) 234 174 { 175 + } 176 + 177 + static inline int init_iova_flush_queue(struct iova_domain *iovad, 178 + iova_flush_cb flush_cb, 179 + iova_entry_dtor entry_dtor) 180 + { 181 + return -ENODEV; 235 182 } 236 183 237 184 static inline struct iova *find_iova(struct iova_domain *iovad,
+1 -1
include/soc/mediatek/smi.h
··· 19 19 20 20 #ifdef CONFIG_MTK_SMI 21 21 22 - #define MTK_LARB_NR_MAX 8 22 + #define MTK_LARB_NR_MAX 16 23 23 24 24 #define MTK_SMI_MMU_EN(port) BIT(port) 25 25