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

Merge branches 'arm/exynos', 'arm/renesas', 'arm/rockchip', 'arm/omap', 'arm/mediatek', 'arm/tegra', 'arm/qcom', 'arm/smmu', 'ppc/pamu', 'x86/vt-d', 'x86/amd', 's390' and 'core' into next

+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
··· 10940 10940 S: Supported 10941 10941 F: arch/hexagon/ 10942 10942 10943 + QUALCOMM IOMMU 10944 + M: Rob Clark <robdclark@gmail.com> 10945 + L: iommu@lists.linux-foundation.org 10946 + L: linux-arm-msm@vger.kernel.org 10947 + S: Maintained 10948 + F: drivers/iommu/qcom_iommu.c 10949 + 10943 10950 QUALCOMM VENUS VIDEO ACCELERATOR DRIVER 10944 10951 M: Stanimir Varbanov <stanimir.varbanov@linaro.org> 10945 10952 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
··· 776 776 777 777 zpci_exit_slot(zdev); 778 778 zpci_cleanup_bus_resources(zdev); 779 + zpci_destroy_iommu(zdev); 779 780 zpci_free_domain(zdev); 780 781 781 782 spin_lock(&zpci_list_lock); ··· 849 848 if (rc) 850 849 goto out; 851 850 851 + rc = zpci_init_iommu(zdev); 852 + if (rc) 853 + goto out_free; 854 + 852 855 mutex_init(&zdev->lock); 853 856 if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 854 857 rc = zpci_enable_device(zdev); 855 858 if (rc) 856 - goto out_free; 859 + goto out_destroy_iommu; 857 860 } 858 861 rc = zpci_scan_bus(zdev); 859 862 if (rc) ··· 874 869 out_disable: 875 870 if (zdev->state == ZPCI_FN_STATE_ONLINE) 876 871 zpci_disable_device(zdev); 872 + out_destroy_iommu: 873 + zpci_destroy_iommu(zdev); 877 874 out_free: 878 875 zpci_free_domain(zdev); 879 876 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. ··· 1105 1165 return iommu_queue_command(iommu, &cmd); 1106 1166 } 1107 1167 1108 - static void iommu_flush_dte_all(struct amd_iommu *iommu) 1168 + static void amd_iommu_flush_dte_all(struct amd_iommu *iommu) 1109 1169 { 1110 1170 u32 devid; 1111 1171 ··· 1119 1179 * This function uses heavy locking and may disable irqs for some time. But 1120 1180 * this is no issue because it is only called during resume. 1121 1181 */ 1122 - static void iommu_flush_tlb_all(struct amd_iommu *iommu) 1182 + static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu) 1123 1183 { 1124 1184 u32 dom_id; 1125 1185 ··· 1133 1193 iommu_completion_wait(iommu); 1134 1194 } 1135 1195 1136 - static void iommu_flush_all(struct amd_iommu *iommu) 1196 + static void amd_iommu_flush_all(struct amd_iommu *iommu) 1137 1197 { 1138 1198 struct iommu_cmd cmd; 1139 1199 ··· 1152 1212 iommu_queue_command(iommu, &cmd); 1153 1213 } 1154 1214 1155 - static void iommu_flush_irt_all(struct amd_iommu *iommu) 1215 + static void amd_iommu_flush_irt_all(struct amd_iommu *iommu) 1156 1216 { 1157 1217 u32 devid; 1158 1218 ··· 1165 1225 void iommu_flush_all_caches(struct amd_iommu *iommu) 1166 1226 { 1167 1227 if (iommu_feature(iommu, FEATURE_IA)) { 1168 - iommu_flush_all(iommu); 1228 + amd_iommu_flush_all(iommu); 1169 1229 } else { 1170 - iommu_flush_dte_all(iommu); 1171 - iommu_flush_irt_all(iommu); 1172 - iommu_flush_tlb_all(iommu); 1230 + amd_iommu_flush_dte_all(iommu); 1231 + amd_iommu_flush_irt_all(iommu); 1232 + amd_iommu_flush_tlb_all(iommu); 1173 1233 } 1174 1234 } 1175 1235 ··· 1477 1537 1478 1538 if (count > 1) { 1479 1539 __pte = PAGE_SIZE_PTE(phys_addr, page_size); 1480 - __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC; 1540 + __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_PR | IOMMU_PTE_FC; 1481 1541 } else 1482 - __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC; 1542 + __pte = phys_addr | IOMMU_PTE_PR | IOMMU_PTE_FC; 1483 1543 1484 1544 if (prot & IOMMU_PROT_IR) 1485 1545 __pte |= IOMMU_PTE_IR; ··· 1728 1788 free_page((unsigned long)domain->gcr3_tbl); 1729 1789 } 1730 1790 1731 - static void dma_ops_domain_free_flush_queue(struct dma_ops_domain *dom) 1732 - { 1733 - int cpu; 1734 - 1735 - for_each_possible_cpu(cpu) { 1736 - struct flush_queue *queue; 1737 - 1738 - queue = per_cpu_ptr(dom->flush_queue, cpu); 1739 - kfree(queue->entries); 1740 - } 1741 - 1742 - free_percpu(dom->flush_queue); 1743 - 1744 - dom->flush_queue = NULL; 1745 - } 1746 - 1747 - static int dma_ops_domain_alloc_flush_queue(struct dma_ops_domain *dom) 1748 - { 1749 - int cpu; 1750 - 1751 - atomic64_set(&dom->flush_start_cnt, 0); 1752 - atomic64_set(&dom->flush_finish_cnt, 0); 1753 - 1754 - dom->flush_queue = alloc_percpu(struct flush_queue); 1755 - if (!dom->flush_queue) 1756 - return -ENOMEM; 1757 - 1758 - /* First make sure everything is cleared */ 1759 - for_each_possible_cpu(cpu) { 1760 - struct flush_queue *queue; 1761 - 1762 - queue = per_cpu_ptr(dom->flush_queue, cpu); 1763 - queue->head = 0; 1764 - queue->tail = 0; 1765 - queue->entries = NULL; 1766 - } 1767 - 1768 - /* Now start doing the allocation */ 1769 - for_each_possible_cpu(cpu) { 1770 - struct flush_queue *queue; 1771 - 1772 - queue = per_cpu_ptr(dom->flush_queue, cpu); 1773 - queue->entries = kzalloc(FLUSH_QUEUE_SIZE * sizeof(*queue->entries), 1774 - GFP_KERNEL); 1775 - if (!queue->entries) { 1776 - dma_ops_domain_free_flush_queue(dom); 1777 - return -ENOMEM; 1778 - } 1779 - 1780 - spin_lock_init(&queue->lock); 1781 - } 1782 - 1783 - return 0; 1784 - } 1785 - 1786 1791 static void dma_ops_domain_flush_tlb(struct dma_ops_domain *dom) 1787 1792 { 1788 - atomic64_inc(&dom->flush_start_cnt); 1789 1793 domain_flush_tlb(&dom->domain); 1790 1794 domain_flush_complete(&dom->domain); 1791 - atomic64_inc(&dom->flush_finish_cnt); 1792 1795 } 1793 1796 1794 - static inline bool queue_ring_full(struct flush_queue *queue) 1797 + static void iova_domain_flush_tlb(struct iova_domain *iovad) 1795 1798 { 1796 - assert_spin_locked(&queue->lock); 1799 + struct dma_ops_domain *dom; 1797 1800 1798 - return (((queue->tail + 1) % FLUSH_QUEUE_SIZE) == queue->head); 1799 - } 1800 - 1801 - #define queue_ring_for_each(i, q) \ 1802 - for (i = (q)->head; i != (q)->tail; i = (i + 1) % FLUSH_QUEUE_SIZE) 1803 - 1804 - static inline unsigned queue_ring_add(struct flush_queue *queue) 1805 - { 1806 - unsigned idx = queue->tail; 1807 - 1808 - assert_spin_locked(&queue->lock); 1809 - queue->tail = (idx + 1) % FLUSH_QUEUE_SIZE; 1810 - 1811 - return idx; 1812 - } 1813 - 1814 - static inline void queue_ring_remove_head(struct flush_queue *queue) 1815 - { 1816 - assert_spin_locked(&queue->lock); 1817 - queue->head = (queue->head + 1) % FLUSH_QUEUE_SIZE; 1818 - } 1819 - 1820 - static void queue_ring_free_flushed(struct dma_ops_domain *dom, 1821 - struct flush_queue *queue) 1822 - { 1823 - u64 counter = atomic64_read(&dom->flush_finish_cnt); 1824 - int idx; 1825 - 1826 - queue_ring_for_each(idx, queue) { 1827 - /* 1828 - * This assumes that counter values in the ring-buffer are 1829 - * monotonously rising. 1830 - */ 1831 - if (queue->entries[idx].counter >= counter) 1832 - break; 1833 - 1834 - free_iova_fast(&dom->iovad, 1835 - queue->entries[idx].iova_pfn, 1836 - queue->entries[idx].pages); 1837 - 1838 - queue_ring_remove_head(queue); 1839 - } 1840 - } 1841 - 1842 - static void queue_add(struct dma_ops_domain *dom, 1843 - unsigned long address, unsigned long pages) 1844 - { 1845 - struct flush_queue *queue; 1846 - unsigned long flags; 1847 - int idx; 1848 - 1849 - pages = __roundup_pow_of_two(pages); 1850 - address >>= PAGE_SHIFT; 1851 - 1852 - queue = get_cpu_ptr(dom->flush_queue); 1853 - spin_lock_irqsave(&queue->lock, flags); 1854 - 1855 - /* 1856 - * First remove the enries from the ring-buffer that are already 1857 - * flushed to make the below queue_ring_full() check less likely 1858 - */ 1859 - queue_ring_free_flushed(dom, queue); 1860 - 1861 - /* 1862 - * When ring-queue is full, flush the entries from the IOTLB so 1863 - * that we can free all entries with queue_ring_free_flushed() 1864 - * below. 1865 - */ 1866 - if (queue_ring_full(queue)) { 1867 - dma_ops_domain_flush_tlb(dom); 1868 - queue_ring_free_flushed(dom, queue); 1869 - } 1870 - 1871 - idx = queue_ring_add(queue); 1872 - 1873 - queue->entries[idx].iova_pfn = address; 1874 - queue->entries[idx].pages = pages; 1875 - queue->entries[idx].counter = atomic64_read(&dom->flush_start_cnt); 1876 - 1877 - spin_unlock_irqrestore(&queue->lock, flags); 1878 - 1879 - if (atomic_cmpxchg(&dom->flush_timer_on, 0, 1) == 0) 1880 - mod_timer(&dom->flush_timer, jiffies + msecs_to_jiffies(10)); 1881 - 1882 - put_cpu_ptr(dom->flush_queue); 1883 - } 1884 - 1885 - static void queue_flush_timeout(unsigned long data) 1886 - { 1887 - struct dma_ops_domain *dom = (struct dma_ops_domain *)data; 1888 - int cpu; 1889 - 1890 - atomic_set(&dom->flush_timer_on, 0); 1801 + dom = container_of(iovad, struct dma_ops_domain, iovad); 1891 1802 1892 1803 dma_ops_domain_flush_tlb(dom); 1893 - 1894 - for_each_possible_cpu(cpu) { 1895 - struct flush_queue *queue; 1896 - unsigned long flags; 1897 - 1898 - queue = per_cpu_ptr(dom->flush_queue, cpu); 1899 - spin_lock_irqsave(&queue->lock, flags); 1900 - queue_ring_free_flushed(dom, queue); 1901 - spin_unlock_irqrestore(&queue->lock, flags); 1902 - } 1903 1804 } 1904 1805 1905 1806 /* ··· 1753 1972 return; 1754 1973 1755 1974 del_domain_from_list(&dom->domain); 1756 - 1757 - if (timer_pending(&dom->flush_timer)) 1758 - del_timer(&dom->flush_timer); 1759 - 1760 - dma_ops_domain_free_flush_queue(dom); 1761 1975 1762 1976 put_iova_domain(&dom->iovad); 1763 1977 ··· 1789 2013 init_iova_domain(&dma_dom->iovad, PAGE_SIZE, 1790 2014 IOVA_START_PFN, DMA_32BIT_PFN); 1791 2015 1792 - /* Initialize reserved ranges */ 1793 - copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad); 1794 - 1795 - if (dma_ops_domain_alloc_flush_queue(dma_dom)) 2016 + if (init_iova_flush_queue(&dma_dom->iovad, iova_domain_flush_tlb, NULL)) 1796 2017 goto free_dma_dom; 1797 2018 1798 - setup_timer(&dma_dom->flush_timer, queue_flush_timeout, 1799 - (unsigned long)dma_dom); 1800 - 1801 - atomic_set(&dma_dom->flush_timer_on, 0); 2019 + /* Initialize reserved ranges */ 2020 + copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad); 1802 2021 1803 2022 add_domain_to_list(&dma_dom->domain); 1804 2023 ··· 1824 2053 1825 2054 pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK) 1826 2055 << DEV_ENTRY_MODE_SHIFT; 1827 - pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV; 2056 + pte_root |= DTE_FLAG_IR | DTE_FLAG_IW | DTE_FLAG_V | DTE_FLAG_TV; 1828 2057 1829 2058 flags = amd_iommu_dev_table[devid].data[1]; 1830 2059 ··· 1857 2086 flags |= tmp; 1858 2087 } 1859 2088 1860 - 1861 - flags &= ~(DTE_FLAG_SA | 0xffffULL); 2089 + flags &= ~DEV_DOMID_MASK; 1862 2090 flags |= domain->id; 1863 2091 1864 2092 amd_iommu_dev_table[devid].data[1] = flags; ··· 1867 2097 static void clear_dte_entry(u16 devid) 1868 2098 { 1869 2099 /* remove entry from the device table seen by the hardware */ 1870 - amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV; 2100 + amd_iommu_dev_table[devid].data[0] = DTE_FLAG_V | DTE_FLAG_TV; 1871 2101 amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK; 1872 2102 1873 2103 amd_iommu_apply_erratum_63(devid); ··· 2248 2478 static struct protection_domain *get_domain(struct device *dev) 2249 2479 { 2250 2480 struct protection_domain *domain; 2481 + struct iommu_domain *io_domain; 2251 2482 2252 2483 if (!check_device(dev)) 2253 2484 return ERR_PTR(-EINVAL); 2254 2485 2255 2486 domain = get_dev_data(dev)->domain; 2487 + if (domain == NULL && get_dev_data(dev)->defer_attach) { 2488 + get_dev_data(dev)->defer_attach = false; 2489 + io_domain = iommu_get_domain_for_dev(dev); 2490 + domain = to_pdomain(io_domain); 2491 + attach_device(dev, domain); 2492 + } 2493 + if (domain == NULL) 2494 + return ERR_PTR(-EBUSY); 2495 + 2256 2496 if (!dma_ops_domain(domain)) 2257 2497 return ERR_PTR(-EBUSY); 2258 2498 ··· 2308 2528 else 2309 2529 return 0; 2310 2530 } 2531 + 2311 2532 /* 2312 2533 * This function contains common code for mapping of a physically 2313 2534 * contiguous memory region into DMA address space. It is used by all ··· 2400 2619 domain_flush_tlb(&dma_dom->domain); 2401 2620 domain_flush_complete(&dma_dom->domain); 2402 2621 } else { 2403 - queue_add(dma_dom, dma_addr, pages); 2622 + pages = __roundup_pow_of_two(pages); 2623 + queue_iova(&dma_dom->iovad, dma_addr >> PAGE_SHIFT, pages, 0); 2404 2624 } 2405 2625 } 2406 2626 ··· 3155 3373 WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL); 3156 3374 } 3157 3375 3376 + static bool amd_iommu_is_attach_deferred(struct iommu_domain *domain, 3377 + struct device *dev) 3378 + { 3379 + struct iommu_dev_data *dev_data = dev->archdata.iommu; 3380 + return dev_data->defer_attach; 3381 + } 3382 + 3158 3383 const struct iommu_ops amd_iommu_ops = { 3159 3384 .capable = amd_iommu_capable, 3160 3385 .domain_alloc = amd_iommu_domain_alloc, ··· 3178 3389 .get_resv_regions = amd_iommu_get_resv_regions, 3179 3390 .put_resv_regions = amd_iommu_put_resv_regions, 3180 3391 .apply_resv_region = amd_iommu_apply_resv_region, 3392 + .is_attach_deferred = amd_iommu_is_attach_deferred, 3181 3393 .pgsize_bitmap = AMD_IOMMU_PGSIZES, 3182 3394 }; 3183 3395 ··· 3566 3776 *****************************************************************************/ 3567 3777 3568 3778 static struct irq_chip amd_ir_chip; 3569 - 3570 - #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6) 3571 - #define DTE_IRQ_REMAP_INTCTL (2ULL << 60) 3572 - #define DTE_IRQ_TABLE_LEN (8ULL << 1) 3573 - #define DTE_IRQ_REMAP_ENABLE 1ULL 3574 3779 3575 3780 static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table) 3576 3781 {
+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 <asm/pci-direct.h> 34 33 #include <asm/iommu.h> 35 34 #include <asm/gart.h> ··· 37 38 #include <asm/io_apic.h> 38 39 #include <asm/irq_remapping.h> 39 40 41 + #include <linux/crash_dump.h> 40 42 #include "amd_iommu_proto.h" 41 43 #include "amd_iommu_types.h" 42 44 #include "irq_remapping.h" ··· 196 196 * page table root pointer. 197 197 */ 198 198 struct dev_table_entry *amd_iommu_dev_table; 199 + /* 200 + * Pointer to a device table which the content of old device table 201 + * will be copied to. It's only be used in kdump kernel. 202 + */ 203 + static struct dev_table_entry *old_dev_tbl_cpy; 199 204 200 205 /* 201 206 * The alias table is a driver specific data structure which contains the ··· 214 209 * for a specific device. It is also indexed by the PCI device id. 215 210 */ 216 211 struct amd_iommu **amd_iommu_rlookup_table; 212 + EXPORT_SYMBOL(amd_iommu_rlookup_table); 217 213 218 214 /* 219 215 * This table is used to find the irq remapping table for a given device id ··· 263 257 static int amd_iommu_enable_interrupts(void); 264 258 static int __init iommu_go_to_state(enum iommu_init_state state); 265 259 static void init_device_table_dma(void); 260 + 261 + static bool amd_iommu_pre_enabled = true; 262 + 263 + bool translation_pre_enabled(struct amd_iommu *iommu) 264 + { 265 + return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED); 266 + } 267 + EXPORT_SYMBOL(translation_pre_enabled); 268 + 269 + static void clear_translation_pre_enabled(struct amd_iommu *iommu) 270 + { 271 + iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED; 272 + } 273 + 274 + static void init_translation_status(struct amd_iommu *iommu) 275 + { 276 + u32 ctrl; 277 + 278 + ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET); 279 + if (ctrl & (1<<CONTROL_IOMMU_EN)) 280 + iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED; 281 + } 266 282 267 283 static inline void update_last_devid(u16 devid) 268 284 { ··· 643 615 amd_iommu_reset_cmd_buffer(iommu); 644 616 } 645 617 618 + /* 619 + * This function disables the command buffer 620 + */ 621 + static void iommu_disable_command_buffer(struct amd_iommu *iommu) 622 + { 623 + iommu_feature_disable(iommu, CONTROL_CMDBUF_EN); 624 + } 625 + 646 626 static void __init free_command_buffer(struct amd_iommu *iommu) 647 627 { 648 628 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE)); ··· 681 645 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); 682 646 683 647 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN); 648 + } 649 + 650 + /* 651 + * This function disables the event log buffer 652 + */ 653 + static void iommu_disable_event_buffer(struct amd_iommu *iommu) 654 + { 655 + iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN); 684 656 } 685 657 686 658 static void __init free_event_buffer(struct amd_iommu *iommu) ··· 851 807 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit; 852 808 } 853 809 810 + 811 + static bool copy_device_table(void) 812 + { 813 + u64 int_ctl, int_tab_len, entry = 0, last_entry = 0; 814 + struct dev_table_entry *old_devtb = NULL; 815 + u32 lo, hi, devid, old_devtb_size; 816 + phys_addr_t old_devtb_phys; 817 + struct amd_iommu *iommu; 818 + u16 dom_id, dte_v, irq_v; 819 + gfp_t gfp_flag; 820 + u64 tmp; 821 + 822 + if (!amd_iommu_pre_enabled) 823 + return false; 824 + 825 + pr_warn("Translation is already enabled - trying to copy translation structures\n"); 826 + for_each_iommu(iommu) { 827 + /* All IOMMUs should use the same device table with the same size */ 828 + lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET); 829 + hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4); 830 + entry = (((u64) hi) << 32) + lo; 831 + if (last_entry && last_entry != entry) { 832 + pr_err("IOMMU:%d should use the same dev table as others!/n", 833 + iommu->index); 834 + return false; 835 + } 836 + last_entry = entry; 837 + 838 + old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12; 839 + if (old_devtb_size != dev_table_size) { 840 + pr_err("The device table size of IOMMU:%d is not expected!/n", 841 + iommu->index); 842 + return false; 843 + } 844 + } 845 + 846 + old_devtb_phys = entry & PAGE_MASK; 847 + if (old_devtb_phys >= 0x100000000ULL) { 848 + pr_err("The address of old device table is above 4G, not trustworthy!/n"); 849 + return false; 850 + } 851 + old_devtb = memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB); 852 + if (!old_devtb) 853 + return false; 854 + 855 + gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32; 856 + old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag, 857 + get_order(dev_table_size)); 858 + if (old_dev_tbl_cpy == NULL) { 859 + pr_err("Failed to allocate memory for copying old device table!/n"); 860 + return false; 861 + } 862 + 863 + for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) { 864 + old_dev_tbl_cpy[devid] = old_devtb[devid]; 865 + dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK; 866 + dte_v = old_devtb[devid].data[0] & DTE_FLAG_V; 867 + 868 + if (dte_v && dom_id) { 869 + old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0]; 870 + old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1]; 871 + __set_bit(dom_id, amd_iommu_pd_alloc_bitmap); 872 + /* If gcr3 table existed, mask it out */ 873 + if (old_devtb[devid].data[0] & DTE_FLAG_GV) { 874 + tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B; 875 + tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C; 876 + old_dev_tbl_cpy[devid].data[1] &= ~tmp; 877 + tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A; 878 + tmp |= DTE_FLAG_GV; 879 + old_dev_tbl_cpy[devid].data[0] &= ~tmp; 880 + } 881 + } 882 + 883 + irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE; 884 + int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK; 885 + int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK; 886 + if (irq_v && (int_ctl || int_tab_len)) { 887 + if ((int_ctl != DTE_IRQ_REMAP_INTCTL) || 888 + (int_tab_len != DTE_IRQ_TABLE_LEN)) { 889 + pr_err("Wrong old irq remapping flag: %#x\n", devid); 890 + return false; 891 + } 892 + 893 + old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2]; 894 + } 895 + } 896 + memunmap(old_devtb); 897 + 898 + return true; 899 + } 854 900 855 901 void amd_iommu_apply_erratum_63(u16 devid) 856 902 { ··· 1533 1399 1534 1400 iommu->int_enabled = false; 1535 1401 1402 + init_translation_status(iommu); 1403 + if (translation_pre_enabled(iommu) && !is_kdump_kernel()) { 1404 + iommu_disable(iommu); 1405 + clear_translation_pre_enabled(iommu); 1406 + pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n", 1407 + iommu->index); 1408 + } 1409 + if (amd_iommu_pre_enabled) 1410 + amd_iommu_pre_enabled = translation_pre_enabled(iommu); 1411 + 1536 1412 ret = init_iommu_from_acpi(iommu, h); 1537 1413 if (ret) 1538 1414 return ret; ··· 2036 1892 } 2037 1893 2038 1894 /* 2039 - * Init the device table to not allow DMA access for devices and 2040 - * suppress all page faults 1895 + * Init the device table to not allow DMA access for devices 2041 1896 */ 2042 1897 static void init_device_table_dma(void) 2043 1898 { ··· 2045 1902 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) { 2046 1903 set_dev_entry_bit(devid, DEV_ENTRY_VALID); 2047 1904 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION); 2048 - /* 2049 - * In kdump kernels in-flight DMA from the old kernel might 2050 - * cause IO_PAGE_FAULTs. There are no reports that a kdump 2051 - * actually failed because of that, so just disable fault 2052 - * reporting in the hardware to get rid of the messages 2053 - */ 2054 - if (is_kdump_kernel()) 2055 - set_dev_entry_bit(devid, DEV_ENTRY_NO_PAGE_FAULT); 2056 1905 } 2057 1906 } 2058 1907 ··· 2157 2022 #endif 2158 2023 } 2159 2024 2025 + static void early_enable_iommu(struct amd_iommu *iommu) 2026 + { 2027 + iommu_disable(iommu); 2028 + iommu_init_flags(iommu); 2029 + iommu_set_device_table(iommu); 2030 + iommu_enable_command_buffer(iommu); 2031 + iommu_enable_event_buffer(iommu); 2032 + iommu_set_exclusion_range(iommu); 2033 + iommu_enable_ga(iommu); 2034 + iommu_enable(iommu); 2035 + iommu_flush_all_caches(iommu); 2036 + } 2037 + 2160 2038 /* 2161 2039 * This function finally enables all IOMMUs found in the system after 2162 - * they have been initialized 2040 + * they have been initialized. 2041 + * 2042 + * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy 2043 + * the old content of device table entries. Not this case or copy failed, 2044 + * just continue as normal kernel does. 2163 2045 */ 2164 2046 static void early_enable_iommus(void) 2165 2047 { 2166 2048 struct amd_iommu *iommu; 2167 2049 2168 - for_each_iommu(iommu) { 2169 - iommu_disable(iommu); 2170 - iommu_init_flags(iommu); 2171 - iommu_set_device_table(iommu); 2172 - iommu_enable_command_buffer(iommu); 2173 - iommu_enable_event_buffer(iommu); 2174 - iommu_set_exclusion_range(iommu); 2175 - iommu_enable_ga(iommu); 2176 - iommu_enable(iommu); 2177 - iommu_flush_all_caches(iommu); 2050 + 2051 + if (!copy_device_table()) { 2052 + /* 2053 + * If come here because of failure in copying device table from old 2054 + * kernel with all IOMMUs enabled, print error message and try to 2055 + * free allocated old_dev_tbl_cpy. 2056 + */ 2057 + if (amd_iommu_pre_enabled) 2058 + pr_err("Failed to copy DEV table from previous kernel.\n"); 2059 + if (old_dev_tbl_cpy != NULL) 2060 + free_pages((unsigned long)old_dev_tbl_cpy, 2061 + get_order(dev_table_size)); 2062 + 2063 + for_each_iommu(iommu) { 2064 + clear_translation_pre_enabled(iommu); 2065 + early_enable_iommu(iommu); 2066 + } 2067 + } else { 2068 + pr_info("Copied DEV table from previous kernel.\n"); 2069 + free_pages((unsigned long)amd_iommu_dev_table, 2070 + get_order(dev_table_size)); 2071 + amd_iommu_dev_table = old_dev_tbl_cpy; 2072 + for_each_iommu(iommu) { 2073 + iommu_disable_command_buffer(iommu); 2074 + iommu_disable_event_buffer(iommu); 2075 + iommu_enable_command_buffer(iommu); 2076 + iommu_enable_event_buffer(iommu); 2077 + iommu_enable_ga(iommu); 2078 + iommu_set_device_table(iommu); 2079 + iommu_flush_all_caches(iommu); 2080 + } 2178 2081 } 2179 2082 2180 2083 #ifdef CONFIG_IRQ_REMAP ··· 2448 2275 2449 2276 /* Device table - directly used by all IOMMUs */ 2450 2277 ret = -ENOMEM; 2451 - amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2278 + amd_iommu_dev_table = (void *)__get_free_pages( 2279 + GFP_KERNEL | __GFP_ZERO | GFP_DMA32, 2452 2280 get_order(dev_table_size)); 2453 2281 if (amd_iommu_dev_table == NULL) 2454 2282 goto out; ··· 2499 2325 goto out; 2500 2326 2501 2327 /* Disable any previously enabled IOMMUs */ 2502 - disable_iommus(); 2328 + if (!is_kdump_kernel() || amd_iommu_disabled) 2329 + disable_iommus(); 2503 2330 2504 2331 if (amd_iommu_irq_remap) 2505 2332 amd_iommu_irq_remap = check_ioapic_information();
+2
drivers/iommu/amd_iommu_proto.h
··· 87 87 return !!(iommu->features & f); 88 88 } 89 89 90 + extern bool translation_pre_enabled(struct amd_iommu *iommu); 91 + extern struct iommu_dev_data *get_dev_data(struct device *dev); 90 92 #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) (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
··· 562 562 unsigned long flags; 563 563 struct fault *fault; 564 564 bool finish; 565 - u16 tag; 565 + u16 tag, devid; 566 566 int ret; 567 + struct iommu_dev_data *dev_data; 568 + struct pci_dev *pdev = NULL; 567 569 568 570 iommu_fault = data; 569 571 tag = iommu_fault->tag & 0x1ff; 570 572 finish = (iommu_fault->tag >> 9) & 1; 571 573 574 + devid = iommu_fault->device_id; 575 + pdev = pci_get_bus_and_slot(PCI_BUS_NUM(devid), devid & 0xff); 576 + if (!pdev) 577 + return -ENODEV; 578 + dev_data = get_dev_data(&pdev->dev); 579 + 580 + /* In kdump kernel pci dev is not initialized yet -> send INVALID */ 572 581 ret = NOTIFY_DONE; 582 + if (translation_pre_enabled(amd_iommu_rlookup_table[devid]) 583 + && dev_data->defer_attach) { 584 + amd_iommu_complete_ppr(pdev, iommu_fault->pasid, 585 + PPR_INVALID, tag); 586 + goto out; 587 + } 588 + 573 589 dev_state = get_device_state(iommu_fault->device_id); 574 590 if (dev_state == NULL) 575 591 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 ··· 949 974 return ret; 950 975 } 951 976 952 - static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn) 953 - { 954 - struct context_entry *context; 955 - unsigned long flags; 956 - 957 - spin_lock_irqsave(&iommu->lock, flags); 958 - context = iommu_context_addr(iommu, bus, devfn, 0); 959 - if (context) { 960 - context_clear_entry(context); 961 - __iommu_flush_cache(iommu, context, sizeof(*context)); 962 - } 963 - spin_unlock_irqrestore(&iommu->lock, flags); 964 - } 965 - 966 977 static void free_context_table(struct intel_iommu *iommu) 967 978 { 968 979 int i; ··· 1098 1137 } 1099 1138 1100 1139 static void dma_pte_free_level(struct dmar_domain *domain, int level, 1101 - struct dma_pte *pte, unsigned long pfn, 1102 - unsigned long start_pfn, unsigned long last_pfn) 1140 + int retain_level, struct dma_pte *pte, 1141 + unsigned long pfn, unsigned long start_pfn, 1142 + unsigned long last_pfn) 1103 1143 { 1104 1144 pfn = max(start_pfn, pfn); 1105 1145 pte = &pte[pfn_level_offset(pfn, level)]; ··· 1115 1153 level_pfn = pfn & level_mask(level); 1116 1154 level_pte = phys_to_virt(dma_pte_addr(pte)); 1117 1155 1118 - if (level > 2) 1119 - dma_pte_free_level(domain, level - 1, level_pte, 1120 - level_pfn, start_pfn, last_pfn); 1156 + if (level > 2) { 1157 + dma_pte_free_level(domain, level - 1, retain_level, 1158 + level_pte, level_pfn, start_pfn, 1159 + last_pfn); 1160 + } 1121 1161 1122 - /* If range covers entire pagetable, free it */ 1123 - if (!(start_pfn > level_pfn || 1162 + /* 1163 + * Free the page table if we're below the level we want to 1164 + * retain and the range covers the entire table. 1165 + */ 1166 + if (level < retain_level && !(start_pfn > level_pfn || 1124 1167 last_pfn < level_pfn + level_size(level) - 1)) { 1125 1168 dma_clear_pte(pte); 1126 1169 domain_flush_cache(domain, pte, sizeof(*pte)); ··· 1136 1169 } while (!first_pte_in_page(++pte) && pfn <= last_pfn); 1137 1170 } 1138 1171 1139 - /* clear last level (leaf) ptes and free page table pages. */ 1172 + /* 1173 + * clear last level (leaf) ptes and free page table pages below the 1174 + * level we wish to keep intact. 1175 + */ 1140 1176 static void dma_pte_free_pagetable(struct dmar_domain *domain, 1141 1177 unsigned long start_pfn, 1142 - unsigned long last_pfn) 1178 + unsigned long last_pfn, 1179 + int retain_level) 1143 1180 { 1144 1181 BUG_ON(!domain_pfn_supported(domain, start_pfn)); 1145 1182 BUG_ON(!domain_pfn_supported(domain, last_pfn)); ··· 1152 1181 dma_pte_clear_range(domain, start_pfn, last_pfn); 1153 1182 1154 1183 /* We don't need lock here; nobody else touches the iova range */ 1155 - dma_pte_free_level(domain, agaw_to_level(domain->agaw), 1184 + dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level, 1156 1185 domain->pgd, 0, start_pfn, last_pfn); 1157 1186 1158 1187 /* free pgd */ ··· 1278 1307 freelist = pg->freelist; 1279 1308 free_pgtable_page(page_address(pg)); 1280 1309 } 1310 + } 1311 + 1312 + static void iova_entry_free(unsigned long data) 1313 + { 1314 + struct page *freelist = (struct page *)data; 1315 + 1316 + dma_free_pagelist(freelist); 1281 1317 } 1282 1318 1283 1319 /* iommu handling */ ··· 1600 1622 addr, mask); 1601 1623 } 1602 1624 1625 + static void iommu_flush_iova(struct iova_domain *iovad) 1626 + { 1627 + struct dmar_domain *domain; 1628 + int idx; 1629 + 1630 + domain = container_of(iovad, struct dmar_domain, iovad); 1631 + 1632 + for_each_domain_iommu(idx, domain) { 1633 + struct intel_iommu *iommu = g_iommus[idx]; 1634 + u16 did = domain->iommu_did[iommu->seq_id]; 1635 + 1636 + iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); 1637 + 1638 + if (!cap_caching_mode(iommu->cap)) 1639 + iommu_flush_dev_iotlb(get_iommu_domain(iommu, did), 1640 + 0, MAX_AGAW_PFN_WIDTH); 1641 + } 1642 + } 1643 + 1603 1644 static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu) 1604 1645 { 1605 1646 u32 pmen; ··· 1929 1932 { 1930 1933 int adjust_width, agaw; 1931 1934 unsigned long sagaw; 1935 + int err; 1932 1936 1933 1937 init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN, 1934 1938 DMA_32BIT_PFN); 1939 + 1940 + err = init_iova_flush_queue(&domain->iovad, 1941 + iommu_flush_iova, iova_entry_free); 1942 + if (err) 1943 + return err; 1944 + 1935 1945 domain_reserve_special_ranges(domain); 1936 1946 1937 1947 /* calculate AGAW */ ··· 1989 1985 /* Domain 0 is reserved, so dont process it */ 1990 1986 if (!domain) 1991 1987 return; 1992 - 1993 - /* Flush any lazy unmaps that may reference this domain */ 1994 - if (!intel_iommu_strict) { 1995 - int cpu; 1996 - 1997 - for_each_possible_cpu(cpu) 1998 - flush_unmaps_timeout(cpu); 1999 - } 2000 1988 2001 1989 /* Remove associated devices and clear attached or cached domains */ 2002 1990 rcu_read_lock(); ··· 2273 2277 /* 2274 2278 * Ensure that old small page tables are 2275 2279 * removed to make room for superpage(s). 2280 + * We're adding new large pages, so make sure 2281 + * we don't remove their parent tables. 2276 2282 */ 2277 - dma_pte_free_pagetable(domain, iov_pfn, end_pfn); 2283 + dma_pte_free_pagetable(domain, iov_pfn, end_pfn, 2284 + largepage_lvl + 1); 2278 2285 } else { 2279 2286 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE; 2280 2287 } ··· 2350 2351 2351 2352 static void domain_context_clear_one(struct intel_iommu *iommu, u8 bus, u8 devfn) 2352 2353 { 2354 + unsigned long flags; 2355 + struct context_entry *context; 2356 + u16 did_old; 2357 + 2353 2358 if (!iommu) 2354 2359 return; 2355 2360 2356 - clear_context_table(iommu, bus, devfn); 2357 - iommu->flush.flush_context(iommu, 0, 0, 0, 2358 - DMA_CCMD_GLOBAL_INVL); 2359 - iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); 2361 + spin_lock_irqsave(&iommu->lock, flags); 2362 + context = iommu_context_addr(iommu, bus, devfn, 0); 2363 + if (!context) { 2364 + spin_unlock_irqrestore(&iommu->lock, flags); 2365 + return; 2366 + } 2367 + did_old = context_domain_id(context); 2368 + context_clear_entry(context); 2369 + __iommu_flush_cache(iommu, context, sizeof(*context)); 2370 + spin_unlock_irqrestore(&iommu->lock, flags); 2371 + iommu->flush.flush_context(iommu, 2372 + did_old, 2373 + (((u16)bus) << 8) | devfn, 2374 + DMA_CCMD_MASK_NOBIT, 2375 + DMA_CCMD_DEVICE_INVL); 2376 + iommu->flush.flush_iotlb(iommu, 2377 + did_old, 2378 + 0, 2379 + 0, 2380 + DMA_TLB_DSI_FLUSH); 2360 2381 } 2361 2382 2362 2383 static inline void unlink_domain_info(struct device_domain_info *info) ··· 3225 3206 bool copied_tables = false; 3226 3207 struct device *dev; 3227 3208 struct intel_iommu *iommu; 3228 - int i, ret, cpu; 3209 + int i, ret; 3229 3210 3230 3211 /* 3231 3212 * for each drhd ··· 3256 3237 pr_err("Allocating global iommu array failed\n"); 3257 3238 ret = -ENOMEM; 3258 3239 goto error; 3259 - } 3260 - 3261 - for_each_possible_cpu(cpu) { 3262 - struct deferred_flush_data *dfd = per_cpu_ptr(&deferred_flush, 3263 - cpu); 3264 - 3265 - dfd->tables = kzalloc(g_num_of_iommus * 3266 - sizeof(struct deferred_flush_table), 3267 - GFP_KERNEL); 3268 - if (!dfd->tables) { 3269 - ret = -ENOMEM; 3270 - goto free_g_iommus; 3271 - } 3272 - 3273 - spin_lock_init(&dfd->lock); 3274 - setup_timer(&dfd->timer, flush_unmaps_timeout, cpu); 3275 3240 } 3276 3241 3277 3242 for_each_active_iommu(iommu, drhd) { ··· 3440 3437 disable_dmar_iommu(iommu); 3441 3438 free_dmar_iommu(iommu); 3442 3439 } 3443 - free_g_iommus: 3444 - for_each_possible_cpu(cpu) 3445 - kfree(per_cpu_ptr(&deferred_flush, cpu)->tables); 3440 + 3446 3441 kfree(g_iommus); 3442 + 3447 3443 error: 3448 3444 return ret; 3449 3445 } ··· 3647 3645 dir, *dev->dma_mask); 3648 3646 } 3649 3647 3650 - static void flush_unmaps(struct deferred_flush_data *flush_data) 3651 - { 3652 - int i, j; 3653 - 3654 - flush_data->timer_on = 0; 3655 - 3656 - /* just flush them all */ 3657 - for (i = 0; i < g_num_of_iommus; i++) { 3658 - struct intel_iommu *iommu = g_iommus[i]; 3659 - struct deferred_flush_table *flush_table = 3660 - &flush_data->tables[i]; 3661 - if (!iommu) 3662 - continue; 3663 - 3664 - if (!flush_table->next) 3665 - continue; 3666 - 3667 - /* In caching mode, global flushes turn emulation expensive */ 3668 - if (!cap_caching_mode(iommu->cap)) 3669 - iommu->flush.flush_iotlb(iommu, 0, 0, 0, 3670 - DMA_TLB_GLOBAL_FLUSH); 3671 - for (j = 0; j < flush_table->next; j++) { 3672 - unsigned long mask; 3673 - struct deferred_flush_entry *entry = 3674 - &flush_table->entries[j]; 3675 - unsigned long iova_pfn = entry->iova_pfn; 3676 - unsigned long nrpages = entry->nrpages; 3677 - struct dmar_domain *domain = entry->domain; 3678 - struct page *freelist = entry->freelist; 3679 - 3680 - /* On real hardware multiple invalidations are expensive */ 3681 - if (cap_caching_mode(iommu->cap)) 3682 - iommu_flush_iotlb_psi(iommu, domain, 3683 - mm_to_dma_pfn(iova_pfn), 3684 - nrpages, !freelist, 0); 3685 - else { 3686 - mask = ilog2(nrpages); 3687 - iommu_flush_dev_iotlb(domain, 3688 - (uint64_t)iova_pfn << PAGE_SHIFT, mask); 3689 - } 3690 - free_iova_fast(&domain->iovad, iova_pfn, nrpages); 3691 - if (freelist) 3692 - dma_free_pagelist(freelist); 3693 - } 3694 - flush_table->next = 0; 3695 - } 3696 - 3697 - flush_data->size = 0; 3698 - } 3699 - 3700 - static void flush_unmaps_timeout(unsigned long cpuid) 3701 - { 3702 - struct deferred_flush_data *flush_data = per_cpu_ptr(&deferred_flush, cpuid); 3703 - unsigned long flags; 3704 - 3705 - spin_lock_irqsave(&flush_data->lock, flags); 3706 - flush_unmaps(flush_data); 3707 - spin_unlock_irqrestore(&flush_data->lock, flags); 3708 - } 3709 - 3710 - static void add_unmap(struct dmar_domain *dom, unsigned long iova_pfn, 3711 - unsigned long nrpages, struct page *freelist) 3712 - { 3713 - unsigned long flags; 3714 - int entry_id, iommu_id; 3715 - struct intel_iommu *iommu; 3716 - struct deferred_flush_entry *entry; 3717 - struct deferred_flush_data *flush_data; 3718 - 3719 - flush_data = raw_cpu_ptr(&deferred_flush); 3720 - 3721 - /* Flush all CPUs' entries to avoid deferring too much. If 3722 - * this becomes a bottleneck, can just flush us, and rely on 3723 - * flush timer for the rest. 3724 - */ 3725 - if (flush_data->size == HIGH_WATER_MARK) { 3726 - int cpu; 3727 - 3728 - for_each_online_cpu(cpu) 3729 - flush_unmaps_timeout(cpu); 3730 - } 3731 - 3732 - spin_lock_irqsave(&flush_data->lock, flags); 3733 - 3734 - iommu = domain_get_iommu(dom); 3735 - iommu_id = iommu->seq_id; 3736 - 3737 - entry_id = flush_data->tables[iommu_id].next; 3738 - ++(flush_data->tables[iommu_id].next); 3739 - 3740 - entry = &flush_data->tables[iommu_id].entries[entry_id]; 3741 - entry->domain = dom; 3742 - entry->iova_pfn = iova_pfn; 3743 - entry->nrpages = nrpages; 3744 - entry->freelist = freelist; 3745 - 3746 - if (!flush_data->timer_on) { 3747 - mod_timer(&flush_data->timer, jiffies + msecs_to_jiffies(10)); 3748 - flush_data->timer_on = 1; 3749 - } 3750 - flush_data->size++; 3751 - spin_unlock_irqrestore(&flush_data->lock, flags); 3752 - } 3753 - 3754 3648 static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size) 3755 3649 { 3756 3650 struct dmar_domain *domain; ··· 3682 3784 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages)); 3683 3785 dma_free_pagelist(freelist); 3684 3786 } else { 3685 - add_unmap(domain, iova_pfn, nrpages, freelist); 3787 + queue_iova(&domain->iovad, iova_pfn, nrpages, 3788 + (unsigned long)freelist); 3686 3789 /* 3687 3790 * queue up the release of the unmap to save the 1/6th of the 3688 3791 * cpu used up by the iotlb flush operation... ··· 3837 3938 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot); 3838 3939 if (unlikely(ret)) { 3839 3940 dma_pte_free_pagetable(domain, start_vpfn, 3840 - start_vpfn + size - 1); 3941 + start_vpfn + size - 1, 3942 + agaw_to_level(domain->agaw) + 1); 3841 3943 free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size)); 3842 3944 return 0; 3843 3945 } ··· 4621 4721 static int intel_iommu_cpu_dead(unsigned int cpu) 4622 4722 { 4623 4723 free_all_cpu_cached_iovas(cpu); 4624 - flush_unmaps_timeout(cpu); 4625 4724 return 0; 4626 4725 } 4627 4726 ··· 5242 5343 sdev->sid = PCI_DEVID(info->bus, info->devfn); 5243 5344 5244 5345 if (!(ctx_lo & CONTEXT_PASIDE)) { 5245 - context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table); 5346 + if (iommu->pasid_state_table) 5347 + context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table); 5246 5348 context[1].lo = (u64)virt_to_phys(iommu->pasid_table) | 5247 5349 intel_iommu_get_pts(iommu); 5248 5350
+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 ··· 556 555 return (requested & ~vma->vm_flags) != 0; 557 556 } 558 557 558 + static bool is_canonical_address(u64 addr) 559 + { 560 + int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1); 561 + long saddr = (long) addr; 562 + 563 + return (((saddr << shift) >> shift) == saddr); 564 + } 565 + 559 566 static irqreturn_t prq_event_thread(int irq, void *d) 560 567 { 561 568 struct intel_iommu *iommu = d; ··· 621 612 /* If the mm is already defunct, don't handle faults. */ 622 613 if (!mmget_not_zero(svm->mm)) 623 614 goto bad_req; 615 + 616 + /* If address is not canonical, return invalid response */ 617 + if (!is_canonical_address(address)) 618 + goto bad_req; 619 + 624 620 down_read(&svm->mm->mmap_sem); 625 621 vma = find_extend_vma(svm->mm, address); 626 622 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