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

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

Pull IOMMU updates from Joerg Roedel:
"This time with:

- Generic page-table framework for ARM IOMMUs using the LPAE
page-table format, ARM-SMMU and Renesas IPMMU make use of it
already.

- Break out the IO virtual address allocator from the Intel IOMMU so
that it can be used by other DMA-API implementations too. The
first user will be the ARM64 common DMA-API implementation for
IOMMUs

- Device tree support for Renesas IPMMU

- Various fixes and cleanups all over the place"

* tag 'iommu-updates-v3.20' of git://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu: (36 commits)
iommu/amd: Convert non-returned local variable to boolean when relevant
iommu: Update my email address
iommu/amd: Use wait_event in put_pasid_state_wait
iommu/amd: Fix amd_iommu_free_device()
iommu/arm-smmu: Avoid build warning
iommu/fsl: Various cleanups
iommu/fsl: Use %pa to print phys_addr_t
iommu/omap: Print phys_addr_t using %pa
iommu: Make more drivers depend on COMPILE_TEST
iommu/ipmmu-vmsa: Fix IOMMU lookup when multiple IOMMUs are registered
iommu: Disable on !MMU builds
iommu/fsl: Remove unused fsl_of_pamu_ids[]
iommu/fsl: Fix section mismatch
iommu/ipmmu-vmsa: Use the ARM LPAE page table allocator
iommu: Fix trace_map() to report original iova and original size
iommu/arm-smmu: add support for iova_to_phys through ATS1PR
iopoll: Introduce memory-mapped IO polling macros
iommu/arm-smmu: don't touch the secure STLBIALL register
iommu/arm-smmu: make use of generic LPAE allocator
iommu: io-pgtable-arm: add non-secure quirk
...

+2251 -1504
+41
Documentation/devicetree/bindings/iommu/renesas,ipmmu-vmsa.txt
··· 1 + * Renesas VMSA-Compatible IOMMU 2 + 3 + The IPMMU is an IOMMU implementation compatible with the ARM VMSA page tables. 4 + It provides address translation for bus masters outside of the CPU, each 5 + connected to the IPMMU through a port called micro-TLB. 6 + 7 + 8 + Required Properties: 9 + 10 + - compatible: Must contain "renesas,ipmmu-vmsa". 11 + - reg: Base address and size of the IPMMU registers. 12 + - interrupts: Specifiers for the MMU fault interrupts. For instances that 13 + support secure mode two interrupts must be specified, for non-secure and 14 + secure mode, in that order. For instances that don't support secure mode a 15 + single interrupt must be specified. 16 + 17 + - #iommu-cells: Must be 1. 18 + 19 + Each bus master connected to an IPMMU must reference the IPMMU in its device 20 + node with the following property: 21 + 22 + - iommus: A reference to the IPMMU in two cells. The first cell is a phandle 23 + to the IPMMU and the second cell the number of the micro-TLB that the 24 + device is connected to. 25 + 26 + 27 + Example: R8A7791 IPMMU-MX and VSP1-D0 bus master 28 + 29 + ipmmu_mx: mmu@fe951000 { 30 + compatible = "renasas,ipmmu-vmsa"; 31 + reg = <0 0xfe951000 0 0x1000>; 32 + interrupts = <0 222 IRQ_TYPE_LEVEL_HIGH>, 33 + <0 221 IRQ_TYPE_LEVEL_HIGH>; 34 + #iommu-cells = <1>; 35 + }; 36 + 37 + vsp1@fe928000 { 38 + ... 39 + iommus = <&ipmmu_mx 13>; 40 + ... 41 + };
+1
MAINTAINERS
··· 1606 1606 L: linux-arm-kernel@lists.infradead.org (moderated for non-subscribers) 1607 1607 S: Maintained 1608 1608 F: drivers/iommu/arm-smmu.c 1609 + F: drivers/iommu/io-pgtable-arm.c 1609 1610 1610 1611 ARM64 PORT (AARCH64 ARCHITECTURE) 1611 1612 M: Catalin Marinas <catalin.marinas@arm.com>
-1
arch/arm64/Kconfig
··· 350 350 351 351 config ARM64_VA_BITS_48 352 352 bool "48-bit" 353 - depends on !ARM_SMMU 354 353 355 354 endchoice 356 355
+2 -2
arch/powerpc/include/asm/fsl_pamu_stash.h
··· 32 32 */ 33 33 34 34 struct pamu_stash_attribute { 35 - u32 cpu; /* cpu number */ 36 - u32 cache; /* cache to stash to: L1,L2,L3 */ 35 + u32 cpu; /* cpu number */ 36 + u32 cache; /* cache to stash to: L1,L2,L3 */ 37 37 }; 38 38 39 39 #endif /* __FSL_PAMU_STASH_H */
+43 -8
drivers/iommu/Kconfig
··· 4 4 5 5 menuconfig IOMMU_SUPPORT 6 6 bool "IOMMU Hardware Support" 7 + depends on MMU 7 8 default y 8 9 ---help--- 9 10 Say Y here if you want to compile device drivers for IO Memory ··· 14 13 15 14 if IOMMU_SUPPORT 16 15 16 + menu "Generic IOMMU Pagetable Support" 17 + 18 + # Selected by the actual pagetable implementations 19 + config IOMMU_IO_PGTABLE 20 + bool 21 + 22 + config IOMMU_IO_PGTABLE_LPAE 23 + bool "ARMv7/v8 Long Descriptor Format" 24 + select IOMMU_IO_PGTABLE 25 + help 26 + Enable support for the ARM long descriptor pagetable format. 27 + This allocator supports 4K/2M/1G, 16K/32M and 64K/512M page 28 + sizes at both stage-1 and stage-2, as well as address spaces 29 + up to 48-bits in size. 30 + 31 + config IOMMU_IO_PGTABLE_LPAE_SELFTEST 32 + bool "LPAE selftests" 33 + depends on IOMMU_IO_PGTABLE_LPAE 34 + help 35 + Enable self-tests for LPAE page table allocator. This performs 36 + a series of page-table consistency checks during boot. 37 + 38 + If unsure, say N here. 39 + 40 + endmenu 41 + 42 + config IOMMU_IOVA 43 + bool 44 + 17 45 config OF_IOMMU 18 46 def_bool y 19 47 depends on OF && IOMMU_API 20 48 21 49 config FSL_PAMU 22 50 bool "Freescale IOMMU support" 23 - depends on PPC_E500MC 51 + depends on PPC32 52 + depends on PPC_E500MC || COMPILE_TEST 24 53 select IOMMU_API 25 54 select GENERIC_ALLOCATOR 26 55 help ··· 61 30 # MSM IOMMU support 62 31 config MSM_IOMMU 63 32 bool "MSM IOMMU Support" 64 - depends on ARCH_MSM8X60 || ARCH_MSM8960 33 + depends on ARM 34 + depends on ARCH_MSM8X60 || ARCH_MSM8960 || COMPILE_TEST 65 35 select IOMMU_API 66 36 help 67 37 Support for the IOMMUs found on certain Qualcomm SOCs. ··· 123 91 bool "Support for Intel IOMMU using DMA Remapping Devices" 124 92 depends on PCI_MSI && ACPI && (X86 || IA64_GENERIC) 125 93 select IOMMU_API 94 + select IOMMU_IOVA 126 95 select DMAR_TABLE 127 96 help 128 97 DMA remapping (DMAR) devices support enables independent address ··· 173 140 # OMAP IOMMU support 174 141 config OMAP_IOMMU 175 142 bool "OMAP IOMMU Support" 176 - depends on ARCH_OMAP2PLUS 143 + depends on ARM && MMU 144 + depends on ARCH_OMAP2PLUS || COMPILE_TEST 177 145 select IOMMU_API 178 146 179 147 config OMAP_IOMMU_DEBUG ··· 221 187 222 188 config EXYNOS_IOMMU 223 189 bool "Exynos IOMMU Support" 224 - depends on ARCH_EXYNOS && ARM 190 + depends on ARCH_EXYNOS && ARM && MMU 225 191 select IOMMU_API 226 192 select ARM_DMA_USE_IOMMU 227 193 help ··· 250 216 config SHMOBILE_IOMMU 251 217 bool "IOMMU for Renesas IPMMU/IPMMUI" 252 218 default n 253 - depends on ARM 219 + depends on ARM && MMU 254 220 depends on ARCH_SHMOBILE || COMPILE_TEST 255 221 select IOMMU_API 256 222 select ARM_DMA_USE_IOMMU ··· 321 287 depends on ARM_LPAE 322 288 depends on ARCH_SHMOBILE || COMPILE_TEST 323 289 select IOMMU_API 290 + select IOMMU_IO_PGTABLE_LPAE 324 291 select ARM_DMA_USE_IOMMU 325 292 help 326 293 Support for the Renesas VMSA-compatible IPMMU Renesas found in the ··· 339 304 340 305 config ARM_SMMU 341 306 bool "ARM Ltd. System MMU (SMMU) Support" 342 - depends on ARM64 || (ARM_LPAE && OF) 307 + depends on (ARM64 || ARM) && MMU 343 308 select IOMMU_API 309 + select IOMMU_IO_PGTABLE_LPAE 344 310 select ARM_DMA_USE_IOMMU if ARM 345 311 help 346 312 Support for implementations of the ARM System MMU architecture 347 - versions 1 and 2. The driver supports both v7l and v8l table 348 - formats with 4k and 64k page sizes. 313 + versions 1 and 2. 349 314 350 315 Say Y here if your SoC includes an IOMMU device implementing 351 316 the ARM SMMU architecture.
+4 -1
drivers/iommu/Makefile
··· 1 1 obj-$(CONFIG_IOMMU_API) += iommu.o 2 2 obj-$(CONFIG_IOMMU_API) += iommu-traces.o 3 3 obj-$(CONFIG_IOMMU_API) += iommu-sysfs.o 4 + obj-$(CONFIG_IOMMU_IO_PGTABLE) += io-pgtable.o 5 + obj-$(CONFIG_IOMMU_IO_PGTABLE_LPAE) += io-pgtable-arm.o 6 + obj-$(CONFIG_IOMMU_IOVA) += iova.o 4 7 obj-$(CONFIG_OF_IOMMU) += of_iommu.o 5 8 obj-$(CONFIG_MSM_IOMMU) += msm_iommu.o msm_iommu_dev.o 6 9 obj-$(CONFIG_AMD_IOMMU) += amd_iommu.o amd_iommu_init.o 7 10 obj-$(CONFIG_AMD_IOMMU_V2) += amd_iommu_v2.o 8 11 obj-$(CONFIG_ARM_SMMU) += arm-smmu.o 9 12 obj-$(CONFIG_DMAR_TABLE) += dmar.o 10 - obj-$(CONFIG_INTEL_IOMMU) += iova.o intel-iommu.o 13 + obj-$(CONFIG_INTEL_IOMMU) += intel-iommu.o 11 14 obj-$(CONFIG_IPMMU_VMSA) += ipmmu-vmsa.o 12 15 obj-$(CONFIG_IRQ_REMAP) += intel_irq_remapping.o irq_remapping.o 13 16 obj-$(CONFIG_OMAP_IOMMU) += omap-iommu.o
+7 -7
drivers/iommu/amd_iommu.c
··· 1 1 /* 2 2 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc. 3 - * Author: Joerg Roedel <joerg.roedel@amd.com> 3 + * Author: Joerg Roedel <jroedel@suse.de> 4 4 * Leo Duran <leo.duran@amd.com> 5 5 * 6 6 * This program is free software; you can redistribute it and/or modify it ··· 843 843 size_t size, u16 domid, int pde) 844 844 { 845 845 u64 pages; 846 - int s; 846 + bool s; 847 847 848 848 pages = iommu_num_pages(address, size, PAGE_SIZE); 849 - s = 0; 849 + s = false; 850 850 851 851 if (pages > 1) { 852 852 /* ··· 854 854 * TLB entries for this domain 855 855 */ 856 856 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; 857 - s = 1; 857 + s = true; 858 858 } 859 859 860 860 address &= PAGE_MASK; ··· 874 874 u64 address, size_t size) 875 875 { 876 876 u64 pages; 877 - int s; 877 + bool s; 878 878 879 879 pages = iommu_num_pages(address, size, PAGE_SIZE); 880 - s = 0; 880 + s = false; 881 881 882 882 if (pages > 1) { 883 883 /* ··· 885 885 * TLB entries for this domain 886 886 */ 887 887 address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; 888 - s = 1; 888 + s = true; 889 889 } 890 890 891 891 address &= PAGE_MASK;
+1 -1
drivers/iommu/amd_iommu_init.c
··· 1 1 /* 2 2 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc. 3 - * Author: Joerg Roedel <joerg.roedel@amd.com> 3 + * Author: Joerg Roedel <jroedel@suse.de> 4 4 * Leo Duran <leo.duran@amd.com> 5 5 * 6 6 * This program is free software; you can redistribute it and/or modify it
+1 -1
drivers/iommu/amd_iommu_proto.h
··· 1 1 /* 2 2 * Copyright (C) 2009-2010 Advanced Micro Devices, Inc. 3 - * Author: Joerg Roedel <joerg.roedel@amd.com> 3 + * Author: Joerg Roedel <jroedel@suse.de> 4 4 * 5 5 * This program is free software; you can redistribute it and/or modify it 6 6 * under the terms of the GNU General Public License version 2 as published
+1 -1
drivers/iommu/amd_iommu_types.h
··· 1 1 /* 2 2 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc. 3 - * Author: Joerg Roedel <joerg.roedel@amd.com> 3 + * Author: Joerg Roedel <jroedel@suse.de> 4 4 * Leo Duran <leo.duran@amd.com> 5 5 * 6 6 * This program is free software; you can redistribute it and/or modify it
+11 -24
drivers/iommu/amd_iommu_v2.c
··· 1 1 /* 2 2 * Copyright (C) 2010-2012 Advanced Micro Devices, Inc. 3 - * Author: Joerg Roedel <joerg.roedel@amd.com> 3 + * Author: Joerg Roedel <jroedel@suse.de> 4 4 * 5 5 * This program is free software; you can redistribute it and/or modify it 6 6 * under the terms of the GNU General Public License version 2 as published ··· 31 31 #include "amd_iommu_proto.h" 32 32 33 33 MODULE_LICENSE("GPL v2"); 34 - MODULE_AUTHOR("Joerg Roedel <joerg.roedel@amd.com>"); 34 + MODULE_AUTHOR("Joerg Roedel <jroedel@suse.de>"); 35 35 36 36 #define MAX_DEVICES 0x10000 37 37 #define PRI_QUEUE_SIZE 512 ··· 151 151 wake_up(&dev_state->wq); 152 152 } 153 153 154 - static void put_device_state_wait(struct device_state *dev_state) 155 - { 156 - DEFINE_WAIT(wait); 157 - 158 - prepare_to_wait(&dev_state->wq, &wait, TASK_UNINTERRUPTIBLE); 159 - if (!atomic_dec_and_test(&dev_state->count)) 160 - schedule(); 161 - finish_wait(&dev_state->wq, &wait); 162 - 163 - free_device_state(dev_state); 164 - } 165 - 166 154 /* Must be called under dev_state->lock */ 167 155 static struct pasid_state **__get_pasid_state_ptr(struct device_state *dev_state, 168 156 int pasid, bool alloc) ··· 266 278 267 279 static void put_pasid_state_wait(struct pasid_state *pasid_state) 268 280 { 269 - DEFINE_WAIT(wait); 270 - 271 - prepare_to_wait(&pasid_state->wq, &wait, TASK_UNINTERRUPTIBLE); 272 - 273 - if (!atomic_dec_and_test(&pasid_state->count)) 274 - schedule(); 275 - 276 - finish_wait(&pasid_state->wq, &wait); 281 + wait_event(pasid_state->wq, !atomic_read(&pasid_state->count)); 277 282 free_pasid_state(pasid_state); 278 283 } 279 284 ··· 832 851 /* Get rid of any remaining pasid states */ 833 852 free_pasid_states(dev_state); 834 853 835 - put_device_state_wait(dev_state); 854 + put_device_state(dev_state); 855 + /* 856 + * Wait until the last reference is dropped before freeing 857 + * the device state. 858 + */ 859 + wait_event(dev_state->wq, !atomic_read(&dev_state->count)); 860 + free_device_state(dev_state); 836 861 } 837 862 EXPORT_SYMBOL(amd_iommu_free_device); 838 863 ··· 908 921 { 909 922 int ret; 910 923 911 - pr_info("AMD IOMMUv2 driver by Joerg Roedel <joerg.roedel@amd.com>\n"); 924 + pr_info("AMD IOMMUv2 driver by Joerg Roedel <jroedel@suse.de>\n"); 912 925 913 926 if (!amd_iommu_v2_supported()) { 914 927 pr_info("AMD IOMMUv2 functionality not available on this system\n");
+328 -625
drivers/iommu/arm-smmu.c
··· 23 23 * - Stream-matching and stream-indexing 24 24 * - v7/v8 long-descriptor format 25 25 * - Non-secure access to the SMMU 26 - * - 4k and 64k pages, with contiguous pte hints. 27 - * - Up to 48-bit addressing (dependent on VA_BITS) 28 26 * - Context fault reporting 29 27 */ 30 28 ··· 34 36 #include <linux/interrupt.h> 35 37 #include <linux/io.h> 36 38 #include <linux/iommu.h> 37 - #include <linux/mm.h> 39 + #include <linux/iopoll.h> 38 40 #include <linux/module.h> 39 41 #include <linux/of.h> 40 42 #include <linux/pci.h> ··· 44 46 45 47 #include <linux/amba/bus.h> 46 48 47 - #include <asm/pgalloc.h> 49 + #include "io-pgtable.h" 48 50 49 51 /* Maximum number of stream IDs assigned to a single device */ 50 52 #define MAX_MASTER_STREAMIDS MAX_PHANDLE_ARGS ··· 68 70 ((smmu)->base + \ 69 71 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \ 70 72 ? 0x400 : 0)) 71 - 72 - /* Page table bits */ 73 - #define ARM_SMMU_PTE_XN (((pteval_t)3) << 53) 74 - #define ARM_SMMU_PTE_CONT (((pteval_t)1) << 52) 75 - #define ARM_SMMU_PTE_AF (((pteval_t)1) << 10) 76 - #define ARM_SMMU_PTE_SH_NS (((pteval_t)0) << 8) 77 - #define ARM_SMMU_PTE_SH_OS (((pteval_t)2) << 8) 78 - #define ARM_SMMU_PTE_SH_IS (((pteval_t)3) << 8) 79 - #define ARM_SMMU_PTE_PAGE (((pteval_t)3) << 0) 80 - 81 - #if PAGE_SIZE == SZ_4K 82 - #define ARM_SMMU_PTE_CONT_ENTRIES 16 83 - #elif PAGE_SIZE == SZ_64K 84 - #define ARM_SMMU_PTE_CONT_ENTRIES 32 85 - #else 86 - #define ARM_SMMU_PTE_CONT_ENTRIES 1 87 - #endif 88 - 89 - #define ARM_SMMU_PTE_CONT_SIZE (PAGE_SIZE * ARM_SMMU_PTE_CONT_ENTRIES) 90 - #define ARM_SMMU_PTE_CONT_MASK (~(ARM_SMMU_PTE_CONT_SIZE - 1)) 91 - 92 - /* Stage-1 PTE */ 93 - #define ARM_SMMU_PTE_AP_UNPRIV (((pteval_t)1) << 6) 94 - #define ARM_SMMU_PTE_AP_RDONLY (((pteval_t)2) << 6) 95 - #define ARM_SMMU_PTE_ATTRINDX_SHIFT 2 96 - #define ARM_SMMU_PTE_nG (((pteval_t)1) << 11) 97 - 98 - /* Stage-2 PTE */ 99 - #define ARM_SMMU_PTE_HAP_FAULT (((pteval_t)0) << 6) 100 - #define ARM_SMMU_PTE_HAP_READ (((pteval_t)1) << 6) 101 - #define ARM_SMMU_PTE_HAP_WRITE (((pteval_t)2) << 6) 102 - #define ARM_SMMU_PTE_MEMATTR_OIWB (((pteval_t)0xf) << 2) 103 - #define ARM_SMMU_PTE_MEMATTR_NC (((pteval_t)0x5) << 2) 104 - #define ARM_SMMU_PTE_MEMATTR_DEV (((pteval_t)0x1) << 2) 105 73 106 74 /* Configuration registers */ 107 75 #define ARM_SMMU_GR0_sCR0 0x0 ··· 96 132 #define ARM_SMMU_GR0_sGFSYNR0 0x50 97 133 #define ARM_SMMU_GR0_sGFSYNR1 0x54 98 134 #define ARM_SMMU_GR0_sGFSYNR2 0x58 99 - #define ARM_SMMU_GR0_PIDR0 0xfe0 100 - #define ARM_SMMU_GR0_PIDR1 0xfe4 101 - #define ARM_SMMU_GR0_PIDR2 0xfe8 102 135 103 136 #define ID0_S1TS (1 << 30) 104 137 #define ID0_S2TS (1 << 29) 105 138 #define ID0_NTS (1 << 28) 106 139 #define ID0_SMS (1 << 27) 107 - #define ID0_PTFS_SHIFT 24 108 - #define ID0_PTFS_MASK 0x2 109 - #define ID0_PTFS_V8_ONLY 0x2 140 + #define ID0_ATOSNS (1 << 26) 110 141 #define ID0_CTTW (1 << 14) 111 142 #define ID0_NUMIRPT_SHIFT 16 112 143 #define ID0_NUMIRPT_MASK 0xff ··· 128 169 #define ID2_PTFS_16K (1 << 13) 129 170 #define ID2_PTFS_64K (1 << 14) 130 171 131 - #define PIDR2_ARCH_SHIFT 4 132 - #define PIDR2_ARCH_MASK 0xf 133 - 134 172 /* Global TLB invalidation */ 135 - #define ARM_SMMU_GR0_STLBIALL 0x60 136 173 #define ARM_SMMU_GR0_TLBIVMID 0x64 137 174 #define ARM_SMMU_GR0_TLBIALLNSNH 0x68 138 175 #define ARM_SMMU_GR0_TLBIALLH 0x6c ··· 186 231 #define ARM_SMMU_CB_TTBCR2 0x10 187 232 #define ARM_SMMU_CB_TTBR0_LO 0x20 188 233 #define ARM_SMMU_CB_TTBR0_HI 0x24 234 + #define ARM_SMMU_CB_TTBR1_LO 0x28 235 + #define ARM_SMMU_CB_TTBR1_HI 0x2c 189 236 #define ARM_SMMU_CB_TTBCR 0x30 190 237 #define ARM_SMMU_CB_S1_MAIR0 0x38 238 + #define ARM_SMMU_CB_S1_MAIR1 0x3c 239 + #define ARM_SMMU_CB_PAR_LO 0x50 240 + #define ARM_SMMU_CB_PAR_HI 0x54 191 241 #define ARM_SMMU_CB_FSR 0x58 192 242 #define ARM_SMMU_CB_FAR_LO 0x60 193 243 #define ARM_SMMU_CB_FAR_HI 0x64 194 244 #define ARM_SMMU_CB_FSYNR0 0x68 245 + #define ARM_SMMU_CB_S1_TLBIVA 0x600 195 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_ATS1PR_LO 0x800 251 + #define ARM_SMMU_CB_ATS1PR_HI 0x804 252 + #define ARM_SMMU_CB_ATSR 0x8f0 196 253 197 254 #define SCTLR_S1_ASIDPNE (1 << 12) 198 255 #define SCTLR_CFCFG (1 << 7) ··· 216 249 #define SCTLR_M (1 << 0) 217 250 #define SCTLR_EAE_SBOP (SCTLR_AFE | SCTLR_TRE) 218 251 252 + #define CB_PAR_F (1 << 0) 253 + 254 + #define ATSR_ACTIVE (1 << 0) 255 + 219 256 #define RESUME_RETRY (0 << 0) 220 257 #define RESUME_TERMINATE (1 << 0) 221 - 222 - #define TTBCR_EAE (1 << 31) 223 - 224 - #define TTBCR_PASIZE_SHIFT 16 225 - #define TTBCR_PASIZE_MASK 0x7 226 - 227 - #define TTBCR_TG0_4K (0 << 14) 228 - #define TTBCR_TG0_64K (1 << 14) 229 - 230 - #define TTBCR_SH0_SHIFT 12 231 - #define TTBCR_SH0_MASK 0x3 232 - #define TTBCR_SH_NS 0 233 - #define TTBCR_SH_OS 2 234 - #define TTBCR_SH_IS 3 235 - 236 - #define TTBCR_ORGN0_SHIFT 10 237 - #define TTBCR_IRGN0_SHIFT 8 238 - #define TTBCR_RGN_MASK 0x3 239 - #define TTBCR_RGN_NC 0 240 - #define TTBCR_RGN_WBWA 1 241 - #define TTBCR_RGN_WT 2 242 - #define TTBCR_RGN_WB 3 243 - 244 - #define TTBCR_SL0_SHIFT 6 245 - #define TTBCR_SL0_MASK 0x3 246 - #define TTBCR_SL0_LVL_2 0 247 - #define TTBCR_SL0_LVL_1 1 248 - 249 - #define TTBCR_T1SZ_SHIFT 16 250 - #define TTBCR_T0SZ_SHIFT 0 251 - #define TTBCR_SZ_MASK 0xf 252 258 253 259 #define TTBCR2_SEP_SHIFT 15 254 260 #define TTBCR2_SEP_MASK 0x7 255 261 256 - #define TTBCR2_PASIZE_SHIFT 0 257 - #define TTBCR2_PASIZE_MASK 0x7 258 - 259 - /* Common definitions for PASize and SEP fields */ 260 262 #define TTBCR2_ADDR_32 0 261 263 #define TTBCR2_ADDR_36 1 262 264 #define TTBCR2_ADDR_40 2 ··· 233 297 #define TTBCR2_ADDR_44 4 234 298 #define TTBCR2_ADDR_48 5 235 299 236 - #define TTBRn_HI_ASID_SHIFT 16 237 - 238 - #define MAIR_ATTR_SHIFT(n) ((n) << 3) 239 - #define MAIR_ATTR_MASK 0xff 240 - #define MAIR_ATTR_DEVICE 0x04 241 - #define MAIR_ATTR_NC 0x44 242 - #define MAIR_ATTR_WBRWA 0xff 243 - #define MAIR_ATTR_IDX_NC 0 244 - #define MAIR_ATTR_IDX_CACHE 1 245 - #define MAIR_ATTR_IDX_DEV 2 300 + #define TTBRn_HI_ASID_SHIFT 16 246 301 247 302 #define FSR_MULTI (1 << 31) 248 303 #define FSR_SS (1 << 30) ··· 293 366 #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2) 294 367 #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3) 295 368 #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4) 369 + #define ARM_SMMU_FEAT_TRANS_OPS (1 << 5) 296 370 u32 features; 297 371 298 372 #define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0) ··· 308 380 u32 num_mapping_groups; 309 381 DECLARE_BITMAP(smr_map, ARM_SMMU_MAX_SMRS); 310 382 311 - unsigned long s1_input_size; 312 - unsigned long s1_output_size; 313 - unsigned long s2_input_size; 314 - unsigned long s2_output_size; 383 + unsigned long va_size; 384 + unsigned long ipa_size; 385 + unsigned long pa_size; 315 386 316 387 u32 num_global_irqs; 317 388 u32 num_context_irqs; ··· 324 397 u8 cbndx; 325 398 u8 irptndx; 326 399 u32 cbar; 327 - pgd_t *pgd; 328 400 }; 329 401 #define INVALID_IRPTNDX 0xff 330 402 ··· 338 412 339 413 struct arm_smmu_domain { 340 414 struct arm_smmu_device *smmu; 415 + struct io_pgtable_ops *pgtbl_ops; 416 + spinlock_t pgtbl_lock; 341 417 struct arm_smmu_cfg cfg; 342 418 enum arm_smmu_domain_stage stage; 343 - spinlock_t lock; 419 + struct mutex init_mutex; /* Protects smmu pointer */ 344 420 }; 421 + 422 + static struct iommu_ops arm_smmu_ops; 345 423 346 424 static DEFINE_SPINLOCK(arm_smmu_devices_lock); 347 425 static LIST_HEAD(arm_smmu_devices); ··· 527 597 } 528 598 529 599 /* Wait for any pending TLB invalidations to complete */ 530 - static void arm_smmu_tlb_sync(struct arm_smmu_device *smmu) 600 + static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu) 531 601 { 532 602 int count = 0; 533 603 void __iomem *gr0_base = ARM_SMMU_GR0(smmu); ··· 545 615 } 546 616 } 547 617 548 - static void arm_smmu_tlb_inv_context(struct arm_smmu_domain *smmu_domain) 618 + static void arm_smmu_tlb_sync(void *cookie) 549 619 { 620 + struct arm_smmu_domain *smmu_domain = cookie; 621 + __arm_smmu_tlb_sync(smmu_domain->smmu); 622 + } 623 + 624 + static void arm_smmu_tlb_inv_context(void *cookie) 625 + { 626 + struct arm_smmu_domain *smmu_domain = cookie; 550 627 struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 551 628 struct arm_smmu_device *smmu = smmu_domain->smmu; 552 - void __iomem *base = ARM_SMMU_GR0(smmu); 553 629 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS; 630 + void __iomem *base; 554 631 555 632 if (stage1) { 556 633 base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); ··· 569 632 base + ARM_SMMU_GR0_TLBIVMID); 570 633 } 571 634 572 - arm_smmu_tlb_sync(smmu); 635 + __arm_smmu_tlb_sync(smmu); 573 636 } 637 + 638 + static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size, 639 + bool leaf, void *cookie) 640 + { 641 + struct arm_smmu_domain *smmu_domain = cookie; 642 + struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 643 + struct arm_smmu_device *smmu = smmu_domain->smmu; 644 + bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS; 645 + void __iomem *reg; 646 + 647 + if (stage1) { 648 + reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); 649 + reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA; 650 + 651 + if (!IS_ENABLED(CONFIG_64BIT) || smmu->version == ARM_SMMU_V1) { 652 + iova &= ~12UL; 653 + iova |= ARM_SMMU_CB_ASID(cfg); 654 + writel_relaxed(iova, reg); 655 + #ifdef CONFIG_64BIT 656 + } else { 657 + iova >>= 12; 658 + iova |= (u64)ARM_SMMU_CB_ASID(cfg) << 48; 659 + writeq_relaxed(iova, reg); 660 + #endif 661 + } 662 + #ifdef CONFIG_64BIT 663 + } else if (smmu->version == ARM_SMMU_V2) { 664 + reg = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); 665 + reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L : 666 + ARM_SMMU_CB_S2_TLBIIPAS2; 667 + writeq_relaxed(iova >> 12, reg); 668 + #endif 669 + } else { 670 + reg = ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_TLBIVMID; 671 + writel_relaxed(ARM_SMMU_CB_VMID(cfg), reg); 672 + } 673 + } 674 + 675 + static void arm_smmu_flush_pgtable(void *addr, size_t size, void *cookie) 676 + { 677 + struct arm_smmu_domain *smmu_domain = cookie; 678 + struct arm_smmu_device *smmu = smmu_domain->smmu; 679 + unsigned long offset = (unsigned long)addr & ~PAGE_MASK; 680 + 681 + 682 + /* Ensure new page tables are visible to the hardware walker */ 683 + if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) { 684 + dsb(ishst); 685 + } else { 686 + /* 687 + * If the SMMU can't walk tables in the CPU caches, treat them 688 + * like non-coherent DMA since we need to flush the new entries 689 + * all the way out to memory. There's no possibility of 690 + * recursion here as the SMMU table walker will not be wired 691 + * through another SMMU. 692 + */ 693 + dma_map_page(smmu->dev, virt_to_page(addr), offset, size, 694 + DMA_TO_DEVICE); 695 + } 696 + } 697 + 698 + static struct iommu_gather_ops arm_smmu_gather_ops = { 699 + .tlb_flush_all = arm_smmu_tlb_inv_context, 700 + .tlb_add_flush = arm_smmu_tlb_inv_range_nosync, 701 + .tlb_sync = arm_smmu_tlb_sync, 702 + .flush_pgtable = arm_smmu_flush_pgtable, 703 + }; 574 704 575 705 static irqreturn_t arm_smmu_context_fault(int irq, void *dev) 576 706 { ··· 716 712 return IRQ_HANDLED; 717 713 } 718 714 719 - static void arm_smmu_flush_pgtable(struct arm_smmu_device *smmu, void *addr, 720 - size_t size) 721 - { 722 - unsigned long offset = (unsigned long)addr & ~PAGE_MASK; 723 - 724 - 725 - /* Ensure new page tables are visible to the hardware walker */ 726 - if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK) { 727 - dsb(ishst); 728 - } else { 729 - /* 730 - * If the SMMU can't walk tables in the CPU caches, treat them 731 - * like non-coherent DMA since we need to flush the new entries 732 - * all the way out to memory. There's no possibility of 733 - * recursion here as the SMMU table walker will not be wired 734 - * through another SMMU. 735 - */ 736 - dma_map_page(smmu->dev, virt_to_page(addr), offset, size, 737 - DMA_TO_DEVICE); 738 - } 739 - } 740 - 741 - static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain) 715 + static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain, 716 + struct io_pgtable_cfg *pgtbl_cfg) 742 717 { 743 718 u32 reg; 744 719 bool stage1; ··· 754 771 #else 755 772 reg = CBA2R_RW64_32BIT; 756 773 #endif 757 - writel_relaxed(reg, 758 - gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx)); 759 - 760 - /* TTBCR2 */ 761 - switch (smmu->s1_input_size) { 762 - case 32: 763 - reg = (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT); 764 - break; 765 - case 36: 766 - reg = (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT); 767 - break; 768 - case 39: 769 - case 40: 770 - reg = (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT); 771 - break; 772 - case 42: 773 - reg = (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT); 774 - break; 775 - case 44: 776 - reg = (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT); 777 - break; 778 - case 48: 779 - reg = (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT); 780 - break; 781 - } 782 - 783 - switch (smmu->s1_output_size) { 784 - case 32: 785 - reg |= (TTBCR2_ADDR_32 << TTBCR2_PASIZE_SHIFT); 786 - break; 787 - case 36: 788 - reg |= (TTBCR2_ADDR_36 << TTBCR2_PASIZE_SHIFT); 789 - break; 790 - case 39: 791 - case 40: 792 - reg |= (TTBCR2_ADDR_40 << TTBCR2_PASIZE_SHIFT); 793 - break; 794 - case 42: 795 - reg |= (TTBCR2_ADDR_42 << TTBCR2_PASIZE_SHIFT); 796 - break; 797 - case 44: 798 - reg |= (TTBCR2_ADDR_44 << TTBCR2_PASIZE_SHIFT); 799 - break; 800 - case 48: 801 - reg |= (TTBCR2_ADDR_48 << TTBCR2_PASIZE_SHIFT); 802 - break; 803 - } 804 - 805 - if (stage1) 806 - writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2); 774 + writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(cfg->cbndx)); 807 775 } 808 776 809 - /* TTBR0 */ 810 - arm_smmu_flush_pgtable(smmu, cfg->pgd, 811 - PTRS_PER_PGD * sizeof(pgd_t)); 812 - reg = __pa(cfg->pgd); 813 - writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO); 814 - reg = (phys_addr_t)__pa(cfg->pgd) >> 32; 815 - if (stage1) 777 + /* TTBRs */ 778 + if (stage1) { 779 + reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0]; 780 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO); 781 + reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0] >> 32; 816 782 reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT; 817 - writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI); 783 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI); 818 784 819 - /* 820 - * TTBCR 821 - * We use long descriptor, with inner-shareable WBWA tables in TTBR0. 822 - */ 823 - if (smmu->version > ARM_SMMU_V1) { 824 - if (PAGE_SIZE == SZ_4K) 825 - reg = TTBCR_TG0_4K; 826 - else 827 - reg = TTBCR_TG0_64K; 785 + reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1]; 786 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_LO); 787 + reg = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1] >> 32; 788 + reg |= ARM_SMMU_CB_ASID(cfg) << TTBRn_HI_ASID_SHIFT; 789 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR1_HI); 790 + } else { 791 + reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr; 792 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_LO); 793 + reg = pgtbl_cfg->arm_lpae_s2_cfg.vttbr >> 32; 794 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBR0_HI); 795 + } 828 796 829 - if (!stage1) { 830 - reg |= (64 - smmu->s2_input_size) << TTBCR_T0SZ_SHIFT; 831 - 832 - switch (smmu->s2_output_size) { 797 + /* TTBCR */ 798 + if (stage1) { 799 + reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr; 800 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR); 801 + if (smmu->version > ARM_SMMU_V1) { 802 + reg = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32; 803 + switch (smmu->va_size) { 833 804 case 32: 834 - reg |= (TTBCR2_ADDR_32 << TTBCR_PASIZE_SHIFT); 805 + reg |= (TTBCR2_ADDR_32 << TTBCR2_SEP_SHIFT); 835 806 break; 836 807 case 36: 837 - reg |= (TTBCR2_ADDR_36 << TTBCR_PASIZE_SHIFT); 808 + reg |= (TTBCR2_ADDR_36 << TTBCR2_SEP_SHIFT); 838 809 break; 839 810 case 40: 840 - reg |= (TTBCR2_ADDR_40 << TTBCR_PASIZE_SHIFT); 811 + reg |= (TTBCR2_ADDR_40 << TTBCR2_SEP_SHIFT); 841 812 break; 842 813 case 42: 843 - reg |= (TTBCR2_ADDR_42 << TTBCR_PASIZE_SHIFT); 814 + reg |= (TTBCR2_ADDR_42 << TTBCR2_SEP_SHIFT); 844 815 break; 845 816 case 44: 846 - reg |= (TTBCR2_ADDR_44 << TTBCR_PASIZE_SHIFT); 817 + reg |= (TTBCR2_ADDR_44 << TTBCR2_SEP_SHIFT); 847 818 break; 848 819 case 48: 849 - reg |= (TTBCR2_ADDR_48 << TTBCR_PASIZE_SHIFT); 820 + reg |= (TTBCR2_ADDR_48 << TTBCR2_SEP_SHIFT); 850 821 break; 851 822 } 852 - } else { 853 - reg |= (64 - smmu->s1_input_size) << TTBCR_T0SZ_SHIFT; 823 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR2); 854 824 } 855 825 } else { 856 - reg = 0; 826 + reg = pgtbl_cfg->arm_lpae_s2_cfg.vtcr; 827 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR); 857 828 } 858 829 859 - reg |= TTBCR_EAE | 860 - (TTBCR_SH_IS << TTBCR_SH0_SHIFT) | 861 - (TTBCR_RGN_WBWA << TTBCR_ORGN0_SHIFT) | 862 - (TTBCR_RGN_WBWA << TTBCR_IRGN0_SHIFT); 863 - 864 - if (!stage1) 865 - reg |= (TTBCR_SL0_LVL_1 << TTBCR_SL0_SHIFT); 866 - 867 - writel_relaxed(reg, cb_base + ARM_SMMU_CB_TTBCR); 868 - 869 - /* MAIR0 (stage-1 only) */ 830 + /* MAIRs (stage-1 only) */ 870 831 if (stage1) { 871 - reg = (MAIR_ATTR_NC << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_NC)) | 872 - (MAIR_ATTR_WBRWA << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_CACHE)) | 873 - (MAIR_ATTR_DEVICE << MAIR_ATTR_SHIFT(MAIR_ATTR_IDX_DEV)); 832 + reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[0]; 874 833 writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR0); 834 + reg = pgtbl_cfg->arm_lpae_s1_cfg.mair[1]; 835 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_S1_MAIR1); 875 836 } 876 837 877 838 /* SCTLR */ ··· 832 905 struct arm_smmu_device *smmu) 833 906 { 834 907 int irq, start, ret = 0; 835 - unsigned long flags; 908 + unsigned long ias, oas; 909 + struct io_pgtable_ops *pgtbl_ops; 910 + struct io_pgtable_cfg pgtbl_cfg; 911 + enum io_pgtable_fmt fmt; 836 912 struct arm_smmu_domain *smmu_domain = domain->priv; 837 913 struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 838 914 839 - spin_lock_irqsave(&smmu_domain->lock, flags); 915 + mutex_lock(&smmu_domain->init_mutex); 840 916 if (smmu_domain->smmu) 841 917 goto out_unlock; 842 918 ··· 870 940 case ARM_SMMU_DOMAIN_S1: 871 941 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS; 872 942 start = smmu->num_s2_context_banks; 943 + ias = smmu->va_size; 944 + oas = smmu->ipa_size; 945 + if (IS_ENABLED(CONFIG_64BIT)) 946 + fmt = ARM_64_LPAE_S1; 947 + else 948 + fmt = ARM_32_LPAE_S1; 873 949 break; 874 950 case ARM_SMMU_DOMAIN_NESTED: 875 951 /* ··· 885 949 case ARM_SMMU_DOMAIN_S2: 886 950 cfg->cbar = CBAR_TYPE_S2_TRANS; 887 951 start = 0; 952 + ias = smmu->ipa_size; 953 + oas = smmu->pa_size; 954 + if (IS_ENABLED(CONFIG_64BIT)) 955 + fmt = ARM_64_LPAE_S2; 956 + else 957 + fmt = ARM_32_LPAE_S2; 888 958 break; 889 959 default: 890 960 ret = -EINVAL; ··· 910 968 cfg->irptndx = cfg->cbndx; 911 969 } 912 970 913 - ACCESS_ONCE(smmu_domain->smmu) = smmu; 914 - arm_smmu_init_context_bank(smmu_domain); 915 - spin_unlock_irqrestore(&smmu_domain->lock, flags); 971 + pgtbl_cfg = (struct io_pgtable_cfg) { 972 + .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap, 973 + .ias = ias, 974 + .oas = oas, 975 + .tlb = &arm_smmu_gather_ops, 976 + }; 916 977 978 + smmu_domain->smmu = smmu; 979 + pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain); 980 + if (!pgtbl_ops) { 981 + ret = -ENOMEM; 982 + goto out_clear_smmu; 983 + } 984 + 985 + /* Update our support page sizes to reflect the page table format */ 986 + arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap; 987 + 988 + /* Initialise the context bank with our page table cfg */ 989 + arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg); 990 + 991 + /* 992 + * Request context fault interrupt. Do this last to avoid the 993 + * handler seeing a half-initialised domain state. 994 + */ 917 995 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx]; 918 996 ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED, 919 997 "arm-smmu-context-fault", domain); ··· 943 981 cfg->irptndx = INVALID_IRPTNDX; 944 982 } 945 983 984 + mutex_unlock(&smmu_domain->init_mutex); 985 + 986 + /* Publish page table ops for map/unmap */ 987 + smmu_domain->pgtbl_ops = pgtbl_ops; 946 988 return 0; 947 989 990 + out_clear_smmu: 991 + smmu_domain->smmu = NULL; 948 992 out_unlock: 949 - spin_unlock_irqrestore(&smmu_domain->lock, flags); 993 + mutex_unlock(&smmu_domain->init_mutex); 950 994 return ret; 951 995 } 952 996 ··· 967 999 if (!smmu) 968 1000 return; 969 1001 970 - /* Disable the context bank and nuke the TLB before freeing it. */ 1002 + /* 1003 + * Disable the context bank and free the page tables before freeing 1004 + * it. 1005 + */ 971 1006 cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); 972 1007 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR); 973 - arm_smmu_tlb_inv_context(smmu_domain); 974 1008 975 1009 if (cfg->irptndx != INVALID_IRPTNDX) { 976 1010 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx]; 977 1011 free_irq(irq, domain); 978 1012 } 1013 + 1014 + if (smmu_domain->pgtbl_ops) 1015 + free_io_pgtable_ops(smmu_domain->pgtbl_ops); 979 1016 980 1017 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx); 981 1018 } ··· 988 1015 static int arm_smmu_domain_init(struct iommu_domain *domain) 989 1016 { 990 1017 struct arm_smmu_domain *smmu_domain; 991 - pgd_t *pgd; 992 1018 993 1019 /* 994 1020 * Allocate the domain and initialise some of its data structures. ··· 998 1026 if (!smmu_domain) 999 1027 return -ENOMEM; 1000 1028 1001 - pgd = kcalloc(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL); 1002 - if (!pgd) 1003 - goto out_free_domain; 1004 - smmu_domain->cfg.pgd = pgd; 1005 - 1006 - spin_lock_init(&smmu_domain->lock); 1029 + mutex_init(&smmu_domain->init_mutex); 1030 + spin_lock_init(&smmu_domain->pgtbl_lock); 1007 1031 domain->priv = smmu_domain; 1008 1032 return 0; 1009 - 1010 - out_free_domain: 1011 - kfree(smmu_domain); 1012 - return -ENOMEM; 1013 - } 1014 - 1015 - static void arm_smmu_free_ptes(pmd_t *pmd) 1016 - { 1017 - pgtable_t table = pmd_pgtable(*pmd); 1018 - 1019 - __free_page(table); 1020 - } 1021 - 1022 - static void arm_smmu_free_pmds(pud_t *pud) 1023 - { 1024 - int i; 1025 - pmd_t *pmd, *pmd_base = pmd_offset(pud, 0); 1026 - 1027 - pmd = pmd_base; 1028 - for (i = 0; i < PTRS_PER_PMD; ++i) { 1029 - if (pmd_none(*pmd)) 1030 - continue; 1031 - 1032 - arm_smmu_free_ptes(pmd); 1033 - pmd++; 1034 - } 1035 - 1036 - pmd_free(NULL, pmd_base); 1037 - } 1038 - 1039 - static void arm_smmu_free_puds(pgd_t *pgd) 1040 - { 1041 - int i; 1042 - pud_t *pud, *pud_base = pud_offset(pgd, 0); 1043 - 1044 - pud = pud_base; 1045 - for (i = 0; i < PTRS_PER_PUD; ++i) { 1046 - if (pud_none(*pud)) 1047 - continue; 1048 - 1049 - arm_smmu_free_pmds(pud); 1050 - pud++; 1051 - } 1052 - 1053 - pud_free(NULL, pud_base); 1054 - } 1055 - 1056 - static void arm_smmu_free_pgtables(struct arm_smmu_domain *smmu_domain) 1057 - { 1058 - int i; 1059 - struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 1060 - pgd_t *pgd, *pgd_base = cfg->pgd; 1061 - 1062 - /* 1063 - * Recursively free the page tables for this domain. We don't 1064 - * care about speculative TLB filling because the tables should 1065 - * not be active in any context bank at this point (SCTLR.M is 0). 1066 - */ 1067 - pgd = pgd_base; 1068 - for (i = 0; i < PTRS_PER_PGD; ++i) { 1069 - if (pgd_none(*pgd)) 1070 - continue; 1071 - arm_smmu_free_puds(pgd); 1072 - pgd++; 1073 - } 1074 - 1075 - kfree(pgd_base); 1076 1033 } 1077 1034 1078 1035 static void arm_smmu_domain_destroy(struct iommu_domain *domain) ··· 1013 1112 * already been detached. 1014 1113 */ 1015 1114 arm_smmu_destroy_domain_context(domain); 1016 - arm_smmu_free_pgtables(smmu_domain); 1017 1115 kfree(smmu_domain); 1018 1116 } 1019 1117 ··· 1144 1244 { 1145 1245 int ret; 1146 1246 struct arm_smmu_domain *smmu_domain = domain->priv; 1147 - struct arm_smmu_device *smmu, *dom_smmu; 1247 + struct arm_smmu_device *smmu; 1148 1248 struct arm_smmu_master_cfg *cfg; 1149 1249 1150 1250 smmu = find_smmu_for_device(dev); ··· 1158 1258 return -EEXIST; 1159 1259 } 1160 1260 1261 + /* Ensure that the domain is finalised */ 1262 + ret = arm_smmu_init_domain_context(domain, smmu); 1263 + if (IS_ERR_VALUE(ret)) 1264 + return ret; 1265 + 1161 1266 /* 1162 1267 * Sanity check the domain. We don't support domains across 1163 1268 * different SMMUs. 1164 1269 */ 1165 - dom_smmu = ACCESS_ONCE(smmu_domain->smmu); 1166 - if (!dom_smmu) { 1167 - /* Now that we have a master, we can finalise the domain */ 1168 - ret = arm_smmu_init_domain_context(domain, smmu); 1169 - if (IS_ERR_VALUE(ret)) 1170 - return ret; 1171 - 1172 - dom_smmu = smmu_domain->smmu; 1173 - } 1174 - 1175 - if (dom_smmu != smmu) { 1270 + if (smmu_domain->smmu != smmu) { 1176 1271 dev_err(dev, 1177 1272 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n", 1178 1273 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev)); ··· 1198 1303 arm_smmu_domain_remove_master(smmu_domain, cfg); 1199 1304 } 1200 1305 1201 - static bool arm_smmu_pte_is_contiguous_range(unsigned long addr, 1202 - unsigned long end) 1203 - { 1204 - return !(addr & ~ARM_SMMU_PTE_CONT_MASK) && 1205 - (addr + ARM_SMMU_PTE_CONT_SIZE <= end); 1206 - } 1207 - 1208 - static int arm_smmu_alloc_init_pte(struct arm_smmu_device *smmu, pmd_t *pmd, 1209 - unsigned long addr, unsigned long end, 1210 - unsigned long pfn, int prot, int stage) 1211 - { 1212 - pte_t *pte, *start; 1213 - pteval_t pteval = ARM_SMMU_PTE_PAGE | ARM_SMMU_PTE_AF; 1214 - 1215 - if (pmd_none(*pmd)) { 1216 - /* Allocate a new set of tables */ 1217 - pgtable_t table = alloc_page(GFP_ATOMIC|__GFP_ZERO); 1218 - 1219 - if (!table) 1220 - return -ENOMEM; 1221 - 1222 - arm_smmu_flush_pgtable(smmu, page_address(table), PAGE_SIZE); 1223 - pmd_populate(NULL, pmd, table); 1224 - arm_smmu_flush_pgtable(smmu, pmd, sizeof(*pmd)); 1225 - } 1226 - 1227 - if (stage == 1) { 1228 - pteval |= ARM_SMMU_PTE_AP_UNPRIV | ARM_SMMU_PTE_nG; 1229 - if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 1230 - pteval |= ARM_SMMU_PTE_AP_RDONLY; 1231 - 1232 - if (prot & IOMMU_CACHE) 1233 - pteval |= (MAIR_ATTR_IDX_CACHE << 1234 - ARM_SMMU_PTE_ATTRINDX_SHIFT); 1235 - } else { 1236 - pteval |= ARM_SMMU_PTE_HAP_FAULT; 1237 - if (prot & IOMMU_READ) 1238 - pteval |= ARM_SMMU_PTE_HAP_READ; 1239 - if (prot & IOMMU_WRITE) 1240 - pteval |= ARM_SMMU_PTE_HAP_WRITE; 1241 - if (prot & IOMMU_CACHE) 1242 - pteval |= ARM_SMMU_PTE_MEMATTR_OIWB; 1243 - else 1244 - pteval |= ARM_SMMU_PTE_MEMATTR_NC; 1245 - } 1246 - 1247 - if (prot & IOMMU_NOEXEC) 1248 - pteval |= ARM_SMMU_PTE_XN; 1249 - 1250 - /* If no access, create a faulting entry to avoid TLB fills */ 1251 - if (!(prot & (IOMMU_READ | IOMMU_WRITE))) 1252 - pteval &= ~ARM_SMMU_PTE_PAGE; 1253 - 1254 - pteval |= ARM_SMMU_PTE_SH_IS; 1255 - start = pmd_page_vaddr(*pmd) + pte_index(addr); 1256 - pte = start; 1257 - 1258 - /* 1259 - * Install the page table entries. This is fairly complicated 1260 - * since we attempt to make use of the contiguous hint in the 1261 - * ptes where possible. The contiguous hint indicates a series 1262 - * of ARM_SMMU_PTE_CONT_ENTRIES ptes mapping a physically 1263 - * contiguous region with the following constraints: 1264 - * 1265 - * - The region start is aligned to ARM_SMMU_PTE_CONT_SIZE 1266 - * - Each pte in the region has the contiguous hint bit set 1267 - * 1268 - * This complicates unmapping (also handled by this code, when 1269 - * neither IOMMU_READ or IOMMU_WRITE are set) because it is 1270 - * possible, yet highly unlikely, that a client may unmap only 1271 - * part of a contiguous range. This requires clearing of the 1272 - * contiguous hint bits in the range before installing the new 1273 - * faulting entries. 1274 - * 1275 - * Note that re-mapping an address range without first unmapping 1276 - * it is not supported, so TLB invalidation is not required here 1277 - * and is instead performed at unmap and domain-init time. 1278 - */ 1279 - do { 1280 - int i = 1; 1281 - 1282 - pteval &= ~ARM_SMMU_PTE_CONT; 1283 - 1284 - if (arm_smmu_pte_is_contiguous_range(addr, end)) { 1285 - i = ARM_SMMU_PTE_CONT_ENTRIES; 1286 - pteval |= ARM_SMMU_PTE_CONT; 1287 - } else if (pte_val(*pte) & 1288 - (ARM_SMMU_PTE_CONT | ARM_SMMU_PTE_PAGE)) { 1289 - int j; 1290 - pte_t *cont_start; 1291 - unsigned long idx = pte_index(addr); 1292 - 1293 - idx &= ~(ARM_SMMU_PTE_CONT_ENTRIES - 1); 1294 - cont_start = pmd_page_vaddr(*pmd) + idx; 1295 - for (j = 0; j < ARM_SMMU_PTE_CONT_ENTRIES; ++j) 1296 - pte_val(*(cont_start + j)) &= 1297 - ~ARM_SMMU_PTE_CONT; 1298 - 1299 - arm_smmu_flush_pgtable(smmu, cont_start, 1300 - sizeof(*pte) * 1301 - ARM_SMMU_PTE_CONT_ENTRIES); 1302 - } 1303 - 1304 - do { 1305 - *pte = pfn_pte(pfn, __pgprot(pteval)); 1306 - } while (pte++, pfn++, addr += PAGE_SIZE, --i); 1307 - } while (addr != end); 1308 - 1309 - arm_smmu_flush_pgtable(smmu, start, sizeof(*pte) * (pte - start)); 1310 - return 0; 1311 - } 1312 - 1313 - static int arm_smmu_alloc_init_pmd(struct arm_smmu_device *smmu, pud_t *pud, 1314 - unsigned long addr, unsigned long end, 1315 - phys_addr_t phys, int prot, int stage) 1316 - { 1317 - int ret; 1318 - pmd_t *pmd; 1319 - unsigned long next, pfn = __phys_to_pfn(phys); 1320 - 1321 - #ifndef __PAGETABLE_PMD_FOLDED 1322 - if (pud_none(*pud)) { 1323 - pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC); 1324 - if (!pmd) 1325 - return -ENOMEM; 1326 - 1327 - arm_smmu_flush_pgtable(smmu, pmd, PAGE_SIZE); 1328 - pud_populate(NULL, pud, pmd); 1329 - arm_smmu_flush_pgtable(smmu, pud, sizeof(*pud)); 1330 - 1331 - pmd += pmd_index(addr); 1332 - } else 1333 - #endif 1334 - pmd = pmd_offset(pud, addr); 1335 - 1336 - do { 1337 - next = pmd_addr_end(addr, end); 1338 - ret = arm_smmu_alloc_init_pte(smmu, pmd, addr, next, pfn, 1339 - prot, stage); 1340 - phys += next - addr; 1341 - pfn = __phys_to_pfn(phys); 1342 - } while (pmd++, addr = next, addr < end); 1343 - 1344 - return ret; 1345 - } 1346 - 1347 - static int arm_smmu_alloc_init_pud(struct arm_smmu_device *smmu, pgd_t *pgd, 1348 - unsigned long addr, unsigned long end, 1349 - phys_addr_t phys, int prot, int stage) 1350 - { 1351 - int ret = 0; 1352 - pud_t *pud; 1353 - unsigned long next; 1354 - 1355 - #ifndef __PAGETABLE_PUD_FOLDED 1356 - if (pgd_none(*pgd)) { 1357 - pud = (pud_t *)get_zeroed_page(GFP_ATOMIC); 1358 - if (!pud) 1359 - return -ENOMEM; 1360 - 1361 - arm_smmu_flush_pgtable(smmu, pud, PAGE_SIZE); 1362 - pgd_populate(NULL, pgd, pud); 1363 - arm_smmu_flush_pgtable(smmu, pgd, sizeof(*pgd)); 1364 - 1365 - pud += pud_index(addr); 1366 - } else 1367 - #endif 1368 - pud = pud_offset(pgd, addr); 1369 - 1370 - do { 1371 - next = pud_addr_end(addr, end); 1372 - ret = arm_smmu_alloc_init_pmd(smmu, pud, addr, next, phys, 1373 - prot, stage); 1374 - phys += next - addr; 1375 - } while (pud++, addr = next, addr < end); 1376 - 1377 - return ret; 1378 - } 1379 - 1380 - static int arm_smmu_handle_mapping(struct arm_smmu_domain *smmu_domain, 1381 - unsigned long iova, phys_addr_t paddr, 1382 - size_t size, int prot) 1383 - { 1384 - int ret, stage; 1385 - unsigned long end; 1386 - phys_addr_t input_mask, output_mask; 1387 - struct arm_smmu_device *smmu = smmu_domain->smmu; 1388 - struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 1389 - pgd_t *pgd = cfg->pgd; 1390 - unsigned long flags; 1391 - 1392 - if (cfg->cbar == CBAR_TYPE_S2_TRANS) { 1393 - stage = 2; 1394 - input_mask = (1ULL << smmu->s2_input_size) - 1; 1395 - output_mask = (1ULL << smmu->s2_output_size) - 1; 1396 - } else { 1397 - stage = 1; 1398 - input_mask = (1ULL << smmu->s1_input_size) - 1; 1399 - output_mask = (1ULL << smmu->s1_output_size) - 1; 1400 - } 1401 - 1402 - if (!pgd) 1403 - return -EINVAL; 1404 - 1405 - if (size & ~PAGE_MASK) 1406 - return -EINVAL; 1407 - 1408 - if ((phys_addr_t)iova & ~input_mask) 1409 - return -ERANGE; 1410 - 1411 - if (paddr & ~output_mask) 1412 - return -ERANGE; 1413 - 1414 - spin_lock_irqsave(&smmu_domain->lock, flags); 1415 - pgd += pgd_index(iova); 1416 - end = iova + size; 1417 - do { 1418 - unsigned long next = pgd_addr_end(iova, end); 1419 - 1420 - ret = arm_smmu_alloc_init_pud(smmu, pgd, iova, next, paddr, 1421 - prot, stage); 1422 - if (ret) 1423 - goto out_unlock; 1424 - 1425 - paddr += next - iova; 1426 - iova = next; 1427 - } while (pgd++, iova != end); 1428 - 1429 - out_unlock: 1430 - spin_unlock_irqrestore(&smmu_domain->lock, flags); 1431 - 1432 - return ret; 1433 - } 1434 - 1435 1306 static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova, 1436 1307 phys_addr_t paddr, size_t size, int prot) 1437 1308 { 1309 + int ret; 1310 + unsigned long flags; 1438 1311 struct arm_smmu_domain *smmu_domain = domain->priv; 1312 + struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops; 1439 1313 1440 - if (!smmu_domain) 1314 + if (!ops) 1441 1315 return -ENODEV; 1442 1316 1443 - return arm_smmu_handle_mapping(smmu_domain, iova, paddr, size, prot); 1317 + spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags); 1318 + ret = ops->map(ops, iova, paddr, size, prot); 1319 + spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags); 1320 + return ret; 1444 1321 } 1445 1322 1446 1323 static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova, 1447 1324 size_t size) 1448 1325 { 1449 - int ret; 1326 + size_t ret; 1327 + unsigned long flags; 1450 1328 struct arm_smmu_domain *smmu_domain = domain->priv; 1329 + struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops; 1451 1330 1452 - ret = arm_smmu_handle_mapping(smmu_domain, iova, 0, size, 0); 1453 - arm_smmu_tlb_inv_context(smmu_domain); 1454 - return ret ? 0 : size; 1331 + if (!ops) 1332 + return 0; 1333 + 1334 + spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags); 1335 + ret = ops->unmap(ops, iova, size); 1336 + spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags); 1337 + return ret; 1338 + } 1339 + 1340 + static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain, 1341 + dma_addr_t iova) 1342 + { 1343 + struct arm_smmu_domain *smmu_domain = domain->priv; 1344 + struct arm_smmu_device *smmu = smmu_domain->smmu; 1345 + struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 1346 + struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops; 1347 + struct device *dev = smmu->dev; 1348 + void __iomem *cb_base; 1349 + u32 tmp; 1350 + u64 phys; 1351 + 1352 + cb_base = ARM_SMMU_CB_BASE(smmu) + ARM_SMMU_CB(smmu, cfg->cbndx); 1353 + 1354 + if (smmu->version == 1) { 1355 + u32 reg = iova & ~0xfff; 1356 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_LO); 1357 + } else { 1358 + u32 reg = iova & ~0xfff; 1359 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_LO); 1360 + reg = ((u64)iova & ~0xfff) >> 32; 1361 + writel_relaxed(reg, cb_base + ARM_SMMU_CB_ATS1PR_HI); 1362 + } 1363 + 1364 + if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp, 1365 + !(tmp & ATSR_ACTIVE), 5, 50)) { 1366 + dev_err(dev, 1367 + "iova to phys timed out on 0x%pad. Falling back to software table walk.\n", 1368 + &iova); 1369 + return ops->iova_to_phys(ops, iova); 1370 + } 1371 + 1372 + phys = readl_relaxed(cb_base + ARM_SMMU_CB_PAR_LO); 1373 + phys |= ((u64)readl_relaxed(cb_base + ARM_SMMU_CB_PAR_HI)) << 32; 1374 + 1375 + if (phys & CB_PAR_F) { 1376 + dev_err(dev, "translation fault!\n"); 1377 + dev_err(dev, "PAR = 0x%llx\n", phys); 1378 + return 0; 1379 + } 1380 + 1381 + return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff); 1455 1382 } 1456 1383 1457 1384 static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain, 1458 - dma_addr_t iova) 1385 + dma_addr_t iova) 1459 1386 { 1460 - pgd_t *pgdp, pgd; 1461 - pud_t pud; 1462 - pmd_t pmd; 1463 - pte_t pte; 1387 + phys_addr_t ret; 1388 + unsigned long flags; 1464 1389 struct arm_smmu_domain *smmu_domain = domain->priv; 1465 - struct arm_smmu_cfg *cfg = &smmu_domain->cfg; 1390 + struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops; 1466 1391 1467 - pgdp = cfg->pgd; 1468 - if (!pgdp) 1392 + if (!ops) 1469 1393 return 0; 1470 1394 1471 - pgd = *(pgdp + pgd_index(iova)); 1472 - if (pgd_none(pgd)) 1473 - return 0; 1395 + spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags); 1396 + if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS) 1397 + ret = arm_smmu_iova_to_phys_hard(domain, iova); 1398 + else 1399 + ret = ops->iova_to_phys(ops, iova); 1400 + spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags); 1474 1401 1475 - pud = *pud_offset(&pgd, iova); 1476 - if (pud_none(pud)) 1477 - return 0; 1478 - 1479 - pmd = *pmd_offset(&pud, iova); 1480 - if (pmd_none(pmd)) 1481 - return 0; 1482 - 1483 - pte = *(pmd_page_vaddr(pmd) + pte_index(iova)); 1484 - if (pte_none(pte)) 1485 - return 0; 1486 - 1487 - return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK); 1402 + return ret; 1488 1403 } 1489 1404 1490 1405 static bool arm_smmu_capable(enum iommu_cap cap) ··· 1403 1698 static int arm_smmu_domain_set_attr(struct iommu_domain *domain, 1404 1699 enum iommu_attr attr, void *data) 1405 1700 { 1701 + int ret = 0; 1406 1702 struct arm_smmu_domain *smmu_domain = domain->priv; 1703 + 1704 + mutex_lock(&smmu_domain->init_mutex); 1407 1705 1408 1706 switch (attr) { 1409 1707 case DOMAIN_ATTR_NESTING: 1410 - if (smmu_domain->smmu) 1411 - return -EPERM; 1708 + if (smmu_domain->smmu) { 1709 + ret = -EPERM; 1710 + goto out_unlock; 1711 + } 1712 + 1412 1713 if (*(int *)data) 1413 1714 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED; 1414 1715 else 1415 1716 smmu_domain->stage = ARM_SMMU_DOMAIN_S1; 1416 1717 1417 - return 0; 1718 + break; 1418 1719 default: 1419 - return -ENODEV; 1720 + ret = -ENODEV; 1420 1721 } 1722 + 1723 + out_unlock: 1724 + mutex_unlock(&smmu_domain->init_mutex); 1725 + return ret; 1421 1726 } 1422 1727 1423 - static const struct iommu_ops arm_smmu_ops = { 1728 + static struct iommu_ops arm_smmu_ops = { 1424 1729 .capable = arm_smmu_capable, 1425 1730 .domain_init = arm_smmu_domain_init, 1426 1731 .domain_destroy = arm_smmu_domain_destroy, ··· 1444 1729 .remove_device = arm_smmu_remove_device, 1445 1730 .domain_get_attr = arm_smmu_domain_get_attr, 1446 1731 .domain_set_attr = arm_smmu_domain_set_attr, 1447 - .pgsize_bitmap = (SECTION_SIZE | 1448 - ARM_SMMU_PTE_CONT_SIZE | 1449 - PAGE_SIZE), 1732 + .pgsize_bitmap = -1UL, /* Restricted during device attach */ 1450 1733 }; 1451 1734 1452 1735 static void arm_smmu_device_reset(struct arm_smmu_device *smmu) ··· 1473 1760 } 1474 1761 1475 1762 /* Invalidate the TLB, just in case */ 1476 - writel_relaxed(0, gr0_base + ARM_SMMU_GR0_STLBIALL); 1477 1763 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLH); 1478 1764 writel_relaxed(0, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH); 1479 1765 ··· 1494 1782 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT); 1495 1783 1496 1784 /* Push the button */ 1497 - arm_smmu_tlb_sync(smmu); 1785 + __arm_smmu_tlb_sync(smmu); 1498 1786 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0); 1499 1787 } 1500 1788 ··· 1528 1816 1529 1817 /* ID0 */ 1530 1818 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0); 1531 - #ifndef CONFIG_64BIT 1532 - if (((id >> ID0_PTFS_SHIFT) & ID0_PTFS_MASK) == ID0_PTFS_V8_ONLY) { 1533 - dev_err(smmu->dev, "\tno v7 descriptor support!\n"); 1534 - return -ENODEV; 1535 - } 1536 - #endif 1537 1819 1538 1820 /* Restrict available stages based on module parameter */ 1539 1821 if (force_stage == 1) ··· 1554 1848 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) { 1555 1849 dev_err(smmu->dev, "\tno translation support!\n"); 1556 1850 return -ENODEV; 1851 + } 1852 + 1853 + if (smmu->version == 1 || (!(id & ID0_ATOSNS) && (id & ID0_S1TS))) { 1854 + smmu->features |= ARM_SMMU_FEAT_TRANS_OPS; 1855 + dev_notice(smmu->dev, "\taddress translation ops\n"); 1557 1856 } 1558 1857 1559 1858 if (id & ID0_CTTW) { ··· 1605 1894 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12; 1606 1895 1607 1896 /* Check for size mismatch of SMMU address space from mapped region */ 1608 - size = 1 << 1609 - (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1); 1897 + size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1); 1610 1898 size *= 2 << smmu->pgshift; 1611 1899 if (smmu->size != size) 1612 1900 dev_warn(smmu->dev, 1613 1901 "SMMU address space size (0x%lx) differs from mapped region size (0x%lx)!\n", 1614 1902 size, smmu->size); 1615 1903 1616 - smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & 1617 - ID1_NUMS2CB_MASK; 1904 + smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK; 1618 1905 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK; 1619 1906 if (smmu->num_s2_context_banks > smmu->num_context_banks) { 1620 1907 dev_err(smmu->dev, "impossible number of S2 context banks!\n"); ··· 1624 1915 /* ID2 */ 1625 1916 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2); 1626 1917 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK); 1627 - smmu->s1_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size); 1918 + smmu->ipa_size = size; 1628 1919 1629 - /* Stage-2 input size limited due to pgd allocation (PTRS_PER_PGD) */ 1630 - #ifdef CONFIG_64BIT 1631 - smmu->s2_input_size = min_t(unsigned long, VA_BITS, size); 1632 - #else 1633 - smmu->s2_input_size = min(32UL, size); 1634 - #endif 1635 - 1636 - /* The stage-2 output mask is also applied for bypass */ 1920 + /* The output mask is also applied for bypass */ 1637 1921 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK); 1638 - smmu->s2_output_size = min_t(unsigned long, PHYS_MASK_SHIFT, size); 1922 + smmu->pa_size = size; 1639 1923 1640 1924 if (smmu->version == ARM_SMMU_V1) { 1641 - smmu->s1_input_size = 32; 1925 + smmu->va_size = smmu->ipa_size; 1926 + size = SZ_4K | SZ_2M | SZ_1G; 1642 1927 } else { 1643 - #ifdef CONFIG_64BIT 1644 1928 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK; 1645 - size = min(VA_BITS, arm_smmu_id_size_to_bits(size)); 1646 - #else 1647 - size = 32; 1929 + smmu->va_size = arm_smmu_id_size_to_bits(size); 1930 + #ifndef CONFIG_64BIT 1931 + smmu->va_size = min(32UL, smmu->va_size); 1648 1932 #endif 1649 - smmu->s1_input_size = size; 1650 - 1651 - if ((PAGE_SIZE == SZ_4K && !(id & ID2_PTFS_4K)) || 1652 - (PAGE_SIZE == SZ_64K && !(id & ID2_PTFS_64K)) || 1653 - (PAGE_SIZE != SZ_4K && PAGE_SIZE != SZ_64K)) { 1654 - dev_err(smmu->dev, "CPU page size 0x%lx unsupported\n", 1655 - PAGE_SIZE); 1656 - return -ENODEV; 1657 - } 1933 + size = 0; 1934 + if (id & ID2_PTFS_4K) 1935 + size |= SZ_4K | SZ_2M | SZ_1G; 1936 + if (id & ID2_PTFS_16K) 1937 + size |= SZ_16K | SZ_32M; 1938 + if (id & ID2_PTFS_64K) 1939 + size |= SZ_64K | SZ_512M; 1658 1940 } 1941 + 1942 + arm_smmu_ops.pgsize_bitmap &= size; 1943 + dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n", size); 1659 1944 1660 1945 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1) 1661 1946 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n", 1662 - smmu->s1_input_size, smmu->s1_output_size); 1947 + smmu->va_size, smmu->ipa_size); 1663 1948 1664 1949 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2) 1665 1950 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n", 1666 - smmu->s2_input_size, smmu->s2_output_size); 1951 + smmu->ipa_size, smmu->pa_size); 1667 1952 1668 1953 return 0; 1669 1954 }
+98 -122
drivers/iommu/fsl_pamu.c
··· 18 18 19 19 #define pr_fmt(fmt) "fsl-pamu: %s: " fmt, __func__ 20 20 21 - #include <linux/init.h> 22 - #include <linux/iommu.h> 23 - #include <linux/slab.h> 24 - #include <linux/module.h> 25 - #include <linux/types.h> 26 - #include <linux/mm.h> 27 - #include <linux/interrupt.h> 28 - #include <linux/device.h> 29 - #include <linux/of_platform.h> 30 - #include <linux/bootmem.h> 31 - #include <linux/genalloc.h> 32 - #include <asm/io.h> 33 - #include <asm/bitops.h> 34 - #include <asm/fsl_guts.h> 35 - 36 21 #include "fsl_pamu.h" 22 + 23 + #include <linux/interrupt.h> 24 + #include <linux/genalloc.h> 25 + 26 + #include <asm/mpc85xx.h> 27 + #include <asm/fsl_guts.h> 37 28 38 29 /* define indexes for each operation mapping scenario */ 39 30 #define OMI_QMAN 0x00 ··· 35 44 #define make64(high, low) (((u64)(high) << 32) | (low)) 36 45 37 46 struct pamu_isr_data { 38 - void __iomem *pamu_reg_base; /* Base address of PAMU regs*/ 47 + void __iomem *pamu_reg_base; /* Base address of PAMU regs */ 39 48 unsigned int count; /* The number of PAMUs */ 40 49 }; 41 50 42 51 static struct paace *ppaact; 43 52 static struct paace *spaact; 44 - static struct ome *omt; 53 + static struct ome *omt __initdata; 45 54 46 55 /* 47 56 * Table for matching compatible strings, for device tree ··· 49 58 * "fsl,qoriq-device-config-2.0" corresponds to T4 & B4 50 59 * SOCs. For the older SOCs "fsl,qoriq-device-config-1.0" 51 60 * string would be used. 52 - */ 53 - static const struct of_device_id guts_device_ids[] = { 61 + */ 62 + static const struct of_device_id guts_device_ids[] __initconst = { 54 63 { .compatible = "fsl,qoriq-device-config-1.0", }, 55 64 { .compatible = "fsl,qoriq-device-config-2.0", }, 56 65 {} 57 66 }; 58 - 59 67 60 68 /* 61 69 * Table for matching compatible strings, for device tree ··· 63 73 * "fsl,b4860-l3-cache-controller" corresponds to B4 & 64 74 * "fsl,p4080-l3-cache-controller" corresponds to other, 65 75 * SOCs. 66 - */ 76 + */ 67 77 static const struct of_device_id l3_device_ids[] = { 68 78 { .compatible = "fsl,t4240-l3-cache-controller", }, 69 79 { .compatible = "fsl,b4860-l3-cache-controller", }, ··· 75 85 static u32 max_subwindow_count; 76 86 77 87 /* Pool for fspi allocation */ 78 - struct gen_pool *spaace_pool; 88 + static struct gen_pool *spaace_pool; 79 89 80 90 /** 81 91 * pamu_get_max_subwin_cnt() - Return the maximum supported ··· 160 170 static unsigned int map_addrspace_size_to_wse(phys_addr_t addrspace_size) 161 171 { 162 172 /* Bug if not a power of 2 */ 163 - BUG_ON((addrspace_size & (addrspace_size - 1))); 173 + BUG_ON(addrspace_size & (addrspace_size - 1)); 164 174 165 175 /* window size is 2^(WSE+1) bytes */ 166 176 return fls64(addrspace_size) - 2; ··· 169 179 /* Derive the PAACE window count encoding for the subwindow count */ 170 180 static unsigned int map_subwindow_cnt_to_wce(u32 subwindow_cnt) 171 181 { 172 - /* window count is 2^(WCE+1) bytes */ 173 - return __ffs(subwindow_cnt) - 1; 182 + /* window count is 2^(WCE+1) bytes */ 183 + return __ffs(subwindow_cnt) - 1; 174 184 } 175 185 176 186 /* ··· 231 241 * If no SPAACE entry is available or the allocator can not reserve the required 232 242 * number of contiguous entries function returns ULONG_MAX indicating a failure. 233 243 * 234 - */ 244 + */ 235 245 static unsigned long pamu_get_fspi_and_allocate(u32 subwin_cnt) 236 246 { 237 247 unsigned long spaace_addr; ··· 278 288 } 279 289 if (subwin) { 280 290 paace = pamu_get_spaace(paace, subwin - 1); 281 - if (!paace) { 291 + if (!paace) 282 292 return -ENOENT; 283 - } 284 293 } 285 294 set_bf(paace->impl_attr, PAACE_IA_CID, value); 286 295 ··· 300 311 } 301 312 if (subwin) { 302 313 paace = pamu_get_spaace(paace, subwin - 1); 303 - if (!paace) { 314 + if (!paace) 304 315 return -ENOENT; 305 - } 306 - set_bf(paace->addr_bitfields, PAACE_AF_V, 307 - PAACE_V_INVALID); 316 + set_bf(paace->addr_bitfields, PAACE_AF_V, PAACE_V_INVALID); 308 317 } else { 309 318 set_bf(paace->addr_bitfields, PAACE_AF_AP, 310 - PAACE_AP_PERMS_DENIED); 319 + PAACE_AP_PERMS_DENIED); 311 320 } 312 321 313 322 mb(); 314 323 315 324 return 0; 316 325 } 317 - 318 326 319 327 /** 320 328 * pamu_config_paace() - Sets up PPAACE entry for specified liodn ··· 338 352 unsigned long fspi; 339 353 340 354 if ((win_size & (win_size - 1)) || win_size < PAMU_PAGE_SIZE) { 341 - pr_debug("window size too small or not a power of two %llx\n", win_size); 355 + pr_debug("window size too small or not a power of two %pa\n", 356 + &win_size); 342 357 return -EINVAL; 343 358 } 344 359 ··· 349 362 } 350 363 351 364 ppaace = pamu_get_ppaace(liodn); 352 - if (!ppaace) { 365 + if (!ppaace) 353 366 return -ENOENT; 354 - } 355 367 356 368 /* window size is 2^(WSE+1) bytes */ 357 369 set_bf(ppaace->addr_bitfields, PPAACE_AF_WSE, 358 - map_addrspace_size_to_wse(win_size)); 370 + map_addrspace_size_to_wse(win_size)); 359 371 360 372 pamu_init_ppaace(ppaace); 361 373 ··· 428 442 { 429 443 struct paace *paace; 430 444 431 - 432 445 /* setup sub-windows */ 433 446 if (!subwin_cnt) { 434 447 pr_debug("Invalid subwindow count\n"); ··· 495 510 } 496 511 497 512 /** 498 - * get_ome_index() - Returns the index in the operation mapping table 499 - * for device. 500 - * @*omi_index: pointer for storing the index value 501 - * 502 - */ 513 + * get_ome_index() - Returns the index in the operation mapping table 514 + * for device. 515 + * @*omi_index: pointer for storing the index value 516 + * 517 + */ 503 518 void get_ome_index(u32 *omi_index, struct device *dev) 504 519 { 505 520 if (of_device_is_compatible(dev->of_node, "fsl,qman-portal")) ··· 529 544 if (stash_dest_hint == PAMU_ATTR_CACHE_L3) { 530 545 node = of_find_matching_node(NULL, l3_device_ids); 531 546 if (node) { 532 - prop = of_get_property(node, "cache-stash-id", 0); 547 + prop = of_get_property(node, "cache-stash-id", NULL); 533 548 if (!prop) { 534 - pr_debug("missing cache-stash-id at %s\n", node->full_name); 549 + pr_debug("missing cache-stash-id at %s\n", 550 + node->full_name); 535 551 of_node_put(node); 536 552 return ~(u32)0; 537 553 } ··· 556 570 /* find the hwnode that represents the cache */ 557 571 for (cache_level = PAMU_ATTR_CACHE_L1; (cache_level < PAMU_ATTR_CACHE_L3) && found; cache_level++) { 558 572 if (stash_dest_hint == cache_level) { 559 - prop = of_get_property(node, "cache-stash-id", 0); 573 + prop = of_get_property(node, "cache-stash-id", NULL); 560 574 if (!prop) { 561 - pr_debug("missing cache-stash-id at %s\n", node->full_name); 575 + pr_debug("missing cache-stash-id at %s\n", 576 + node->full_name); 562 577 of_node_put(node); 563 578 return ~(u32)0; 564 579 } ··· 567 580 return be32_to_cpup(prop); 568 581 } 569 582 570 - prop = of_get_property(node, "next-level-cache", 0); 583 + prop = of_get_property(node, "next-level-cache", NULL); 571 584 if (!prop) { 572 585 pr_debug("can't find next-level-cache at %s\n", 573 - node->full_name); 586 + node->full_name); 574 587 of_node_put(node); 575 588 return ~(u32)0; /* can't traverse any further */ 576 589 } ··· 585 598 } 586 599 587 600 pr_debug("stash dest not found for %d on vcpu %d\n", 588 - stash_dest_hint, vcpu); 601 + stash_dest_hint, vcpu); 589 602 return ~(u32)0; 590 603 } 591 604 ··· 599 612 * Memory accesses to QMAN and BMAN private memory need not be coherent, so 600 613 * clear the PAACE entry coherency attribute for them. 601 614 */ 602 - static void setup_qbman_paace(struct paace *ppaace, int paace_type) 615 + static void __init setup_qbman_paace(struct paace *ppaace, int paace_type) 603 616 { 604 617 switch (paace_type) { 605 618 case QMAN_PAACE: ··· 613 626 case QMAN_PORTAL_PAACE: 614 627 set_bf(ppaace->impl_attr, PAACE_IA_OTM, PAACE_OTM_INDEXED); 615 628 ppaace->op_encode.index_ot.omi = OMI_QMAN; 616 - /*Set DQRR and Frame stashing for the L3 cache */ 629 + /* Set DQRR and Frame stashing for the L3 cache */ 617 630 set_bf(ppaace->impl_attr, PAACE_IA_CID, get_stash_id(PAMU_ATTR_CACHE_L3, 0)); 618 631 break; 619 632 case BMAN_PAACE: ··· 666 679 * Get the maximum number of PAACT table entries 667 680 * and subwindows supported by PAMU 668 681 */ 669 - static void get_pamu_cap_values(unsigned long pamu_reg_base) 682 + static void __init get_pamu_cap_values(unsigned long pamu_reg_base) 670 683 { 671 684 u32 pc_val; 672 685 ··· 676 689 } 677 690 678 691 /* Setup PAMU registers pointing to PAACT, SPAACT and OMT */ 679 - int setup_one_pamu(unsigned long pamu_reg_base, unsigned long pamu_reg_size, 680 - phys_addr_t ppaact_phys, phys_addr_t spaact_phys, 681 - phys_addr_t omt_phys) 692 + static int __init setup_one_pamu(unsigned long pamu_reg_base, unsigned long pamu_reg_size, 693 + phys_addr_t ppaact_phys, phys_addr_t spaact_phys, 694 + phys_addr_t omt_phys) 682 695 { 683 696 u32 *pc; 684 697 struct pamu_mmap_regs *pamu_regs; ··· 714 727 */ 715 728 716 729 out_be32((u32 *)(pamu_reg_base + PAMU_PICS), 717 - PAMU_ACCESS_VIOLATION_ENABLE); 730 + PAMU_ACCESS_VIOLATION_ENABLE); 718 731 out_be32(pc, PAMU_PC_PE | PAMU_PC_OCE | PAMU_PC_SPCC | PAMU_PC_PPCC); 719 732 return 0; 720 733 } ··· 744 757 ppaace->wbah = 0; 745 758 set_bf(ppaace->addr_bitfields, PPAACE_AF_WBAL, 0); 746 759 set_bf(ppaace->impl_attr, PAACE_IA_ATM, 747 - PAACE_ATM_NO_XLATE); 760 + PAACE_ATM_NO_XLATE); 748 761 set_bf(ppaace->addr_bitfields, PAACE_AF_AP, 749 - PAACE_AP_PERMS_ALL); 762 + PAACE_AP_PERMS_ALL); 750 763 if (of_device_is_compatible(node, "fsl,qman-portal")) 751 764 setup_qbman_paace(ppaace, QMAN_PORTAL_PAACE); 752 765 if (of_device_is_compatible(node, "fsl,qman")) ··· 759 772 } 760 773 } 761 774 762 - irqreturn_t pamu_av_isr(int irq, void *arg) 775 + static irqreturn_t pamu_av_isr(int irq, void *arg) 763 776 { 764 777 struct pamu_isr_data *data = arg; 765 778 phys_addr_t phys; ··· 779 792 pr_emerg("POES2=%08x\n", in_be32(p + PAMU_POES2)); 780 793 pr_emerg("AVS1=%08x\n", avs1); 781 794 pr_emerg("AVS2=%08x\n", in_be32(p + PAMU_AVS2)); 782 - pr_emerg("AVA=%016llx\n", make64(in_be32(p + PAMU_AVAH), 783 - in_be32(p + PAMU_AVAL))); 795 + pr_emerg("AVA=%016llx\n", 796 + make64(in_be32(p + PAMU_AVAH), 797 + in_be32(p + PAMU_AVAL))); 784 798 pr_emerg("UDAD=%08x\n", in_be32(p + PAMU_UDAD)); 785 - pr_emerg("POEA=%016llx\n", make64(in_be32(p + PAMU_POEAH), 786 - in_be32(p + PAMU_POEAL))); 799 + pr_emerg("POEA=%016llx\n", 800 + make64(in_be32(p + PAMU_POEAH), 801 + in_be32(p + PAMU_POEAL))); 787 802 788 803 phys = make64(in_be32(p + PAMU_POEAH), 789 - in_be32(p + PAMU_POEAL)); 804 + in_be32(p + PAMU_POEAL)); 790 805 791 806 /* Assume that POEA points to a PAACE */ 792 807 if (phys) { ··· 796 807 797 808 /* Only the first four words are relevant */ 798 809 for (j = 0; j < 4; j++) 799 - pr_emerg("PAACE[%u]=%08x\n", j, in_be32(paace + j)); 810 + pr_emerg("PAACE[%u]=%08x\n", 811 + j, in_be32(paace + j)); 800 812 } 801 813 802 814 /* clear access violation condition */ 803 - out_be32((p + PAMU_AVS1), avs1 & PAMU_AV_MASK); 815 + out_be32(p + PAMU_AVS1, avs1 & PAMU_AV_MASK); 804 816 paace = pamu_get_ppaace(avs1 >> PAMU_AVS1_LIODN_SHIFT); 805 817 BUG_ON(!paace); 806 818 /* check if we got a violation for a disabled LIODN */ ··· 817 827 /* Disable the LIODN */ 818 828 ret = pamu_disable_liodn(avs1 >> PAMU_AVS1_LIODN_SHIFT); 819 829 BUG_ON(ret); 820 - pr_emerg("Disabling liodn %x\n", avs1 >> PAMU_AVS1_LIODN_SHIFT); 830 + pr_emerg("Disabling liodn %x\n", 831 + avs1 >> PAMU_AVS1_LIODN_SHIFT); 821 832 } 822 833 out_be32((p + PAMU_PICS), pics); 823 834 } 824 835 } 825 - 826 836 827 837 return IRQ_HANDLED; 828 838 } ··· 942 952 } 943 953 944 954 if (i == 0 || i == num_laws) { 945 - /* This should never happen*/ 955 + /* This should never happen */ 946 956 ret = -ENOENT; 947 957 goto error; 948 958 } ··· 988 998 static const struct { 989 999 u32 svr; 990 1000 u32 port_id; 991 - } port_id_map[] = { 992 - {0x82100010, 0xFF000000}, /* P2040 1.0 */ 993 - {0x82100011, 0xFF000000}, /* P2040 1.1 */ 994 - {0x82100110, 0xFF000000}, /* P2041 1.0 */ 995 - {0x82100111, 0xFF000000}, /* P2041 1.1 */ 996 - {0x82110310, 0xFF000000}, /* P3041 1.0 */ 997 - {0x82110311, 0xFF000000}, /* P3041 1.1 */ 998 - {0x82010020, 0xFFF80000}, /* P4040 2.0 */ 999 - {0x82000020, 0xFFF80000}, /* P4080 2.0 */ 1000 - {0x82210010, 0xFC000000}, /* P5010 1.0 */ 1001 - {0x82210020, 0xFC000000}, /* P5010 2.0 */ 1002 - {0x82200010, 0xFC000000}, /* P5020 1.0 */ 1003 - {0x82050010, 0xFF800000}, /* P5021 1.0 */ 1004 - {0x82040010, 0xFF800000}, /* P5040 1.0 */ 1001 + } port_id_map[] __initconst = { 1002 + {(SVR_P2040 << 8) | 0x10, 0xFF000000}, /* P2040 1.0 */ 1003 + {(SVR_P2040 << 8) | 0x11, 0xFF000000}, /* P2040 1.1 */ 1004 + {(SVR_P2041 << 8) | 0x10, 0xFF000000}, /* P2041 1.0 */ 1005 + {(SVR_P2041 << 8) | 0x11, 0xFF000000}, /* P2041 1.1 */ 1006 + {(SVR_P3041 << 8) | 0x10, 0xFF000000}, /* P3041 1.0 */ 1007 + {(SVR_P3041 << 8) | 0x11, 0xFF000000}, /* P3041 1.1 */ 1008 + {(SVR_P4040 << 8) | 0x20, 0xFFF80000}, /* P4040 2.0 */ 1009 + {(SVR_P4080 << 8) | 0x20, 0xFFF80000}, /* P4080 2.0 */ 1010 + {(SVR_P5010 << 8) | 0x10, 0xFC000000}, /* P5010 1.0 */ 1011 + {(SVR_P5010 << 8) | 0x20, 0xFC000000}, /* P5010 2.0 */ 1012 + {(SVR_P5020 << 8) | 0x10, 0xFC000000}, /* P5020 1.0 */ 1013 + {(SVR_P5021 << 8) | 0x10, 0xFF800000}, /* P5021 1.0 */ 1014 + {(SVR_P5040 << 8) | 0x10, 0xFF800000}, /* P5040 1.0 */ 1005 1015 }; 1006 1016 1007 1017 #define SVR_SECURITY 0x80000 /* The Security (E) bit */ 1008 1018 1009 1019 static int __init fsl_pamu_probe(struct platform_device *pdev) 1010 1020 { 1021 + struct device *dev = &pdev->dev; 1011 1022 void __iomem *pamu_regs = NULL; 1012 1023 struct ccsr_guts __iomem *guts_regs = NULL; 1013 1024 u32 pamubypenr, pamu_counter; ··· 1033 1042 * NOTE : All PAMUs share the same LIODN tables. 1034 1043 */ 1035 1044 1036 - pamu_regs = of_iomap(pdev->dev.of_node, 0); 1045 + pamu_regs = of_iomap(dev->of_node, 0); 1037 1046 if (!pamu_regs) { 1038 - dev_err(&pdev->dev, "ioremap of PAMU node failed\n"); 1047 + dev_err(dev, "ioremap of PAMU node failed\n"); 1039 1048 return -ENOMEM; 1040 1049 } 1041 - of_get_address(pdev->dev.of_node, 0, &size, NULL); 1050 + of_get_address(dev->of_node, 0, &size, NULL); 1042 1051 1043 - irq = irq_of_parse_and_map(pdev->dev.of_node, 0); 1052 + irq = irq_of_parse_and_map(dev->of_node, 0); 1044 1053 if (irq == NO_IRQ) { 1045 - dev_warn(&pdev->dev, "no interrupts listed in PAMU node\n"); 1054 + dev_warn(dev, "no interrupts listed in PAMU node\n"); 1046 1055 goto error; 1047 1056 } 1048 1057 1049 - data = kzalloc(sizeof(struct pamu_isr_data), GFP_KERNEL); 1058 + data = kzalloc(sizeof(*data), GFP_KERNEL); 1050 1059 if (!data) { 1051 - dev_err(&pdev->dev, "PAMU isr data memory allocation failed\n"); 1052 1060 ret = -ENOMEM; 1053 1061 goto error; 1054 1062 } ··· 1057 1067 /* The ISR needs access to the regs, so we won't iounmap them */ 1058 1068 ret = request_irq(irq, pamu_av_isr, 0, "pamu", data); 1059 1069 if (ret < 0) { 1060 - dev_err(&pdev->dev, "error %i installing ISR for irq %i\n", 1061 - ret, irq); 1070 + dev_err(dev, "error %i installing ISR for irq %i\n", ret, irq); 1062 1071 goto error; 1063 1072 } 1064 1073 1065 1074 guts_node = of_find_matching_node(NULL, guts_device_ids); 1066 1075 if (!guts_node) { 1067 - dev_err(&pdev->dev, "could not find GUTS node %s\n", 1068 - pdev->dev.of_node->full_name); 1076 + dev_err(dev, "could not find GUTS node %s\n", 1077 + dev->of_node->full_name); 1069 1078 ret = -ENODEV; 1070 1079 goto error; 1071 1080 } ··· 1072 1083 guts_regs = of_iomap(guts_node, 0); 1073 1084 of_node_put(guts_node); 1074 1085 if (!guts_regs) { 1075 - dev_err(&pdev->dev, "ioremap of GUTS node failed\n"); 1086 + dev_err(dev, "ioremap of GUTS node failed\n"); 1076 1087 ret = -ENODEV; 1077 1088 goto error; 1078 1089 } ··· 1092 1103 1093 1104 p = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); 1094 1105 if (!p) { 1095 - dev_err(&pdev->dev, "unable to allocate PAACT/SPAACT/OMT block\n"); 1106 + dev_err(dev, "unable to allocate PAACT/SPAACT/OMT block\n"); 1096 1107 ret = -ENOMEM; 1097 1108 goto error; 1098 1109 } ··· 1102 1113 1103 1114 /* Make sure the memory is naturally aligned */ 1104 1115 if (ppaact_phys & ((PAGE_SIZE << order) - 1)) { 1105 - dev_err(&pdev->dev, "PAACT/OMT block is unaligned\n"); 1116 + dev_err(dev, "PAACT/OMT block is unaligned\n"); 1106 1117 ret = -ENOMEM; 1107 1118 goto error; 1108 1119 } ··· 1110 1121 spaact = (void *)ppaact + (PAGE_SIZE << get_order(PAACT_SIZE)); 1111 1122 omt = (void *)spaact + (PAGE_SIZE << get_order(SPAACT_SIZE)); 1112 1123 1113 - dev_dbg(&pdev->dev, "ppaact virt=%p phys=0x%llx\n", ppaact, 1114 - (unsigned long long) ppaact_phys); 1124 + dev_dbg(dev, "ppaact virt=%p phys=%pa\n", ppaact, &ppaact_phys); 1115 1125 1116 1126 /* Check to see if we need to implement the work-around on this SOC */ 1117 1127 ··· 1118 1130 for (i = 0; i < ARRAY_SIZE(port_id_map); i++) { 1119 1131 if (port_id_map[i].svr == (mfspr(SPRN_SVR) & ~SVR_SECURITY)) { 1120 1132 csd_port_id = port_id_map[i].port_id; 1121 - dev_dbg(&pdev->dev, "found matching SVR %08x\n", 1133 + dev_dbg(dev, "found matching SVR %08x\n", 1122 1134 port_id_map[i].svr); 1123 1135 break; 1124 1136 } 1125 1137 } 1126 1138 1127 1139 if (csd_port_id) { 1128 - dev_dbg(&pdev->dev, "creating coherency subdomain at address " 1129 - "0x%llx, size %zu, port id 0x%08x", ppaact_phys, 1130 - mem_size, csd_port_id); 1140 + dev_dbg(dev, "creating coherency subdomain at address %pa, size %zu, port id 0x%08x", 1141 + &ppaact_phys, mem_size, csd_port_id); 1131 1142 1132 1143 ret = create_csd(ppaact_phys, mem_size, csd_port_id); 1133 1144 if (ret) { 1134 - dev_err(&pdev->dev, "could not create coherence " 1135 - "subdomain\n"); 1145 + dev_err(dev, "could not create coherence subdomain\n"); 1136 1146 return ret; 1137 1147 } 1138 1148 } ··· 1141 1155 spaace_pool = gen_pool_create(ilog2(sizeof(struct paace)), -1); 1142 1156 if (!spaace_pool) { 1143 1157 ret = -ENOMEM; 1144 - dev_err(&pdev->dev, "PAMU : failed to allocate spaace gen pool\n"); 1158 + dev_err(dev, "Failed to allocate spaace gen pool\n"); 1145 1159 goto error; 1146 1160 } 1147 1161 ··· 1154 1168 for (pamu_reg_off = 0, pamu_counter = 0x80000000; pamu_reg_off < size; 1155 1169 pamu_reg_off += PAMU_OFFSET, pamu_counter >>= 1) { 1156 1170 1157 - pamu_reg_base = (unsigned long) pamu_regs + pamu_reg_off; 1171 + pamu_reg_base = (unsigned long)pamu_regs + pamu_reg_off; 1158 1172 setup_one_pamu(pamu_reg_base, pamu_reg_off, ppaact_phys, 1159 - spaact_phys, omt_phys); 1173 + spaact_phys, omt_phys); 1160 1174 /* Disable PAMU bypass for this PAMU */ 1161 1175 pamubypenr &= ~pamu_counter; 1162 1176 } ··· 1168 1182 1169 1183 iounmap(guts_regs); 1170 1184 1171 - /* Enable DMA for the LIODNs in the device tree*/ 1185 + /* Enable DMA for the LIODNs in the device tree */ 1172 1186 1173 1187 setup_liodns(); 1174 1188 ··· 1200 1214 return ret; 1201 1215 } 1202 1216 1203 - static const struct of_device_id fsl_of_pamu_ids[] = { 1204 - { 1205 - .compatible = "fsl,p4080-pamu", 1206 - }, 1207 - { 1208 - .compatible = "fsl,pamu", 1209 - }, 1210 - {}, 1211 - }; 1212 - 1213 - static struct platform_driver fsl_of_pamu_driver = { 1217 + static struct platform_driver fsl_of_pamu_driver __initdata = { 1214 1218 .driver = { 1215 1219 .name = "fsl-of-pamu", 1216 1220 },
+8 -7
drivers/iommu/fsl_pamu.h
··· 19 19 #ifndef __FSL_PAMU_H 20 20 #define __FSL_PAMU_H 21 21 22 + #include <linux/iommu.h> 23 + 22 24 #include <asm/fsl_pamu_stash.h> 23 25 24 26 /* Bit Field macros 25 27 * v = bit field variable; m = mask, m##_SHIFT = shift, x = value to load 26 28 */ 27 - #define set_bf(v, m, x) (v = ((v) & ~(m)) | (((x) << (m##_SHIFT)) & (m))) 28 - #define get_bf(v, m) (((v) & (m)) >> (m##_SHIFT)) 29 + #define set_bf(v, m, x) (v = ((v) & ~(m)) | (((x) << m##_SHIFT) & (m))) 30 + #define get_bf(v, m) (((v) & (m)) >> m##_SHIFT) 29 31 30 32 /* PAMU CCSR space */ 31 33 #define PAMU_PGC 0x00000000 /* Allows all peripheral accesses */ ··· 67 65 #define PAMU_AVS1_GCV 0x2000 68 66 #define PAMU_AVS1_PDV 0x4000 69 67 #define PAMU_AV_MASK (PAMU_AVS1_AV | PAMU_AVS1_OTV | PAMU_AVS1_APV | PAMU_AVS1_WAV \ 70 - | PAMU_AVS1_LAV | PAMU_AVS1_GCV | PAMU_AVS1_PDV) 68 + | PAMU_AVS1_LAV | PAMU_AVS1_GCV | PAMU_AVS1_PDV) 71 69 #define PAMU_AVS1_LIODN_SHIFT 16 72 70 #define PAMU_LAV_LIODN_NOT_IN_PPAACT 0x400 73 71 ··· 200 198 #define PAACE_ATM_NO_XLATE 0x00 201 199 #define PAACE_ATM_WINDOW_XLATE 0x01 202 200 #define PAACE_ATM_PAGE_XLATE 0x02 203 - #define PAACE_ATM_WIN_PG_XLATE \ 204 - (PAACE_ATM_WINDOW_XLATE | PAACE_ATM_PAGE_XLATE) 201 + #define PAACE_ATM_WIN_PG_XLATE (PAACE_ATM_WINDOW_XLATE | PAACE_ATM_PAGE_XLATE) 205 202 #define PAACE_OTM_NO_XLATE 0x00 206 203 #define PAACE_OTM_IMMEDIATE 0x01 207 204 #define PAACE_OTM_INDEXED 0x02 ··· 220 219 #define PAACE_TCEF_FORMAT0_8B 0x00 221 220 #define PAACE_TCEF_FORMAT1_RSVD 0x01 222 221 /* 223 - * Hard coded value for the PAACT size to accomodate 222 + * Hard coded value for the PAACT size to accommodate 224 223 * maximum LIODN value generated by u-boot. 225 224 */ 226 225 #define PAACE_NUMBER_ENTRIES 0x500 ··· 333 332 #define NUM_MOE 128 334 333 struct ome { 335 334 u8 moe[NUM_MOE]; 336 - } __attribute__((packed)); 335 + } __packed; 337 336 338 337 #define PAACT_SIZE (sizeof(struct paace) * PAACE_NUMBER_ENTRIES) 339 338 #define SPAACT_SIZE (sizeof(struct paace) * SPAACE_NUMBER_ENTRIES)
+74 -99
drivers/iommu/fsl_pamu_domain.c
··· 19 19 20 20 #define pr_fmt(fmt) "fsl-pamu-domain: %s: " fmt, __func__ 21 21 22 - #include <linux/init.h> 23 - #include <linux/iommu.h> 24 - #include <linux/notifier.h> 25 - #include <linux/slab.h> 26 - #include <linux/module.h> 27 - #include <linux/types.h> 28 - #include <linux/mm.h> 29 - #include <linux/interrupt.h> 30 - #include <linux/device.h> 31 - #include <linux/of_platform.h> 32 - #include <linux/bootmem.h> 33 - #include <linux/err.h> 34 - #include <asm/io.h> 35 - #include <asm/bitops.h> 36 - 37 - #include <asm/pci-bridge.h> 38 - #include <sysdev/fsl_pci.h> 39 - 40 22 #include "fsl_pamu_domain.h" 23 + 24 + #include <sysdev/fsl_pci.h> 41 25 42 26 /* 43 27 * Global spinlock that needs to be held while ··· 35 51 36 52 static int __init iommu_init_mempool(void) 37 53 { 38 - 39 54 fsl_pamu_domain_cache = kmem_cache_create("fsl_pamu_domain", 40 - sizeof(struct fsl_dma_domain), 41 - 0, 42 - SLAB_HWCACHE_ALIGN, 43 - 44 - NULL); 55 + sizeof(struct fsl_dma_domain), 56 + 0, 57 + SLAB_HWCACHE_ALIGN, 58 + NULL); 45 59 if (!fsl_pamu_domain_cache) { 46 60 pr_debug("Couldn't create fsl iommu_domain cache\n"); 47 61 return -ENOMEM; 48 62 } 49 63 50 64 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo", 51 - sizeof(struct device_domain_info), 52 - 0, 53 - SLAB_HWCACHE_ALIGN, 54 - NULL); 65 + sizeof(struct device_domain_info), 66 + 0, 67 + SLAB_HWCACHE_ALIGN, 68 + NULL); 55 69 if (!iommu_devinfo_cache) { 56 70 pr_debug("Couldn't create devinfo cache\n"); 57 71 kmem_cache_destroy(fsl_pamu_domain_cache); ··· 62 80 static phys_addr_t get_phys_addr(struct fsl_dma_domain *dma_domain, dma_addr_t iova) 63 81 { 64 82 u32 win_cnt = dma_domain->win_cnt; 65 - struct dma_window *win_ptr = 66 - &dma_domain->win_arr[0]; 83 + struct dma_window *win_ptr = &dma_domain->win_arr[0]; 67 84 struct iommu_domain_geometry *geom; 68 85 69 86 geom = &dma_domain->iommu_domain->geometry; ··· 84 103 } 85 104 86 105 if (win_ptr->valid) 87 - return (win_ptr->paddr + (iova & (win_ptr->size - 1))); 106 + return win_ptr->paddr + (iova & (win_ptr->size - 1)); 88 107 89 108 return 0; 90 109 } 91 110 92 111 static int map_subwins(int liodn, struct fsl_dma_domain *dma_domain) 93 112 { 94 - struct dma_window *sub_win_ptr = 95 - &dma_domain->win_arr[0]; 113 + struct dma_window *sub_win_ptr = &dma_domain->win_arr[0]; 96 114 int i, ret; 97 115 unsigned long rpn, flags; 98 116 99 117 for (i = 0; i < dma_domain->win_cnt; i++) { 100 118 if (sub_win_ptr[i].valid) { 101 - rpn = sub_win_ptr[i].paddr >> 102 - PAMU_PAGE_SHIFT; 119 + rpn = sub_win_ptr[i].paddr >> PAMU_PAGE_SHIFT; 103 120 spin_lock_irqsave(&iommu_lock, flags); 104 121 ret = pamu_config_spaace(liodn, dma_domain->win_cnt, i, 105 122 sub_win_ptr[i].size, ··· 109 130 sub_win_ptr[i].prot); 110 131 spin_unlock_irqrestore(&iommu_lock, flags); 111 132 if (ret) { 112 - pr_debug("PAMU SPAACE configuration failed for liodn %d\n", 133 + pr_debug("SPAACE configuration failed for liodn %d\n", 113 134 liodn); 114 135 return ret; 115 136 } ··· 135 156 0, wnd->prot); 136 157 spin_unlock_irqrestore(&iommu_lock, flags); 137 158 if (ret) 138 - pr_debug("PAMU PAACE configuration failed for liodn %d\n", 139 - liodn); 159 + pr_debug("PAACE configuration failed for liodn %d\n", liodn); 140 160 141 161 return ret; 142 162 } ··· 147 169 return map_subwins(liodn, dma_domain); 148 170 else 149 171 return map_win(liodn, dma_domain); 150 - 151 172 } 152 173 153 174 /* Update window/subwindow mapping for the LIODN */ ··· 167 190 (wnd_nr > 0) ? 1 : 0, 168 191 wnd->prot); 169 192 if (ret) 170 - pr_debug("Subwindow reconfiguration failed for liodn %d\n", liodn); 193 + pr_debug("Subwindow reconfiguration failed for liodn %d\n", 194 + liodn); 171 195 } else { 172 196 phys_addr_t wnd_addr; 173 197 ··· 178 200 wnd->size, 179 201 ~(u32)0, 180 202 wnd->paddr >> PAMU_PAGE_SHIFT, 181 - dma_domain->snoop_id, dma_domain->stash_id, 182 - 0, wnd->prot); 203 + dma_domain->snoop_id, dma_domain->stash_id, 204 + 0, wnd->prot); 183 205 if (ret) 184 - pr_debug("Window reconfiguration failed for liodn %d\n", liodn); 206 + pr_debug("Window reconfiguration failed for liodn %d\n", 207 + liodn); 185 208 } 186 209 187 210 spin_unlock_irqrestore(&iommu_lock, flags); ··· 191 212 } 192 213 193 214 static int update_liodn_stash(int liodn, struct fsl_dma_domain *dma_domain, 194 - u32 val) 215 + u32 val) 195 216 { 196 217 int ret = 0, i; 197 218 unsigned long flags; 198 219 199 220 spin_lock_irqsave(&iommu_lock, flags); 200 221 if (!dma_domain->win_arr) { 201 - pr_debug("Windows not configured, stash destination update failed for liodn %d\n", liodn); 222 + pr_debug("Windows not configured, stash destination update failed for liodn %d\n", 223 + liodn); 202 224 spin_unlock_irqrestore(&iommu_lock, flags); 203 225 return -EINVAL; 204 226 } ··· 207 227 for (i = 0; i < dma_domain->win_cnt; i++) { 208 228 ret = pamu_update_paace_stash(liodn, i, val); 209 229 if (ret) { 210 - pr_debug("Failed to update SPAACE %d field for liodn %d\n ", i, liodn); 230 + pr_debug("Failed to update SPAACE %d field for liodn %d\n ", 231 + i, liodn); 211 232 spin_unlock_irqrestore(&iommu_lock, flags); 212 233 return ret; 213 234 } ··· 221 240 222 241 /* Set the geometry parameters for a LIODN */ 223 242 static int pamu_set_liodn(int liodn, struct device *dev, 224 - struct fsl_dma_domain *dma_domain, 225 - struct iommu_domain_geometry *geom_attr, 226 - u32 win_cnt) 243 + struct fsl_dma_domain *dma_domain, 244 + struct iommu_domain_geometry *geom_attr, 245 + u32 win_cnt) 227 246 { 228 247 phys_addr_t window_addr, window_size; 229 248 phys_addr_t subwin_size; ··· 249 268 dma_domain->stash_id, win_cnt, 0); 250 269 spin_unlock_irqrestore(&iommu_lock, flags); 251 270 if (ret) { 252 - pr_debug("PAMU PAACE configuration failed for liodn %d, win_cnt =%d\n", liodn, win_cnt); 271 + pr_debug("PAACE configuration failed for liodn %d, win_cnt =%d\n", 272 + liodn, win_cnt); 253 273 return ret; 254 274 } 255 275 ··· 267 285 0, 0); 268 286 spin_unlock_irqrestore(&iommu_lock, flags); 269 287 if (ret) { 270 - pr_debug("PAMU SPAACE configuration failed for liodn %d\n", liodn); 288 + pr_debug("SPAACE configuration failed for liodn %d\n", 289 + liodn); 271 290 return ret; 272 291 } 273 292 } ··· 284 301 * to PAMU page size. 285 302 */ 286 303 if ((size & (size - 1)) || size < PAMU_PAGE_SIZE) { 287 - pr_debug("%s: size too small or not a power of two\n", __func__); 304 + pr_debug("Size too small or not a power of two\n"); 288 305 return -EINVAL; 289 306 } 290 307 291 - /* iova must be page size aligned*/ 308 + /* iova must be page size aligned */ 292 309 if (iova & (size - 1)) { 293 - pr_debug("%s: address is not aligned with window size\n", __func__); 310 + pr_debug("Address is not aligned with window size\n"); 294 311 return -EINVAL; 295 312 } 296 313 ··· 379 396 if (!dev->archdata.iommu_domain) 380 397 dev->archdata.iommu_domain = info; 381 398 spin_unlock_irqrestore(&device_domain_lock, flags); 382 - 383 399 } 384 400 385 401 static phys_addr_t fsl_pamu_iova_to_phys(struct iommu_domain *domain, 386 - dma_addr_t iova) 402 + dma_addr_t iova) 387 403 { 388 404 struct fsl_dma_domain *dma_domain = domain->priv; 389 405 390 - if ((iova < domain->geometry.aperture_start) || 391 - iova > (domain->geometry.aperture_end)) 406 + if (iova < domain->geometry.aperture_start || 407 + iova > domain->geometry.aperture_end) 392 408 return 0; 393 409 394 410 return get_phys_addr(dma_domain, iova); ··· 442 460 443 461 list_for_each_entry(info, &dma_domain->devices, link) { 444 462 ret = pamu_set_liodn(info->liodn, info->dev, dma_domain, 445 - geom_attr, win_cnt); 463 + geom_attr, win_cnt); 446 464 if (ret) 447 465 break; 448 466 } ··· 525 543 } 526 544 527 545 spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 528 - 529 546 } 530 547 531 548 static int fsl_pamu_window_enable(struct iommu_domain *domain, u32 wnd_nr, ··· 557 576 558 577 win_size = dma_domain->geom_size >> ilog2(dma_domain->win_cnt); 559 578 if (size > win_size) { 560 - pr_debug("Invalid window size \n"); 579 + pr_debug("Invalid window size\n"); 561 580 spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 562 581 return -EINVAL; 563 582 } ··· 603 622 * and window mappings. 604 623 */ 605 624 static int handle_attach_device(struct fsl_dma_domain *dma_domain, 606 - struct device *dev, const u32 *liodn, 607 - int num) 625 + struct device *dev, const u32 *liodn, 626 + int num) 608 627 { 609 628 unsigned long flags; 610 629 struct iommu_domain *domain = dma_domain->iommu_domain; ··· 613 632 614 633 spin_lock_irqsave(&dma_domain->domain_lock, flags); 615 634 for (i = 0; i < num; i++) { 616 - 617 635 /* Ensure that LIODN value is valid */ 618 636 if (liodn[i] >= PAACE_NUMBER_ENTRIES) { 619 637 pr_debug("Invalid liodn %d, attach device failed for %s\n", 620 - liodn[i], dev->of_node->full_name); 638 + liodn[i], dev->of_node->full_name); 621 639 ret = -EINVAL; 622 640 break; 623 641 } ··· 629 649 */ 630 650 if (dma_domain->win_arr) { 631 651 u32 win_cnt = dma_domain->win_cnt > 1 ? dma_domain->win_cnt : 0; 652 + 632 653 ret = pamu_set_liodn(liodn[i], dev, dma_domain, 633 - &domain->geometry, 634 - win_cnt); 654 + &domain->geometry, win_cnt); 635 655 if (ret) 636 656 break; 637 657 if (dma_domain->mapped) { ··· 678 698 liodn = of_get_property(dev->of_node, "fsl,liodn", &len); 679 699 if (liodn) { 680 700 liodn_cnt = len / sizeof(u32); 681 - ret = handle_attach_device(dma_domain, dev, 682 - liodn, liodn_cnt); 701 + ret = handle_attach_device(dma_domain, dev, liodn, liodn_cnt); 683 702 } else { 684 703 pr_debug("missing fsl,liodn property at %s\n", 685 - dev->of_node->full_name); 686 - ret = -EINVAL; 704 + dev->of_node->full_name); 705 + ret = -EINVAL; 687 706 } 688 707 689 708 return ret; 690 709 } 691 710 692 711 static void fsl_pamu_detach_device(struct iommu_domain *domain, 693 - struct device *dev) 712 + struct device *dev) 694 713 { 695 714 struct fsl_dma_domain *dma_domain = domain->priv; 696 715 const u32 *prop; ··· 717 738 detach_device(dev, dma_domain); 718 739 else 719 740 pr_debug("missing fsl,liodn property at %s\n", 720 - dev->of_node->full_name); 741 + dev->of_node->full_name); 721 742 } 722 743 723 744 static int configure_domain_geometry(struct iommu_domain *domain, void *data) ··· 733 754 * DMA outside of the geometry. 734 755 */ 735 756 if (check_size(geom_size, geom_attr->aperture_start) || 736 - !geom_attr->force_aperture) { 737 - pr_debug("Invalid PAMU geometry attributes\n"); 738 - return -EINVAL; 739 - } 757 + !geom_attr->force_aperture) { 758 + pr_debug("Invalid PAMU geometry attributes\n"); 759 + return -EINVAL; 760 + } 740 761 741 762 spin_lock_irqsave(&dma_domain->domain_lock, flags); 742 763 if (dma_domain->enabled) { ··· 765 786 spin_lock_irqsave(&dma_domain->domain_lock, flags); 766 787 767 788 memcpy(&dma_domain->dma_stash, stash_attr, 768 - sizeof(struct pamu_stash_attribute)); 789 + sizeof(struct pamu_stash_attribute)); 769 790 770 791 dma_domain->stash_id = get_stash_id(stash_attr->cache, 771 792 stash_attr->cpu); ··· 782 803 return ret; 783 804 } 784 805 785 - /* Configure domain dma state i.e. enable/disable DMA*/ 806 + /* Configure domain dma state i.e. enable/disable DMA */ 786 807 static int configure_domain_dma_state(struct fsl_dma_domain *dma_domain, bool enable) 787 808 { 788 809 struct device_domain_info *info; ··· 798 819 } 799 820 800 821 dma_domain->enabled = enable; 801 - list_for_each_entry(info, &dma_domain->devices, 802 - link) { 822 + list_for_each_entry(info, &dma_domain->devices, link) { 803 823 ret = (enable) ? pamu_enable_liodn(info->liodn) : 804 824 pamu_disable_liodn(info->liodn); 805 825 if (ret) ··· 811 833 } 812 834 813 835 static int fsl_pamu_set_domain_attr(struct iommu_domain *domain, 814 - enum iommu_attr attr_type, void *data) 836 + enum iommu_attr attr_type, void *data) 815 837 { 816 838 struct fsl_dma_domain *dma_domain = domain->priv; 817 839 int ret = 0; 818 - 819 840 820 841 switch (attr_type) { 821 842 case DOMAIN_ATTR_GEOMETRY: ··· 830 853 pr_debug("Unsupported attribute type\n"); 831 854 ret = -EINVAL; 832 855 break; 833 - }; 856 + } 834 857 835 858 return ret; 836 859 } 837 860 838 861 static int fsl_pamu_get_domain_attr(struct iommu_domain *domain, 839 - enum iommu_attr attr_type, void *data) 862 + enum iommu_attr attr_type, void *data) 840 863 { 841 864 struct fsl_dma_domain *dma_domain = domain->priv; 842 865 int ret = 0; 843 866 844 - 845 867 switch (attr_type) { 846 868 case DOMAIN_ATTR_FSL_PAMU_STASH: 847 - memcpy((struct pamu_stash_attribute *) data, &dma_domain->dma_stash, 848 - sizeof(struct pamu_stash_attribute)); 869 + memcpy(data, &dma_domain->dma_stash, 870 + sizeof(struct pamu_stash_attribute)); 849 871 break; 850 872 case DOMAIN_ATTR_FSL_PAMU_ENABLE: 851 873 *(int *)data = dma_domain->enabled; ··· 856 880 pr_debug("Unsupported attribute type\n"); 857 881 ret = -EINVAL; 858 882 break; 859 - }; 883 + } 860 884 861 885 return ret; 862 886 } ··· 879 903 /* Check the PCI controller version number by readding BRR1 register */ 880 904 version = in_be32(pci_ctl->cfg_addr + (PCI_FSL_BRR1 >> 2)); 881 905 version &= PCI_FSL_BRR1_VER; 882 - /* If PCI controller version is >= 0x204 we can partition endpoints*/ 883 - if (version >= 0x204) 884 - return 1; 885 - 886 - return 0; 906 + /* If PCI controller version is >= 0x204 we can partition endpoints */ 907 + return version >= 0x204; 887 908 } 888 909 889 910 /* Get iommu group information from peer devices or devices on the parent bus */ ··· 941 968 if (pci_ctl->parent->iommu_group) { 942 969 group = get_device_iommu_group(pci_ctl->parent); 943 970 iommu_group_remove_device(pci_ctl->parent); 944 - } else 971 + } else { 945 972 group = get_shared_pci_device_group(pdev); 973 + } 946 974 } 947 975 948 976 if (!group) ··· 1029 1055 } 1030 1056 1031 1057 ret = pamu_set_domain_geometry(dma_domain, &domain->geometry, 1032 - ((w_count > 1) ? w_count : 0)); 1058 + w_count > 1 ? w_count : 0); 1033 1059 if (!ret) { 1034 1060 kfree(dma_domain->win_arr); 1035 - dma_domain->win_arr = kzalloc(sizeof(struct dma_window) * 1036 - w_count, GFP_ATOMIC); 1061 + dma_domain->win_arr = kcalloc(w_count, 1062 + sizeof(*dma_domain->win_arr), 1063 + GFP_ATOMIC); 1037 1064 if (!dma_domain->win_arr) { 1038 1065 spin_unlock_irqrestore(&dma_domain->domain_lock, flags); 1039 1066 return -ENOMEM; ··· 1070 1095 .remove_device = fsl_pamu_remove_device, 1071 1096 }; 1072 1097 1073 - int pamu_domain_init(void) 1098 + int __init pamu_domain_init(void) 1074 1099 { 1075 1100 int ret = 0; 1076 1101
+11 -34
drivers/iommu/intel-iommu.c
··· 71 71 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1)) 72 72 #define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT) 73 73 74 + /* IO virtual address start page frame number */ 75 + #define IOVA_START_PFN (1) 76 + 74 77 #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT) 75 78 #define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32)) 76 79 #define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64)) ··· 488 485 489 486 static struct kmem_cache *iommu_domain_cache; 490 487 static struct kmem_cache *iommu_devinfo_cache; 491 - static struct kmem_cache *iommu_iova_cache; 492 488 493 489 static inline void *alloc_pgtable_page(int node) 494 490 { ··· 523 521 static inline void free_devinfo_mem(void *vaddr) 524 522 { 525 523 kmem_cache_free(iommu_devinfo_cache, vaddr); 526 - } 527 - 528 - struct iova *alloc_iova_mem(void) 529 - { 530 - return kmem_cache_alloc(iommu_iova_cache, GFP_ATOMIC); 531 - } 532 - 533 - void free_iova_mem(struct iova *iova) 534 - { 535 - kmem_cache_free(iommu_iova_cache, iova); 536 524 } 537 525 538 526 static inline int domain_type_is_vm(struct dmar_domain *domain) ··· 1635 1643 struct iova *iova; 1636 1644 int i; 1637 1645 1638 - init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN); 1646 + init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN, 1647 + DMA_32BIT_PFN); 1639 1648 1640 1649 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock, 1641 1650 &reserved_rbtree_key); ··· 1694 1701 int adjust_width, agaw; 1695 1702 unsigned long sagaw; 1696 1703 1697 - init_iova_domain(&domain->iovad, DMA_32BIT_PFN); 1704 + init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN, 1705 + DMA_32BIT_PFN); 1698 1706 domain_reserve_special_ranges(domain); 1699 1707 1700 1708 /* calculate AGAW */ ··· 3421 3427 return ret; 3422 3428 } 3423 3429 3424 - static inline int iommu_iova_cache_init(void) 3425 - { 3426 - int ret = 0; 3427 - 3428 - iommu_iova_cache = kmem_cache_create("iommu_iova", 3429 - sizeof(struct iova), 3430 - 0, 3431 - SLAB_HWCACHE_ALIGN, 3432 - NULL); 3433 - if (!iommu_iova_cache) { 3434 - printk(KERN_ERR "Couldn't create iova cache\n"); 3435 - ret = -ENOMEM; 3436 - } 3437 - 3438 - return ret; 3439 - } 3440 - 3441 3430 static int __init iommu_init_mempool(void) 3442 3431 { 3443 3432 int ret; ··· 3438 3461 3439 3462 kmem_cache_destroy(iommu_domain_cache); 3440 3463 domain_error: 3441 - kmem_cache_destroy(iommu_iova_cache); 3464 + iommu_iova_cache_destroy(); 3442 3465 3443 3466 return -ENOMEM; 3444 3467 } ··· 3447 3470 { 3448 3471 kmem_cache_destroy(iommu_devinfo_cache); 3449 3472 kmem_cache_destroy(iommu_domain_cache); 3450 - kmem_cache_destroy(iommu_iova_cache); 3451 - 3473 + iommu_iova_cache_destroy(); 3452 3474 } 3453 3475 3454 3476 static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev) ··· 4318 4342 { 4319 4343 int adjust_width; 4320 4344 4321 - init_iova_domain(&domain->iovad, DMA_32BIT_PFN); 4345 + init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN, 4346 + DMA_32BIT_PFN); 4322 4347 domain_reserve_special_ranges(domain); 4323 4348 4324 4349 /* calculate AGAW */
+986
drivers/iommu/io-pgtable-arm.c
··· 1 + /* 2 + * CPU-agnostic ARM page table allocator. 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) 2014 ARM Limited 17 + * 18 + * Author: Will Deacon <will.deacon@arm.com> 19 + */ 20 + 21 + #define pr_fmt(fmt) "arm-lpae io-pgtable: " fmt 22 + 23 + #include <linux/iommu.h> 24 + #include <linux/kernel.h> 25 + #include <linux/sizes.h> 26 + #include <linux/slab.h> 27 + #include <linux/types.h> 28 + 29 + #include "io-pgtable.h" 30 + 31 + #define ARM_LPAE_MAX_ADDR_BITS 48 32 + #define ARM_LPAE_S2_MAX_CONCAT_PAGES 16 33 + #define ARM_LPAE_MAX_LEVELS 4 34 + 35 + /* Struct accessors */ 36 + #define io_pgtable_to_data(x) \ 37 + container_of((x), struct arm_lpae_io_pgtable, iop) 38 + 39 + #define io_pgtable_ops_to_pgtable(x) \ 40 + container_of((x), struct io_pgtable, ops) 41 + 42 + #define io_pgtable_ops_to_data(x) \ 43 + io_pgtable_to_data(io_pgtable_ops_to_pgtable(x)) 44 + 45 + /* 46 + * For consistency with the architecture, we always consider 47 + * ARM_LPAE_MAX_LEVELS levels, with the walk starting at level n >=0 48 + */ 49 + #define ARM_LPAE_START_LVL(d) (ARM_LPAE_MAX_LEVELS - (d)->levels) 50 + 51 + /* 52 + * Calculate the right shift amount to get to the portion describing level l 53 + * in a virtual address mapped by the pagetable in d. 54 + */ 55 + #define ARM_LPAE_LVL_SHIFT(l,d) \ 56 + ((((d)->levels - ((l) - ARM_LPAE_START_LVL(d) + 1)) \ 57 + * (d)->bits_per_level) + (d)->pg_shift) 58 + 59 + #define ARM_LPAE_PAGES_PER_PGD(d) ((d)->pgd_size >> (d)->pg_shift) 60 + 61 + /* 62 + * Calculate the index at level l used to map virtual address a using the 63 + * pagetable in d. 64 + */ 65 + #define ARM_LPAE_PGD_IDX(l,d) \ 66 + ((l) == ARM_LPAE_START_LVL(d) ? ilog2(ARM_LPAE_PAGES_PER_PGD(d)) : 0) 67 + 68 + #define ARM_LPAE_LVL_IDX(a,l,d) \ 69 + (((a) >> ARM_LPAE_LVL_SHIFT(l,d)) & \ 70 + ((1 << ((d)->bits_per_level + ARM_LPAE_PGD_IDX(l,d))) - 1)) 71 + 72 + /* Calculate the block/page mapping size at level l for pagetable in d. */ 73 + #define ARM_LPAE_BLOCK_SIZE(l,d) \ 74 + (1 << (ilog2(sizeof(arm_lpae_iopte)) + \ 75 + ((ARM_LPAE_MAX_LEVELS - (l)) * (d)->bits_per_level))) 76 + 77 + /* Page table bits */ 78 + #define ARM_LPAE_PTE_TYPE_SHIFT 0 79 + #define ARM_LPAE_PTE_TYPE_MASK 0x3 80 + 81 + #define ARM_LPAE_PTE_TYPE_BLOCK 1 82 + #define ARM_LPAE_PTE_TYPE_TABLE 3 83 + #define ARM_LPAE_PTE_TYPE_PAGE 3 84 + 85 + #define ARM_LPAE_PTE_NSTABLE (((arm_lpae_iopte)1) << 63) 86 + #define ARM_LPAE_PTE_XN (((arm_lpae_iopte)3) << 53) 87 + #define ARM_LPAE_PTE_AF (((arm_lpae_iopte)1) << 10) 88 + #define ARM_LPAE_PTE_SH_NS (((arm_lpae_iopte)0) << 8) 89 + #define ARM_LPAE_PTE_SH_OS (((arm_lpae_iopte)2) << 8) 90 + #define ARM_LPAE_PTE_SH_IS (((arm_lpae_iopte)3) << 8) 91 + #define ARM_LPAE_PTE_NS (((arm_lpae_iopte)1) << 5) 92 + #define ARM_LPAE_PTE_VALID (((arm_lpae_iopte)1) << 0) 93 + 94 + #define ARM_LPAE_PTE_ATTR_LO_MASK (((arm_lpae_iopte)0x3ff) << 2) 95 + /* Ignore the contiguous bit for block splitting */ 96 + #define ARM_LPAE_PTE_ATTR_HI_MASK (((arm_lpae_iopte)6) << 52) 97 + #define ARM_LPAE_PTE_ATTR_MASK (ARM_LPAE_PTE_ATTR_LO_MASK | \ 98 + ARM_LPAE_PTE_ATTR_HI_MASK) 99 + 100 + /* Stage-1 PTE */ 101 + #define ARM_LPAE_PTE_AP_UNPRIV (((arm_lpae_iopte)1) << 6) 102 + #define ARM_LPAE_PTE_AP_RDONLY (((arm_lpae_iopte)2) << 6) 103 + #define ARM_LPAE_PTE_ATTRINDX_SHIFT 2 104 + #define ARM_LPAE_PTE_nG (((arm_lpae_iopte)1) << 11) 105 + 106 + /* Stage-2 PTE */ 107 + #define ARM_LPAE_PTE_HAP_FAULT (((arm_lpae_iopte)0) << 6) 108 + #define ARM_LPAE_PTE_HAP_READ (((arm_lpae_iopte)1) << 6) 109 + #define ARM_LPAE_PTE_HAP_WRITE (((arm_lpae_iopte)2) << 6) 110 + #define ARM_LPAE_PTE_MEMATTR_OIWB (((arm_lpae_iopte)0xf) << 2) 111 + #define ARM_LPAE_PTE_MEMATTR_NC (((arm_lpae_iopte)0x5) << 2) 112 + #define ARM_LPAE_PTE_MEMATTR_DEV (((arm_lpae_iopte)0x1) << 2) 113 + 114 + /* Register bits */ 115 + #define ARM_32_LPAE_TCR_EAE (1 << 31) 116 + #define ARM_64_LPAE_S2_TCR_RES1 (1 << 31) 117 + 118 + #define ARM_LPAE_TCR_TG0_4K (0 << 14) 119 + #define ARM_LPAE_TCR_TG0_64K (1 << 14) 120 + #define ARM_LPAE_TCR_TG0_16K (2 << 14) 121 + 122 + #define ARM_LPAE_TCR_SH0_SHIFT 12 123 + #define ARM_LPAE_TCR_SH0_MASK 0x3 124 + #define ARM_LPAE_TCR_SH_NS 0 125 + #define ARM_LPAE_TCR_SH_OS 2 126 + #define ARM_LPAE_TCR_SH_IS 3 127 + 128 + #define ARM_LPAE_TCR_ORGN0_SHIFT 10 129 + #define ARM_LPAE_TCR_IRGN0_SHIFT 8 130 + #define ARM_LPAE_TCR_RGN_MASK 0x3 131 + #define ARM_LPAE_TCR_RGN_NC 0 132 + #define ARM_LPAE_TCR_RGN_WBWA 1 133 + #define ARM_LPAE_TCR_RGN_WT 2 134 + #define ARM_LPAE_TCR_RGN_WB 3 135 + 136 + #define ARM_LPAE_TCR_SL0_SHIFT 6 137 + #define ARM_LPAE_TCR_SL0_MASK 0x3 138 + 139 + #define ARM_LPAE_TCR_T0SZ_SHIFT 0 140 + #define ARM_LPAE_TCR_SZ_MASK 0xf 141 + 142 + #define ARM_LPAE_TCR_PS_SHIFT 16 143 + #define ARM_LPAE_TCR_PS_MASK 0x7 144 + 145 + #define ARM_LPAE_TCR_IPS_SHIFT 32 146 + #define ARM_LPAE_TCR_IPS_MASK 0x7 147 + 148 + #define ARM_LPAE_TCR_PS_32_BIT 0x0ULL 149 + #define ARM_LPAE_TCR_PS_36_BIT 0x1ULL 150 + #define ARM_LPAE_TCR_PS_40_BIT 0x2ULL 151 + #define ARM_LPAE_TCR_PS_42_BIT 0x3ULL 152 + #define ARM_LPAE_TCR_PS_44_BIT 0x4ULL 153 + #define ARM_LPAE_TCR_PS_48_BIT 0x5ULL 154 + 155 + #define ARM_LPAE_MAIR_ATTR_SHIFT(n) ((n) << 3) 156 + #define ARM_LPAE_MAIR_ATTR_MASK 0xff 157 + #define ARM_LPAE_MAIR_ATTR_DEVICE 0x04 158 + #define ARM_LPAE_MAIR_ATTR_NC 0x44 159 + #define ARM_LPAE_MAIR_ATTR_WBRWA 0xff 160 + #define ARM_LPAE_MAIR_ATTR_IDX_NC 0 161 + #define ARM_LPAE_MAIR_ATTR_IDX_CACHE 1 162 + #define ARM_LPAE_MAIR_ATTR_IDX_DEV 2 163 + 164 + /* IOPTE accessors */ 165 + #define iopte_deref(pte,d) \ 166 + (__va((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1) \ 167 + & ~((1ULL << (d)->pg_shift) - 1))) 168 + 169 + #define iopte_type(pte,l) \ 170 + (((pte) >> ARM_LPAE_PTE_TYPE_SHIFT) & ARM_LPAE_PTE_TYPE_MASK) 171 + 172 + #define iopte_prot(pte) ((pte) & ARM_LPAE_PTE_ATTR_MASK) 173 + 174 + #define iopte_leaf(pte,l) \ 175 + (l == (ARM_LPAE_MAX_LEVELS - 1) ? \ 176 + (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_PAGE) : \ 177 + (iopte_type(pte,l) == ARM_LPAE_PTE_TYPE_BLOCK)) 178 + 179 + #define iopte_to_pfn(pte,d) \ 180 + (((pte) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) >> (d)->pg_shift) 181 + 182 + #define pfn_to_iopte(pfn,d) \ 183 + (((pfn) << (d)->pg_shift) & ((1ULL << ARM_LPAE_MAX_ADDR_BITS) - 1)) 184 + 185 + struct arm_lpae_io_pgtable { 186 + struct io_pgtable iop; 187 + 188 + int levels; 189 + size_t pgd_size; 190 + unsigned long pg_shift; 191 + unsigned long bits_per_level; 192 + 193 + void *pgd; 194 + }; 195 + 196 + typedef u64 arm_lpae_iopte; 197 + 198 + static bool selftest_running = false; 199 + 200 + static int arm_lpae_init_pte(struct arm_lpae_io_pgtable *data, 201 + unsigned long iova, phys_addr_t paddr, 202 + arm_lpae_iopte prot, int lvl, 203 + arm_lpae_iopte *ptep) 204 + { 205 + arm_lpae_iopte pte = prot; 206 + 207 + /* We require an unmap first */ 208 + if (iopte_leaf(*ptep, lvl)) { 209 + WARN_ON(!selftest_running); 210 + return -EEXIST; 211 + } 212 + 213 + if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 214 + pte |= ARM_LPAE_PTE_NS; 215 + 216 + if (lvl == ARM_LPAE_MAX_LEVELS - 1) 217 + pte |= ARM_LPAE_PTE_TYPE_PAGE; 218 + else 219 + pte |= ARM_LPAE_PTE_TYPE_BLOCK; 220 + 221 + pte |= ARM_LPAE_PTE_AF | ARM_LPAE_PTE_SH_IS; 222 + pte |= pfn_to_iopte(paddr >> data->pg_shift, data); 223 + 224 + *ptep = pte; 225 + data->iop.cfg.tlb->flush_pgtable(ptep, sizeof(*ptep), data->iop.cookie); 226 + return 0; 227 + } 228 + 229 + static int __arm_lpae_map(struct arm_lpae_io_pgtable *data, unsigned long iova, 230 + phys_addr_t paddr, size_t size, arm_lpae_iopte prot, 231 + int lvl, arm_lpae_iopte *ptep) 232 + { 233 + arm_lpae_iopte *cptep, pte; 234 + void *cookie = data->iop.cookie; 235 + size_t block_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 236 + 237 + /* Find our entry at the current level */ 238 + ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 239 + 240 + /* If we can install a leaf entry at this level, then do so */ 241 + if (size == block_size && (size & data->iop.cfg.pgsize_bitmap)) 242 + return arm_lpae_init_pte(data, iova, paddr, prot, lvl, ptep); 243 + 244 + /* We can't allocate tables at the final level */ 245 + if (WARN_ON(lvl >= ARM_LPAE_MAX_LEVELS - 1)) 246 + return -EINVAL; 247 + 248 + /* Grab a pointer to the next level */ 249 + pte = *ptep; 250 + if (!pte) { 251 + cptep = alloc_pages_exact(1UL << data->pg_shift, 252 + GFP_ATOMIC | __GFP_ZERO); 253 + if (!cptep) 254 + return -ENOMEM; 255 + 256 + data->iop.cfg.tlb->flush_pgtable(cptep, 1UL << data->pg_shift, 257 + cookie); 258 + pte = __pa(cptep) | ARM_LPAE_PTE_TYPE_TABLE; 259 + if (data->iop.cfg.quirks & IO_PGTABLE_QUIRK_ARM_NS) 260 + pte |= ARM_LPAE_PTE_NSTABLE; 261 + *ptep = pte; 262 + data->iop.cfg.tlb->flush_pgtable(ptep, sizeof(*ptep), cookie); 263 + } else { 264 + cptep = iopte_deref(pte, data); 265 + } 266 + 267 + /* Rinse, repeat */ 268 + return __arm_lpae_map(data, iova, paddr, size, prot, lvl + 1, cptep); 269 + } 270 + 271 + static arm_lpae_iopte arm_lpae_prot_to_pte(struct arm_lpae_io_pgtable *data, 272 + int prot) 273 + { 274 + arm_lpae_iopte pte; 275 + 276 + if (data->iop.fmt == ARM_64_LPAE_S1 || 277 + data->iop.fmt == ARM_32_LPAE_S1) { 278 + pte = ARM_LPAE_PTE_AP_UNPRIV | ARM_LPAE_PTE_nG; 279 + 280 + if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 281 + pte |= ARM_LPAE_PTE_AP_RDONLY; 282 + 283 + if (prot & IOMMU_CACHE) 284 + pte |= (ARM_LPAE_MAIR_ATTR_IDX_CACHE 285 + << ARM_LPAE_PTE_ATTRINDX_SHIFT); 286 + } else { 287 + pte = ARM_LPAE_PTE_HAP_FAULT; 288 + if (prot & IOMMU_READ) 289 + pte |= ARM_LPAE_PTE_HAP_READ; 290 + if (prot & IOMMU_WRITE) 291 + pte |= ARM_LPAE_PTE_HAP_WRITE; 292 + if (prot & IOMMU_CACHE) 293 + pte |= ARM_LPAE_PTE_MEMATTR_OIWB; 294 + else 295 + pte |= ARM_LPAE_PTE_MEMATTR_NC; 296 + } 297 + 298 + if (prot & IOMMU_NOEXEC) 299 + pte |= ARM_LPAE_PTE_XN; 300 + 301 + return pte; 302 + } 303 + 304 + static int arm_lpae_map(struct io_pgtable_ops *ops, unsigned long iova, 305 + phys_addr_t paddr, size_t size, int iommu_prot) 306 + { 307 + struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 308 + arm_lpae_iopte *ptep = data->pgd; 309 + int lvl = ARM_LPAE_START_LVL(data); 310 + arm_lpae_iopte prot; 311 + 312 + /* If no access, then nothing to do */ 313 + if (!(iommu_prot & (IOMMU_READ | IOMMU_WRITE))) 314 + return 0; 315 + 316 + prot = arm_lpae_prot_to_pte(data, iommu_prot); 317 + return __arm_lpae_map(data, iova, paddr, size, prot, lvl, ptep); 318 + } 319 + 320 + static void __arm_lpae_free_pgtable(struct arm_lpae_io_pgtable *data, int lvl, 321 + arm_lpae_iopte *ptep) 322 + { 323 + arm_lpae_iopte *start, *end; 324 + unsigned long table_size; 325 + 326 + /* Only leaf entries at the last level */ 327 + if (lvl == ARM_LPAE_MAX_LEVELS - 1) 328 + return; 329 + 330 + if (lvl == ARM_LPAE_START_LVL(data)) 331 + table_size = data->pgd_size; 332 + else 333 + table_size = 1UL << data->pg_shift; 334 + 335 + start = ptep; 336 + end = (void *)ptep + table_size; 337 + 338 + while (ptep != end) { 339 + arm_lpae_iopte pte = *ptep++; 340 + 341 + if (!pte || iopte_leaf(pte, lvl)) 342 + continue; 343 + 344 + __arm_lpae_free_pgtable(data, lvl + 1, iopte_deref(pte, data)); 345 + } 346 + 347 + free_pages_exact(start, table_size); 348 + } 349 + 350 + static void arm_lpae_free_pgtable(struct io_pgtable *iop) 351 + { 352 + struct arm_lpae_io_pgtable *data = io_pgtable_to_data(iop); 353 + 354 + __arm_lpae_free_pgtable(data, ARM_LPAE_START_LVL(data), data->pgd); 355 + kfree(data); 356 + } 357 + 358 + static int arm_lpae_split_blk_unmap(struct arm_lpae_io_pgtable *data, 359 + unsigned long iova, size_t size, 360 + arm_lpae_iopte prot, int lvl, 361 + arm_lpae_iopte *ptep, size_t blk_size) 362 + { 363 + unsigned long blk_start, blk_end; 364 + phys_addr_t blk_paddr; 365 + arm_lpae_iopte table = 0; 366 + void *cookie = data->iop.cookie; 367 + const struct iommu_gather_ops *tlb = data->iop.cfg.tlb; 368 + 369 + blk_start = iova & ~(blk_size - 1); 370 + blk_end = blk_start + blk_size; 371 + blk_paddr = iopte_to_pfn(*ptep, data) << data->pg_shift; 372 + 373 + for (; blk_start < blk_end; blk_start += size, blk_paddr += size) { 374 + arm_lpae_iopte *tablep; 375 + 376 + /* Unmap! */ 377 + if (blk_start == iova) 378 + continue; 379 + 380 + /* __arm_lpae_map expects a pointer to the start of the table */ 381 + tablep = &table - ARM_LPAE_LVL_IDX(blk_start, lvl, data); 382 + if (__arm_lpae_map(data, blk_start, blk_paddr, size, prot, lvl, 383 + tablep) < 0) { 384 + if (table) { 385 + /* Free the table we allocated */ 386 + tablep = iopte_deref(table, data); 387 + __arm_lpae_free_pgtable(data, lvl + 1, tablep); 388 + } 389 + return 0; /* Bytes unmapped */ 390 + } 391 + } 392 + 393 + *ptep = table; 394 + tlb->flush_pgtable(ptep, sizeof(*ptep), cookie); 395 + iova &= ~(blk_size - 1); 396 + tlb->tlb_add_flush(iova, blk_size, true, cookie); 397 + return size; 398 + } 399 + 400 + static int __arm_lpae_unmap(struct arm_lpae_io_pgtable *data, 401 + unsigned long iova, size_t size, int lvl, 402 + arm_lpae_iopte *ptep) 403 + { 404 + arm_lpae_iopte pte; 405 + const struct iommu_gather_ops *tlb = data->iop.cfg.tlb; 406 + void *cookie = data->iop.cookie; 407 + size_t blk_size = ARM_LPAE_BLOCK_SIZE(lvl, data); 408 + 409 + ptep += ARM_LPAE_LVL_IDX(iova, lvl, data); 410 + pte = *ptep; 411 + 412 + /* Something went horribly wrong and we ran out of page table */ 413 + if (WARN_ON(!pte || (lvl == ARM_LPAE_MAX_LEVELS))) 414 + return 0; 415 + 416 + /* If the size matches this level, we're in the right place */ 417 + if (size == blk_size) { 418 + *ptep = 0; 419 + tlb->flush_pgtable(ptep, sizeof(*ptep), cookie); 420 + 421 + if (!iopte_leaf(pte, lvl)) { 422 + /* Also flush any partial walks */ 423 + tlb->tlb_add_flush(iova, size, false, cookie); 424 + tlb->tlb_sync(data->iop.cookie); 425 + ptep = iopte_deref(pte, data); 426 + __arm_lpae_free_pgtable(data, lvl + 1, ptep); 427 + } else { 428 + tlb->tlb_add_flush(iova, size, true, cookie); 429 + } 430 + 431 + return size; 432 + } else if (iopte_leaf(pte, lvl)) { 433 + /* 434 + * Insert a table at the next level to map the old region, 435 + * minus the part we want to unmap 436 + */ 437 + return arm_lpae_split_blk_unmap(data, iova, size, 438 + iopte_prot(pte), lvl, ptep, 439 + blk_size); 440 + } 441 + 442 + /* Keep on walkin' */ 443 + ptep = iopte_deref(pte, data); 444 + return __arm_lpae_unmap(data, iova, size, lvl + 1, ptep); 445 + } 446 + 447 + static int arm_lpae_unmap(struct io_pgtable_ops *ops, unsigned long iova, 448 + size_t size) 449 + { 450 + size_t unmapped; 451 + struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 452 + struct io_pgtable *iop = &data->iop; 453 + arm_lpae_iopte *ptep = data->pgd; 454 + int lvl = ARM_LPAE_START_LVL(data); 455 + 456 + unmapped = __arm_lpae_unmap(data, iova, size, lvl, ptep); 457 + if (unmapped) 458 + iop->cfg.tlb->tlb_sync(iop->cookie); 459 + 460 + return unmapped; 461 + } 462 + 463 + static phys_addr_t arm_lpae_iova_to_phys(struct io_pgtable_ops *ops, 464 + unsigned long iova) 465 + { 466 + struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 467 + arm_lpae_iopte pte, *ptep = data->pgd; 468 + int lvl = ARM_LPAE_START_LVL(data); 469 + 470 + do { 471 + /* Valid IOPTE pointer? */ 472 + if (!ptep) 473 + return 0; 474 + 475 + /* Grab the IOPTE we're interested in */ 476 + pte = *(ptep + ARM_LPAE_LVL_IDX(iova, lvl, data)); 477 + 478 + /* Valid entry? */ 479 + if (!pte) 480 + return 0; 481 + 482 + /* Leaf entry? */ 483 + if (iopte_leaf(pte,lvl)) 484 + goto found_translation; 485 + 486 + /* Take it to the next level */ 487 + ptep = iopte_deref(pte, data); 488 + } while (++lvl < ARM_LPAE_MAX_LEVELS); 489 + 490 + /* Ran out of page tables to walk */ 491 + return 0; 492 + 493 + found_translation: 494 + iova &= ((1 << data->pg_shift) - 1); 495 + return ((phys_addr_t)iopte_to_pfn(pte,data) << data->pg_shift) | iova; 496 + } 497 + 498 + static void arm_lpae_restrict_pgsizes(struct io_pgtable_cfg *cfg) 499 + { 500 + unsigned long granule; 501 + 502 + /* 503 + * We need to restrict the supported page sizes to match the 504 + * translation regime for a particular granule. Aim to match 505 + * the CPU page size if possible, otherwise prefer smaller sizes. 506 + * While we're at it, restrict the block sizes to match the 507 + * chosen granule. 508 + */ 509 + if (cfg->pgsize_bitmap & PAGE_SIZE) 510 + granule = PAGE_SIZE; 511 + else if (cfg->pgsize_bitmap & ~PAGE_MASK) 512 + granule = 1UL << __fls(cfg->pgsize_bitmap & ~PAGE_MASK); 513 + else if (cfg->pgsize_bitmap & PAGE_MASK) 514 + granule = 1UL << __ffs(cfg->pgsize_bitmap & PAGE_MASK); 515 + else 516 + granule = 0; 517 + 518 + switch (granule) { 519 + case SZ_4K: 520 + cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 521 + break; 522 + case SZ_16K: 523 + cfg->pgsize_bitmap &= (SZ_16K | SZ_32M); 524 + break; 525 + case SZ_64K: 526 + cfg->pgsize_bitmap &= (SZ_64K | SZ_512M); 527 + break; 528 + default: 529 + cfg->pgsize_bitmap = 0; 530 + } 531 + } 532 + 533 + static struct arm_lpae_io_pgtable * 534 + arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg) 535 + { 536 + unsigned long va_bits, pgd_bits; 537 + struct arm_lpae_io_pgtable *data; 538 + 539 + arm_lpae_restrict_pgsizes(cfg); 540 + 541 + if (!(cfg->pgsize_bitmap & (SZ_4K | SZ_16K | SZ_64K))) 542 + return NULL; 543 + 544 + if (cfg->ias > ARM_LPAE_MAX_ADDR_BITS) 545 + return NULL; 546 + 547 + if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS) 548 + return NULL; 549 + 550 + data = kmalloc(sizeof(*data), GFP_KERNEL); 551 + if (!data) 552 + return NULL; 553 + 554 + data->pg_shift = __ffs(cfg->pgsize_bitmap); 555 + data->bits_per_level = data->pg_shift - ilog2(sizeof(arm_lpae_iopte)); 556 + 557 + va_bits = cfg->ias - data->pg_shift; 558 + data->levels = DIV_ROUND_UP(va_bits, data->bits_per_level); 559 + 560 + /* Calculate the actual size of our pgd (without concatenation) */ 561 + pgd_bits = va_bits - (data->bits_per_level * (data->levels - 1)); 562 + data->pgd_size = 1UL << (pgd_bits + ilog2(sizeof(arm_lpae_iopte))); 563 + 564 + data->iop.ops = (struct io_pgtable_ops) { 565 + .map = arm_lpae_map, 566 + .unmap = arm_lpae_unmap, 567 + .iova_to_phys = arm_lpae_iova_to_phys, 568 + }; 569 + 570 + return data; 571 + } 572 + 573 + static struct io_pgtable * 574 + arm_64_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 575 + { 576 + u64 reg; 577 + struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg); 578 + 579 + if (!data) 580 + return NULL; 581 + 582 + /* TCR */ 583 + reg = (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | 584 + (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | 585 + (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); 586 + 587 + switch (1 << data->pg_shift) { 588 + case SZ_4K: 589 + reg |= ARM_LPAE_TCR_TG0_4K; 590 + break; 591 + case SZ_16K: 592 + reg |= ARM_LPAE_TCR_TG0_16K; 593 + break; 594 + case SZ_64K: 595 + reg |= ARM_LPAE_TCR_TG0_64K; 596 + break; 597 + } 598 + 599 + switch (cfg->oas) { 600 + case 32: 601 + reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_IPS_SHIFT); 602 + break; 603 + case 36: 604 + reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_IPS_SHIFT); 605 + break; 606 + case 40: 607 + reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_IPS_SHIFT); 608 + break; 609 + case 42: 610 + reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_IPS_SHIFT); 611 + break; 612 + case 44: 613 + reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_IPS_SHIFT); 614 + break; 615 + case 48: 616 + reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_IPS_SHIFT); 617 + break; 618 + default: 619 + goto out_free_data; 620 + } 621 + 622 + reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; 623 + cfg->arm_lpae_s1_cfg.tcr = reg; 624 + 625 + /* MAIRs */ 626 + reg = (ARM_LPAE_MAIR_ATTR_NC 627 + << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_NC)) | 628 + (ARM_LPAE_MAIR_ATTR_WBRWA 629 + << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_CACHE)) | 630 + (ARM_LPAE_MAIR_ATTR_DEVICE 631 + << ARM_LPAE_MAIR_ATTR_SHIFT(ARM_LPAE_MAIR_ATTR_IDX_DEV)); 632 + 633 + cfg->arm_lpae_s1_cfg.mair[0] = reg; 634 + cfg->arm_lpae_s1_cfg.mair[1] = 0; 635 + 636 + /* Looking good; allocate a pgd */ 637 + data->pgd = alloc_pages_exact(data->pgd_size, GFP_KERNEL | __GFP_ZERO); 638 + if (!data->pgd) 639 + goto out_free_data; 640 + 641 + cfg->tlb->flush_pgtable(data->pgd, data->pgd_size, cookie); 642 + 643 + /* TTBRs */ 644 + cfg->arm_lpae_s1_cfg.ttbr[0] = virt_to_phys(data->pgd); 645 + cfg->arm_lpae_s1_cfg.ttbr[1] = 0; 646 + return &data->iop; 647 + 648 + out_free_data: 649 + kfree(data); 650 + return NULL; 651 + } 652 + 653 + static struct io_pgtable * 654 + arm_64_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 655 + { 656 + u64 reg, sl; 657 + struct arm_lpae_io_pgtable *data = arm_lpae_alloc_pgtable(cfg); 658 + 659 + if (!data) 660 + return NULL; 661 + 662 + /* 663 + * Concatenate PGDs at level 1 if possible in order to reduce 664 + * the depth of the stage-2 walk. 665 + */ 666 + if (data->levels == ARM_LPAE_MAX_LEVELS) { 667 + unsigned long pgd_pages; 668 + 669 + pgd_pages = data->pgd_size >> ilog2(sizeof(arm_lpae_iopte)); 670 + if (pgd_pages <= ARM_LPAE_S2_MAX_CONCAT_PAGES) { 671 + data->pgd_size = pgd_pages << data->pg_shift; 672 + data->levels--; 673 + } 674 + } 675 + 676 + /* VTCR */ 677 + reg = ARM_64_LPAE_S2_TCR_RES1 | 678 + (ARM_LPAE_TCR_SH_IS << ARM_LPAE_TCR_SH0_SHIFT) | 679 + (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_IRGN0_SHIFT) | 680 + (ARM_LPAE_TCR_RGN_WBWA << ARM_LPAE_TCR_ORGN0_SHIFT); 681 + 682 + sl = ARM_LPAE_START_LVL(data); 683 + 684 + switch (1 << data->pg_shift) { 685 + case SZ_4K: 686 + reg |= ARM_LPAE_TCR_TG0_4K; 687 + sl++; /* SL0 format is different for 4K granule size */ 688 + break; 689 + case SZ_16K: 690 + reg |= ARM_LPAE_TCR_TG0_16K; 691 + break; 692 + case SZ_64K: 693 + reg |= ARM_LPAE_TCR_TG0_64K; 694 + break; 695 + } 696 + 697 + switch (cfg->oas) { 698 + case 32: 699 + reg |= (ARM_LPAE_TCR_PS_32_BIT << ARM_LPAE_TCR_PS_SHIFT); 700 + break; 701 + case 36: 702 + reg |= (ARM_LPAE_TCR_PS_36_BIT << ARM_LPAE_TCR_PS_SHIFT); 703 + break; 704 + case 40: 705 + reg |= (ARM_LPAE_TCR_PS_40_BIT << ARM_LPAE_TCR_PS_SHIFT); 706 + break; 707 + case 42: 708 + reg |= (ARM_LPAE_TCR_PS_42_BIT << ARM_LPAE_TCR_PS_SHIFT); 709 + break; 710 + case 44: 711 + reg |= (ARM_LPAE_TCR_PS_44_BIT << ARM_LPAE_TCR_PS_SHIFT); 712 + break; 713 + case 48: 714 + reg |= (ARM_LPAE_TCR_PS_48_BIT << ARM_LPAE_TCR_PS_SHIFT); 715 + break; 716 + default: 717 + goto out_free_data; 718 + } 719 + 720 + reg |= (64ULL - cfg->ias) << ARM_LPAE_TCR_T0SZ_SHIFT; 721 + reg |= (~sl & ARM_LPAE_TCR_SL0_MASK) << ARM_LPAE_TCR_SL0_SHIFT; 722 + cfg->arm_lpae_s2_cfg.vtcr = reg; 723 + 724 + /* Allocate pgd pages */ 725 + data->pgd = alloc_pages_exact(data->pgd_size, GFP_KERNEL | __GFP_ZERO); 726 + if (!data->pgd) 727 + goto out_free_data; 728 + 729 + cfg->tlb->flush_pgtable(data->pgd, data->pgd_size, cookie); 730 + 731 + /* VTTBR */ 732 + cfg->arm_lpae_s2_cfg.vttbr = virt_to_phys(data->pgd); 733 + return &data->iop; 734 + 735 + out_free_data: 736 + kfree(data); 737 + return NULL; 738 + } 739 + 740 + static struct io_pgtable * 741 + arm_32_lpae_alloc_pgtable_s1(struct io_pgtable_cfg *cfg, void *cookie) 742 + { 743 + struct io_pgtable *iop; 744 + 745 + if (cfg->ias > 32 || cfg->oas > 40) 746 + return NULL; 747 + 748 + cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 749 + iop = arm_64_lpae_alloc_pgtable_s1(cfg, cookie); 750 + if (iop) { 751 + cfg->arm_lpae_s1_cfg.tcr |= ARM_32_LPAE_TCR_EAE; 752 + cfg->arm_lpae_s1_cfg.tcr &= 0xffffffff; 753 + } 754 + 755 + return iop; 756 + } 757 + 758 + static struct io_pgtable * 759 + arm_32_lpae_alloc_pgtable_s2(struct io_pgtable_cfg *cfg, void *cookie) 760 + { 761 + struct io_pgtable *iop; 762 + 763 + if (cfg->ias > 40 || cfg->oas > 40) 764 + return NULL; 765 + 766 + cfg->pgsize_bitmap &= (SZ_4K | SZ_2M | SZ_1G); 767 + iop = arm_64_lpae_alloc_pgtable_s2(cfg, cookie); 768 + if (iop) 769 + cfg->arm_lpae_s2_cfg.vtcr &= 0xffffffff; 770 + 771 + return iop; 772 + } 773 + 774 + struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns = { 775 + .alloc = arm_64_lpae_alloc_pgtable_s1, 776 + .free = arm_lpae_free_pgtable, 777 + }; 778 + 779 + struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns = { 780 + .alloc = arm_64_lpae_alloc_pgtable_s2, 781 + .free = arm_lpae_free_pgtable, 782 + }; 783 + 784 + struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns = { 785 + .alloc = arm_32_lpae_alloc_pgtable_s1, 786 + .free = arm_lpae_free_pgtable, 787 + }; 788 + 789 + struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns = { 790 + .alloc = arm_32_lpae_alloc_pgtable_s2, 791 + .free = arm_lpae_free_pgtable, 792 + }; 793 + 794 + #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE_SELFTEST 795 + 796 + static struct io_pgtable_cfg *cfg_cookie; 797 + 798 + static void dummy_tlb_flush_all(void *cookie) 799 + { 800 + WARN_ON(cookie != cfg_cookie); 801 + } 802 + 803 + static void dummy_tlb_add_flush(unsigned long iova, size_t size, bool leaf, 804 + void *cookie) 805 + { 806 + WARN_ON(cookie != cfg_cookie); 807 + WARN_ON(!(size & cfg_cookie->pgsize_bitmap)); 808 + } 809 + 810 + static void dummy_tlb_sync(void *cookie) 811 + { 812 + WARN_ON(cookie != cfg_cookie); 813 + } 814 + 815 + static void dummy_flush_pgtable(void *ptr, size_t size, void *cookie) 816 + { 817 + WARN_ON(cookie != cfg_cookie); 818 + } 819 + 820 + static struct iommu_gather_ops dummy_tlb_ops __initdata = { 821 + .tlb_flush_all = dummy_tlb_flush_all, 822 + .tlb_add_flush = dummy_tlb_add_flush, 823 + .tlb_sync = dummy_tlb_sync, 824 + .flush_pgtable = dummy_flush_pgtable, 825 + }; 826 + 827 + static void __init arm_lpae_dump_ops(struct io_pgtable_ops *ops) 828 + { 829 + struct arm_lpae_io_pgtable *data = io_pgtable_ops_to_data(ops); 830 + struct io_pgtable_cfg *cfg = &data->iop.cfg; 831 + 832 + pr_err("cfg: pgsize_bitmap 0x%lx, ias %u-bit\n", 833 + cfg->pgsize_bitmap, cfg->ias); 834 + pr_err("data: %d levels, 0x%zx pgd_size, %lu pg_shift, %lu bits_per_level, pgd @ %p\n", 835 + data->levels, data->pgd_size, data->pg_shift, 836 + data->bits_per_level, data->pgd); 837 + } 838 + 839 + #define __FAIL(ops, i) ({ \ 840 + WARN(1, "selftest: test failed for fmt idx %d\n", (i)); \ 841 + arm_lpae_dump_ops(ops); \ 842 + selftest_running = false; \ 843 + -EFAULT; \ 844 + }) 845 + 846 + static int __init arm_lpae_run_tests(struct io_pgtable_cfg *cfg) 847 + { 848 + static const enum io_pgtable_fmt fmts[] = { 849 + ARM_64_LPAE_S1, 850 + ARM_64_LPAE_S2, 851 + }; 852 + 853 + int i, j; 854 + unsigned long iova; 855 + size_t size; 856 + struct io_pgtable_ops *ops; 857 + 858 + selftest_running = true; 859 + 860 + for (i = 0; i < ARRAY_SIZE(fmts); ++i) { 861 + cfg_cookie = cfg; 862 + ops = alloc_io_pgtable_ops(fmts[i], cfg, cfg); 863 + if (!ops) { 864 + pr_err("selftest: failed to allocate io pgtable ops\n"); 865 + return -ENOMEM; 866 + } 867 + 868 + /* 869 + * Initial sanity checks. 870 + * Empty page tables shouldn't provide any translations. 871 + */ 872 + if (ops->iova_to_phys(ops, 42)) 873 + return __FAIL(ops, i); 874 + 875 + if (ops->iova_to_phys(ops, SZ_1G + 42)) 876 + return __FAIL(ops, i); 877 + 878 + if (ops->iova_to_phys(ops, SZ_2G + 42)) 879 + return __FAIL(ops, i); 880 + 881 + /* 882 + * Distinct mappings of different granule sizes. 883 + */ 884 + iova = 0; 885 + j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG); 886 + while (j != BITS_PER_LONG) { 887 + size = 1UL << j; 888 + 889 + if (ops->map(ops, iova, iova, size, IOMMU_READ | 890 + IOMMU_WRITE | 891 + IOMMU_NOEXEC | 892 + IOMMU_CACHE)) 893 + return __FAIL(ops, i); 894 + 895 + /* Overlapping mappings */ 896 + if (!ops->map(ops, iova, iova + size, size, 897 + IOMMU_READ | IOMMU_NOEXEC)) 898 + return __FAIL(ops, i); 899 + 900 + if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 901 + return __FAIL(ops, i); 902 + 903 + iova += SZ_1G; 904 + j++; 905 + j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j); 906 + } 907 + 908 + /* Partial unmap */ 909 + size = 1UL << __ffs(cfg->pgsize_bitmap); 910 + if (ops->unmap(ops, SZ_1G + size, size) != size) 911 + return __FAIL(ops, i); 912 + 913 + /* Remap of partial unmap */ 914 + if (ops->map(ops, SZ_1G + size, size, size, IOMMU_READ)) 915 + return __FAIL(ops, i); 916 + 917 + if (ops->iova_to_phys(ops, SZ_1G + size + 42) != (size + 42)) 918 + return __FAIL(ops, i); 919 + 920 + /* Full unmap */ 921 + iova = 0; 922 + j = find_first_bit(&cfg->pgsize_bitmap, BITS_PER_LONG); 923 + while (j != BITS_PER_LONG) { 924 + size = 1UL << j; 925 + 926 + if (ops->unmap(ops, iova, size) != size) 927 + return __FAIL(ops, i); 928 + 929 + if (ops->iova_to_phys(ops, iova + 42)) 930 + return __FAIL(ops, i); 931 + 932 + /* Remap full block */ 933 + if (ops->map(ops, iova, iova, size, IOMMU_WRITE)) 934 + return __FAIL(ops, i); 935 + 936 + if (ops->iova_to_phys(ops, iova + 42) != (iova + 42)) 937 + return __FAIL(ops, i); 938 + 939 + iova += SZ_1G; 940 + j++; 941 + j = find_next_bit(&cfg->pgsize_bitmap, BITS_PER_LONG, j); 942 + } 943 + 944 + free_io_pgtable_ops(ops); 945 + } 946 + 947 + selftest_running = false; 948 + return 0; 949 + } 950 + 951 + static int __init arm_lpae_do_selftests(void) 952 + { 953 + static const unsigned long pgsize[] = { 954 + SZ_4K | SZ_2M | SZ_1G, 955 + SZ_16K | SZ_32M, 956 + SZ_64K | SZ_512M, 957 + }; 958 + 959 + static const unsigned int ias[] = { 960 + 32, 36, 40, 42, 44, 48, 961 + }; 962 + 963 + int i, j, pass = 0, fail = 0; 964 + struct io_pgtable_cfg cfg = { 965 + .tlb = &dummy_tlb_ops, 966 + .oas = 48, 967 + }; 968 + 969 + for (i = 0; i < ARRAY_SIZE(pgsize); ++i) { 970 + for (j = 0; j < ARRAY_SIZE(ias); ++j) { 971 + cfg.pgsize_bitmap = pgsize[i]; 972 + cfg.ias = ias[j]; 973 + pr_info("selftest: pgsize_bitmap 0x%08lx, IAS %u\n", 974 + pgsize[i], ias[j]); 975 + if (arm_lpae_run_tests(&cfg)) 976 + fail++; 977 + else 978 + pass++; 979 + } 980 + } 981 + 982 + pr_info("selftest: completed with %d PASS %d FAIL\n", pass, fail); 983 + return fail ? -EFAULT : 0; 984 + } 985 + subsys_initcall(arm_lpae_do_selftests); 986 + #endif
+82
drivers/iommu/io-pgtable.c
··· 1 + /* 2 + * Generic page table allocator for IOMMUs. 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) 2014 ARM Limited 17 + * 18 + * Author: Will Deacon <will.deacon@arm.com> 19 + */ 20 + 21 + #include <linux/bug.h> 22 + #include <linux/kernel.h> 23 + #include <linux/types.h> 24 + 25 + #include "io-pgtable.h" 26 + 27 + extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns; 28 + extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns; 29 + extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns; 30 + extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns; 31 + 32 + static const struct io_pgtable_init_fns * 33 + io_pgtable_init_table[IO_PGTABLE_NUM_FMTS] = 34 + { 35 + #ifdef CONFIG_IOMMU_IO_PGTABLE_LPAE 36 + [ARM_32_LPAE_S1] = &io_pgtable_arm_32_lpae_s1_init_fns, 37 + [ARM_32_LPAE_S2] = &io_pgtable_arm_32_lpae_s2_init_fns, 38 + [ARM_64_LPAE_S1] = &io_pgtable_arm_64_lpae_s1_init_fns, 39 + [ARM_64_LPAE_S2] = &io_pgtable_arm_64_lpae_s2_init_fns, 40 + #endif 41 + }; 42 + 43 + struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt, 44 + struct io_pgtable_cfg *cfg, 45 + void *cookie) 46 + { 47 + struct io_pgtable *iop; 48 + const struct io_pgtable_init_fns *fns; 49 + 50 + if (fmt >= IO_PGTABLE_NUM_FMTS) 51 + return NULL; 52 + 53 + fns = io_pgtable_init_table[fmt]; 54 + if (!fns) 55 + return NULL; 56 + 57 + iop = fns->alloc(cfg, cookie); 58 + if (!iop) 59 + return NULL; 60 + 61 + iop->fmt = fmt; 62 + iop->cookie = cookie; 63 + iop->cfg = *cfg; 64 + 65 + return &iop->ops; 66 + } 67 + 68 + /* 69 + * It is the IOMMU driver's responsibility to ensure that the page table 70 + * is no longer accessible to the walker by this point. 71 + */ 72 + void free_io_pgtable_ops(struct io_pgtable_ops *ops) 73 + { 74 + struct io_pgtable *iop; 75 + 76 + if (!ops) 77 + return; 78 + 79 + iop = container_of(ops, struct io_pgtable, ops); 80 + iop->cfg.tlb->tlb_flush_all(iop->cookie); 81 + io_pgtable_init_table[iop->fmt]->free(iop); 82 + }
+143
drivers/iommu/io-pgtable.h
··· 1 + #ifndef __IO_PGTABLE_H 2 + #define __IO_PGTABLE_H 3 + 4 + /* 5 + * Public API for use by IOMMU drivers 6 + */ 7 + enum io_pgtable_fmt { 8 + ARM_32_LPAE_S1, 9 + ARM_32_LPAE_S2, 10 + ARM_64_LPAE_S1, 11 + ARM_64_LPAE_S2, 12 + IO_PGTABLE_NUM_FMTS, 13 + }; 14 + 15 + /** 16 + * struct iommu_gather_ops - IOMMU callbacks for TLB and page table management. 17 + * 18 + * @tlb_flush_all: Synchronously invalidate the entire TLB context. 19 + * @tlb_add_flush: Queue up a TLB invalidation for a virtual address range. 20 + * @tlb_sync: Ensure any queue TLB invalidation has taken effect. 21 + * @flush_pgtable: Ensure page table updates are visible to the IOMMU. 22 + * 23 + * Note that these can all be called in atomic context and must therefore 24 + * not block. 25 + */ 26 + struct iommu_gather_ops { 27 + void (*tlb_flush_all)(void *cookie); 28 + void (*tlb_add_flush)(unsigned long iova, size_t size, bool leaf, 29 + void *cookie); 30 + void (*tlb_sync)(void *cookie); 31 + void (*flush_pgtable)(void *ptr, size_t size, void *cookie); 32 + }; 33 + 34 + /** 35 + * struct io_pgtable_cfg - Configuration data for a set of page tables. 36 + * 37 + * @quirks: A bitmap of hardware quirks that require some special 38 + * action by the low-level page table allocator. 39 + * @pgsize_bitmap: A bitmap of page sizes supported by this set of page 40 + * tables. 41 + * @ias: Input address (iova) size, in bits. 42 + * @oas: Output address (paddr) size, in bits. 43 + * @tlb: TLB management callbacks for this set of tables. 44 + */ 45 + struct io_pgtable_cfg { 46 + #define IO_PGTABLE_QUIRK_ARM_NS (1 << 0) /* Set NS bit in PTEs */ 47 + int quirks; 48 + unsigned long pgsize_bitmap; 49 + unsigned int ias; 50 + unsigned int oas; 51 + const struct iommu_gather_ops *tlb; 52 + 53 + /* Low-level data specific to the table format */ 54 + union { 55 + struct { 56 + u64 ttbr[2]; 57 + u64 tcr; 58 + u64 mair[2]; 59 + } arm_lpae_s1_cfg; 60 + 61 + struct { 62 + u64 vttbr; 63 + u64 vtcr; 64 + } arm_lpae_s2_cfg; 65 + }; 66 + }; 67 + 68 + /** 69 + * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers. 70 + * 71 + * @map: Map a physically contiguous memory region. 72 + * @unmap: Unmap a physically contiguous memory region. 73 + * @iova_to_phys: Translate iova to physical address. 74 + * 75 + * These functions map directly onto the iommu_ops member functions with 76 + * the same names. 77 + */ 78 + struct io_pgtable_ops { 79 + int (*map)(struct io_pgtable_ops *ops, unsigned long iova, 80 + phys_addr_t paddr, size_t size, int prot); 81 + int (*unmap)(struct io_pgtable_ops *ops, unsigned long iova, 82 + size_t size); 83 + phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops, 84 + unsigned long iova); 85 + }; 86 + 87 + /** 88 + * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU. 89 + * 90 + * @fmt: The page table format. 91 + * @cfg: The page table configuration. This will be modified to represent 92 + * the configuration actually provided by the allocator (e.g. the 93 + * pgsize_bitmap may be restricted). 94 + * @cookie: An opaque token provided by the IOMMU driver and passed back to 95 + * the callback routines in cfg->tlb. 96 + */ 97 + struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt, 98 + struct io_pgtable_cfg *cfg, 99 + void *cookie); 100 + 101 + /** 102 + * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller 103 + * *must* ensure that the page table is no longer 104 + * live, but the TLB can be dirty. 105 + * 106 + * @ops: The ops returned from alloc_io_pgtable_ops. 107 + */ 108 + void free_io_pgtable_ops(struct io_pgtable_ops *ops); 109 + 110 + 111 + /* 112 + * Internal structures for page table allocator implementations. 113 + */ 114 + 115 + /** 116 + * struct io_pgtable - Internal structure describing a set of page tables. 117 + * 118 + * @fmt: The page table format. 119 + * @cookie: An opaque token provided by the IOMMU driver and passed back to 120 + * any callback routines. 121 + * @cfg: A copy of the page table configuration. 122 + * @ops: The page table operations in use for this set of page tables. 123 + */ 124 + struct io_pgtable { 125 + enum io_pgtable_fmt fmt; 126 + void *cookie; 127 + struct io_pgtable_cfg cfg; 128 + struct io_pgtable_ops ops; 129 + }; 130 + 131 + /** 132 + * struct io_pgtable_init_fns - Alloc/free a set of page tables for a 133 + * particular format. 134 + * 135 + * @alloc: Allocate a set of page tables described by cfg. 136 + * @free: Free the page tables associated with iop. 137 + */ 138 + struct io_pgtable_init_fns { 139 + struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie); 140 + void (*free)(struct io_pgtable *iop); 141 + }; 142 + 143 + #endif /* __IO_PGTABLE_H */
+4 -3
drivers/iommu/iommu.c
··· 1 1 /* 2 2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. 3 - * Author: Joerg Roedel <joerg.roedel@amd.com> 3 + * Author: Joerg Roedel <jroedel@suse.de> 4 4 * 5 5 * This program is free software; you can redistribute it and/or modify it 6 6 * under the terms of the GNU General Public License version 2 as published ··· 1084 1084 if (ret) 1085 1085 iommu_unmap(domain, orig_iova, orig_size - size); 1086 1086 else 1087 - trace_map(iova, paddr, size); 1087 + trace_map(orig_iova, paddr, orig_size); 1088 1088 1089 1089 return ret; 1090 1090 } ··· 1094 1094 { 1095 1095 size_t unmapped_page, unmapped = 0; 1096 1096 unsigned int min_pagesz; 1097 + unsigned long orig_iova = iova; 1097 1098 1098 1099 if (unlikely(domain->ops->unmap == NULL || 1099 1100 domain->ops->pgsize_bitmap == 0UL)) ··· 1134 1133 unmapped += unmapped_page; 1135 1134 } 1136 1135 1137 - trace_unmap(iova, 0, size); 1136 + trace_unmap(orig_iova, size, unmapped); 1138 1137 return unmapped; 1139 1138 } 1140 1139 EXPORT_SYMBOL_GPL(iommu_unmap);
+49 -4
drivers/iommu/iova.c
··· 18 18 */ 19 19 20 20 #include <linux/iova.h> 21 + #include <linux/slab.h> 22 + 23 + static struct kmem_cache *iommu_iova_cache; 24 + 25 + int iommu_iova_cache_init(void) 26 + { 27 + int ret = 0; 28 + 29 + iommu_iova_cache = kmem_cache_create("iommu_iova", 30 + sizeof(struct iova), 31 + 0, 32 + SLAB_HWCACHE_ALIGN, 33 + NULL); 34 + if (!iommu_iova_cache) { 35 + pr_err("Couldn't create iova cache\n"); 36 + ret = -ENOMEM; 37 + } 38 + 39 + return ret; 40 + } 41 + 42 + void iommu_iova_cache_destroy(void) 43 + { 44 + kmem_cache_destroy(iommu_iova_cache); 45 + } 46 + 47 + struct iova *alloc_iova_mem(void) 48 + { 49 + return kmem_cache_alloc(iommu_iova_cache, GFP_ATOMIC); 50 + } 51 + 52 + void free_iova_mem(struct iova *iova) 53 + { 54 + kmem_cache_free(iommu_iova_cache, iova); 55 + } 21 56 22 57 void 23 - init_iova_domain(struct iova_domain *iovad, unsigned long pfn_32bit) 58 + init_iova_domain(struct iova_domain *iovad, unsigned long granule, 59 + unsigned long start_pfn, unsigned long pfn_32bit) 24 60 { 61 + /* 62 + * IOVA granularity will normally be equal to the smallest 63 + * supported IOMMU page size; both *must* be capable of 64 + * representing individual CPU pages exactly. 65 + */ 66 + BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule)); 67 + 25 68 spin_lock_init(&iovad->iova_rbtree_lock); 26 69 iovad->rbroot = RB_ROOT; 27 70 iovad->cached32_node = NULL; 71 + iovad->granule = granule; 72 + iovad->start_pfn = start_pfn; 28 73 iovad->dma_32bit_pfn = pfn_32bit; 29 74 } 30 75 ··· 172 127 if (!curr) { 173 128 if (size_aligned) 174 129 pad_size = iova_get_pad_size(size, limit_pfn); 175 - if ((IOVA_START_PFN + size + pad_size) > limit_pfn) { 130 + if ((iovad->start_pfn + size + pad_size) > limit_pfn) { 176 131 spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); 177 132 return -ENOMEM; 178 133 } ··· 247 202 * @size: - size of page frames to allocate 248 203 * @limit_pfn: - max limit address 249 204 * @size_aligned: - set if size_aligned address range is required 250 - * This function allocates an iova in the range limit_pfn to IOVA_START_PFN 251 - * looking from limit_pfn instead from IOVA_START_PFN. If the size_aligned 205 + * This function allocates an iova in the range iovad->start_pfn to limit_pfn, 206 + * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned 252 207 * flag is set then the allocated address iova->pfn_lo will be naturally 253 208 * aligned on roundup_power_of_two(size). 254 209 */
+155 -521
drivers/iommu/ipmmu-vmsa.c
··· 16 16 #include <linux/io.h> 17 17 #include <linux/iommu.h> 18 18 #include <linux/module.h> 19 - #include <linux/platform_data/ipmmu-vmsa.h> 19 + #include <linux/of.h> 20 20 #include <linux/platform_device.h> 21 21 #include <linux/sizes.h> 22 22 #include <linux/slab.h> ··· 24 24 #include <asm/dma-iommu.h> 25 25 #include <asm/pgalloc.h> 26 26 27 + #include "io-pgtable.h" 28 + 27 29 struct ipmmu_vmsa_device { 28 30 struct device *dev; 29 31 void __iomem *base; 30 32 struct list_head list; 31 33 32 - const struct ipmmu_vmsa_platform_data *pdata; 33 34 unsigned int num_utlbs; 34 35 35 36 struct dma_iommu_mapping *mapping; ··· 40 39 struct ipmmu_vmsa_device *mmu; 41 40 struct iommu_domain *io_domain; 42 41 42 + struct io_pgtable_cfg cfg; 43 + struct io_pgtable_ops *iop; 44 + 43 45 unsigned int context_id; 44 46 spinlock_t lock; /* Protects mappings */ 45 - pgd_t *pgd; 46 47 }; 47 48 48 49 struct ipmmu_vmsa_archdata { 49 50 struct ipmmu_vmsa_device *mmu; 50 - unsigned int utlb; 51 + unsigned int *utlbs; 52 + unsigned int num_utlbs; 51 53 }; 52 54 53 55 static DEFINE_SPINLOCK(ipmmu_devices_lock); ··· 61 57 /* ----------------------------------------------------------------------------- 62 58 * Registers Definition 63 59 */ 60 + 61 + #define IM_NS_ALIAS_OFFSET 0x800 64 62 65 63 #define IM_CTX_SIZE 0x40 66 64 ··· 177 171 #define IMUASID_ASID0_SHIFT 0 178 172 179 173 /* ----------------------------------------------------------------------------- 180 - * Page Table Bits 181 - */ 182 - 183 - /* 184 - * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory access, 185 - * Long-descriptor format" that the NStable bit being set in a table descriptor 186 - * will result in the NStable and NS bits of all child entries being ignored and 187 - * considered as being set. The IPMMU seems not to comply with this, as it 188 - * generates a secure access page fault if any of the NStable and NS bits isn't 189 - * set when running in non-secure mode. 190 - */ 191 - #ifndef PMD_NSTABLE 192 - #define PMD_NSTABLE (_AT(pmdval_t, 1) << 63) 193 - #endif 194 - 195 - #define ARM_VMSA_PTE_XN (((pteval_t)3) << 53) 196 - #define ARM_VMSA_PTE_CONT (((pteval_t)1) << 52) 197 - #define ARM_VMSA_PTE_AF (((pteval_t)1) << 10) 198 - #define ARM_VMSA_PTE_SH_NS (((pteval_t)0) << 8) 199 - #define ARM_VMSA_PTE_SH_OS (((pteval_t)2) << 8) 200 - #define ARM_VMSA_PTE_SH_IS (((pteval_t)3) << 8) 201 - #define ARM_VMSA_PTE_SH_MASK (((pteval_t)3) << 8) 202 - #define ARM_VMSA_PTE_NS (((pteval_t)1) << 5) 203 - #define ARM_VMSA_PTE_PAGE (((pteval_t)3) << 0) 204 - 205 - /* Stage-1 PTE */ 206 - #define ARM_VMSA_PTE_nG (((pteval_t)1) << 11) 207 - #define ARM_VMSA_PTE_AP_UNPRIV (((pteval_t)1) << 6) 208 - #define ARM_VMSA_PTE_AP_RDONLY (((pteval_t)2) << 6) 209 - #define ARM_VMSA_PTE_AP_MASK (((pteval_t)3) << 6) 210 - #define ARM_VMSA_PTE_ATTRINDX_MASK (((pteval_t)3) << 2) 211 - #define ARM_VMSA_PTE_ATTRINDX_SHIFT 2 212 - 213 - #define ARM_VMSA_PTE_ATTRS_MASK \ 214 - (ARM_VMSA_PTE_XN | ARM_VMSA_PTE_CONT | ARM_VMSA_PTE_nG | \ 215 - ARM_VMSA_PTE_AF | ARM_VMSA_PTE_SH_MASK | ARM_VMSA_PTE_AP_MASK | \ 216 - ARM_VMSA_PTE_NS | ARM_VMSA_PTE_ATTRINDX_MASK) 217 - 218 - #define ARM_VMSA_PTE_CONT_ENTRIES 16 219 - #define ARM_VMSA_PTE_CONT_SIZE (PAGE_SIZE * ARM_VMSA_PTE_CONT_ENTRIES) 220 - 221 - #define IPMMU_PTRS_PER_PTE 512 222 - #define IPMMU_PTRS_PER_PMD 512 223 - #define IPMMU_PTRS_PER_PGD 4 224 - 225 - /* ----------------------------------------------------------------------------- 226 174 * Read/Write Access 227 175 */ 228 176 ··· 265 305 ipmmu_write(mmu, IMUCTR(utlb), 0); 266 306 } 267 307 268 - static void ipmmu_flush_pgtable(struct ipmmu_vmsa_device *mmu, void *addr, 269 - size_t size) 308 + static void ipmmu_tlb_flush_all(void *cookie) 270 309 { 271 - unsigned long offset = (unsigned long)addr & ~PAGE_MASK; 310 + struct ipmmu_vmsa_domain *domain = cookie; 311 + 312 + ipmmu_tlb_invalidate(domain); 313 + } 314 + 315 + static void ipmmu_tlb_add_flush(unsigned long iova, size_t size, bool leaf, 316 + void *cookie) 317 + { 318 + /* The hardware doesn't support selective TLB flush. */ 319 + } 320 + 321 + static void ipmmu_flush_pgtable(void *ptr, size_t size, void *cookie) 322 + { 323 + unsigned long offset = (unsigned long)ptr & ~PAGE_MASK; 324 + struct ipmmu_vmsa_domain *domain = cookie; 272 325 273 326 /* 274 327 * TODO: Add support for coherent walk through CCI with DVM and remove 275 328 * cache handling. 276 329 */ 277 - dma_map_page(mmu->dev, virt_to_page(addr), offset, size, DMA_TO_DEVICE); 330 + dma_map_page(domain->mmu->dev, virt_to_page(ptr), offset, size, 331 + DMA_TO_DEVICE); 278 332 } 333 + 334 + static struct iommu_gather_ops ipmmu_gather_ops = { 335 + .tlb_flush_all = ipmmu_tlb_flush_all, 336 + .tlb_add_flush = ipmmu_tlb_add_flush, 337 + .tlb_sync = ipmmu_tlb_flush_all, 338 + .flush_pgtable = ipmmu_flush_pgtable, 339 + }; 279 340 280 341 /* ----------------------------------------------------------------------------- 281 342 * Domain/Context Management ··· 305 324 static int ipmmu_domain_init_context(struct ipmmu_vmsa_domain *domain) 306 325 { 307 326 phys_addr_t ttbr; 308 - u32 reg; 327 + 328 + /* 329 + * Allocate the page table operations. 330 + * 331 + * VMSA states in section B3.6.3 "Control of Secure or Non-secure memory 332 + * access, Long-descriptor format" that the NStable bit being set in a 333 + * table descriptor will result in the NStable and NS bits of all child 334 + * entries being ignored and considered as being set. The IPMMU seems 335 + * not to comply with this, as it generates a secure access page fault 336 + * if any of the NStable and NS bits isn't set when running in 337 + * non-secure mode. 338 + */ 339 + domain->cfg.quirks = IO_PGTABLE_QUIRK_ARM_NS; 340 + domain->cfg.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K, 341 + domain->cfg.ias = 32; 342 + domain->cfg.oas = 40; 343 + domain->cfg.tlb = &ipmmu_gather_ops; 344 + 345 + domain->iop = alloc_io_pgtable_ops(ARM_32_LPAE_S1, &domain->cfg, 346 + domain); 347 + if (!domain->iop) 348 + return -EINVAL; 309 349 310 350 /* 311 351 * TODO: When adding support for multiple contexts, find an unused ··· 335 333 domain->context_id = 0; 336 334 337 335 /* TTBR0 */ 338 - ipmmu_flush_pgtable(domain->mmu, domain->pgd, 339 - IPMMU_PTRS_PER_PGD * sizeof(*domain->pgd)); 340 - ttbr = __pa(domain->pgd); 336 + ttbr = domain->cfg.arm_lpae_s1_cfg.ttbr[0]; 341 337 ipmmu_ctx_write(domain, IMTTLBR0, ttbr); 342 338 ipmmu_ctx_write(domain, IMTTUBR0, ttbr >> 32); 343 339 ··· 348 348 IMTTBCR_SH0_INNER_SHAREABLE | IMTTBCR_ORGN0_WB_WA | 349 349 IMTTBCR_IRGN0_WB_WA | IMTTBCR_SL0_LVL_1); 350 350 351 - /* 352 - * MAIR0 353 - * We need three attributes only, non-cacheable, write-back read/write 354 - * allocate and device memory. 355 - */ 356 - reg = (IMMAIR_ATTR_NC << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_NC)) 357 - | (IMMAIR_ATTR_WBRWA << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_WBRWA)) 358 - | (IMMAIR_ATTR_DEVICE << IMMAIR_ATTR_SHIFT(IMMAIR_ATTR_IDX_DEV)); 359 - ipmmu_ctx_write(domain, IMMAIR0, reg); 351 + /* MAIR0 */ 352 + ipmmu_ctx_write(domain, IMMAIR0, domain->cfg.arm_lpae_s1_cfg.mair[0]); 360 353 361 354 /* IMBUSCR */ 362 355 ipmmu_ctx_write(domain, IMBUSCR, ··· 454 461 } 455 462 456 463 /* ----------------------------------------------------------------------------- 457 - * Page Table Management 458 - */ 459 - 460 - #define pud_pgtable(pud) pfn_to_page(__phys_to_pfn(pud_val(pud) & PHYS_MASK)) 461 - 462 - static void ipmmu_free_ptes(pmd_t *pmd) 463 - { 464 - pgtable_t table = pmd_pgtable(*pmd); 465 - __free_page(table); 466 - } 467 - 468 - static void ipmmu_free_pmds(pud_t *pud) 469 - { 470 - pmd_t *pmd = pmd_offset(pud, 0); 471 - pgtable_t table; 472 - unsigned int i; 473 - 474 - for (i = 0; i < IPMMU_PTRS_PER_PMD; ++i) { 475 - if (!pmd_table(*pmd)) 476 - continue; 477 - 478 - ipmmu_free_ptes(pmd); 479 - pmd++; 480 - } 481 - 482 - table = pud_pgtable(*pud); 483 - __free_page(table); 484 - } 485 - 486 - static void ipmmu_free_pgtables(struct ipmmu_vmsa_domain *domain) 487 - { 488 - pgd_t *pgd, *pgd_base = domain->pgd; 489 - unsigned int i; 490 - 491 - /* 492 - * Recursively free the page tables for this domain. We don't care about 493 - * speculative TLB filling, because the TLB will be nuked next time this 494 - * context bank is re-allocated and no devices currently map to these 495 - * tables. 496 - */ 497 - pgd = pgd_base; 498 - for (i = 0; i < IPMMU_PTRS_PER_PGD; ++i) { 499 - if (pgd_none(*pgd)) 500 - continue; 501 - ipmmu_free_pmds((pud_t *)pgd); 502 - pgd++; 503 - } 504 - 505 - kfree(pgd_base); 506 - } 507 - 508 - /* 509 - * We can't use the (pgd|pud|pmd|pte)_populate or the set_(pgd|pud|pmd|pte) 510 - * functions as they would flush the CPU TLB. 511 - */ 512 - 513 - static pte_t *ipmmu_alloc_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd, 514 - unsigned long iova) 515 - { 516 - pte_t *pte; 517 - 518 - if (!pmd_none(*pmd)) 519 - return pte_offset_kernel(pmd, iova); 520 - 521 - pte = (pte_t *)get_zeroed_page(GFP_ATOMIC); 522 - if (!pte) 523 - return NULL; 524 - 525 - ipmmu_flush_pgtable(mmu, pte, PAGE_SIZE); 526 - *pmd = __pmd(__pa(pte) | PMD_NSTABLE | PMD_TYPE_TABLE); 527 - ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd)); 528 - 529 - return pte + pte_index(iova); 530 - } 531 - 532 - static pmd_t *ipmmu_alloc_pmd(struct ipmmu_vmsa_device *mmu, pgd_t *pgd, 533 - unsigned long iova) 534 - { 535 - pud_t *pud = (pud_t *)pgd; 536 - pmd_t *pmd; 537 - 538 - if (!pud_none(*pud)) 539 - return pmd_offset(pud, iova); 540 - 541 - pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC); 542 - if (!pmd) 543 - return NULL; 544 - 545 - ipmmu_flush_pgtable(mmu, pmd, PAGE_SIZE); 546 - *pud = __pud(__pa(pmd) | PMD_NSTABLE | PMD_TYPE_TABLE); 547 - ipmmu_flush_pgtable(mmu, pud, sizeof(*pud)); 548 - 549 - return pmd + pmd_index(iova); 550 - } 551 - 552 - static u64 ipmmu_page_prot(unsigned int prot, u64 type) 553 - { 554 - u64 pgprot = ARM_VMSA_PTE_nG | ARM_VMSA_PTE_AF 555 - | ARM_VMSA_PTE_SH_IS | ARM_VMSA_PTE_AP_UNPRIV 556 - | ARM_VMSA_PTE_NS | type; 557 - 558 - if (!(prot & IOMMU_WRITE) && (prot & IOMMU_READ)) 559 - pgprot |= ARM_VMSA_PTE_AP_RDONLY; 560 - 561 - if (prot & IOMMU_CACHE) 562 - pgprot |= IMMAIR_ATTR_IDX_WBRWA << ARM_VMSA_PTE_ATTRINDX_SHIFT; 563 - 564 - if (prot & IOMMU_NOEXEC) 565 - pgprot |= ARM_VMSA_PTE_XN; 566 - else if (!(prot & (IOMMU_READ | IOMMU_WRITE))) 567 - /* If no access create a faulting entry to avoid TLB fills. */ 568 - pgprot &= ~ARM_VMSA_PTE_PAGE; 569 - 570 - return pgprot; 571 - } 572 - 573 - static int ipmmu_alloc_init_pte(struct ipmmu_vmsa_device *mmu, pmd_t *pmd, 574 - unsigned long iova, unsigned long pfn, 575 - size_t size, int prot) 576 - { 577 - pteval_t pteval = ipmmu_page_prot(prot, ARM_VMSA_PTE_PAGE); 578 - unsigned int num_ptes = 1; 579 - pte_t *pte, *start; 580 - unsigned int i; 581 - 582 - pte = ipmmu_alloc_pte(mmu, pmd, iova); 583 - if (!pte) 584 - return -ENOMEM; 585 - 586 - start = pte; 587 - 588 - /* 589 - * Install the page table entries. We can be called both for a single 590 - * page or for a block of 16 physically contiguous pages. In the latter 591 - * case set the PTE contiguous hint. 592 - */ 593 - if (size == SZ_64K) { 594 - pteval |= ARM_VMSA_PTE_CONT; 595 - num_ptes = ARM_VMSA_PTE_CONT_ENTRIES; 596 - } 597 - 598 - for (i = num_ptes; i; --i) 599 - *pte++ = pfn_pte(pfn++, __pgprot(pteval)); 600 - 601 - ipmmu_flush_pgtable(mmu, start, sizeof(*pte) * num_ptes); 602 - 603 - return 0; 604 - } 605 - 606 - static int ipmmu_alloc_init_pmd(struct ipmmu_vmsa_device *mmu, pmd_t *pmd, 607 - unsigned long iova, unsigned long pfn, 608 - int prot) 609 - { 610 - pmdval_t pmdval = ipmmu_page_prot(prot, PMD_TYPE_SECT); 611 - 612 - *pmd = pfn_pmd(pfn, __pgprot(pmdval)); 613 - ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd)); 614 - 615 - return 0; 616 - } 617 - 618 - static int ipmmu_create_mapping(struct ipmmu_vmsa_domain *domain, 619 - unsigned long iova, phys_addr_t paddr, 620 - size_t size, int prot) 621 - { 622 - struct ipmmu_vmsa_device *mmu = domain->mmu; 623 - pgd_t *pgd = domain->pgd; 624 - unsigned long flags; 625 - unsigned long pfn; 626 - pmd_t *pmd; 627 - int ret; 628 - 629 - if (!pgd) 630 - return -EINVAL; 631 - 632 - if (size & ~PAGE_MASK) 633 - return -EINVAL; 634 - 635 - if (paddr & ~((1ULL << 40) - 1)) 636 - return -ERANGE; 637 - 638 - pfn = __phys_to_pfn(paddr); 639 - pgd += pgd_index(iova); 640 - 641 - /* Update the page tables. */ 642 - spin_lock_irqsave(&domain->lock, flags); 643 - 644 - pmd = ipmmu_alloc_pmd(mmu, pgd, iova); 645 - if (!pmd) { 646 - ret = -ENOMEM; 647 - goto done; 648 - } 649 - 650 - switch (size) { 651 - case SZ_2M: 652 - ret = ipmmu_alloc_init_pmd(mmu, pmd, iova, pfn, prot); 653 - break; 654 - case SZ_64K: 655 - case SZ_4K: 656 - ret = ipmmu_alloc_init_pte(mmu, pmd, iova, pfn, size, prot); 657 - break; 658 - default: 659 - ret = -EINVAL; 660 - break; 661 - } 662 - 663 - done: 664 - spin_unlock_irqrestore(&domain->lock, flags); 665 - 666 - if (!ret) 667 - ipmmu_tlb_invalidate(domain); 668 - 669 - return ret; 670 - } 671 - 672 - static void ipmmu_clear_pud(struct ipmmu_vmsa_device *mmu, pud_t *pud) 673 - { 674 - /* Free the page table. */ 675 - pgtable_t table = pud_pgtable(*pud); 676 - __free_page(table); 677 - 678 - /* Clear the PUD. */ 679 - *pud = __pud(0); 680 - ipmmu_flush_pgtable(mmu, pud, sizeof(*pud)); 681 - } 682 - 683 - static void ipmmu_clear_pmd(struct ipmmu_vmsa_device *mmu, pud_t *pud, 684 - pmd_t *pmd) 685 - { 686 - unsigned int i; 687 - 688 - /* Free the page table. */ 689 - if (pmd_table(*pmd)) { 690 - pgtable_t table = pmd_pgtable(*pmd); 691 - __free_page(table); 692 - } 693 - 694 - /* Clear the PMD. */ 695 - *pmd = __pmd(0); 696 - ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd)); 697 - 698 - /* Check whether the PUD is still needed. */ 699 - pmd = pmd_offset(pud, 0); 700 - for (i = 0; i < IPMMU_PTRS_PER_PMD; ++i) { 701 - if (!pmd_none(pmd[i])) 702 - return; 703 - } 704 - 705 - /* Clear the parent PUD. */ 706 - ipmmu_clear_pud(mmu, pud); 707 - } 708 - 709 - static void ipmmu_clear_pte(struct ipmmu_vmsa_device *mmu, pud_t *pud, 710 - pmd_t *pmd, pte_t *pte, unsigned int num_ptes) 711 - { 712 - unsigned int i; 713 - 714 - /* Clear the PTE. */ 715 - for (i = num_ptes; i; --i) 716 - pte[i-1] = __pte(0); 717 - 718 - ipmmu_flush_pgtable(mmu, pte, sizeof(*pte) * num_ptes); 719 - 720 - /* Check whether the PMD is still needed. */ 721 - pte = pte_offset_kernel(pmd, 0); 722 - for (i = 0; i < IPMMU_PTRS_PER_PTE; ++i) { 723 - if (!pte_none(pte[i])) 724 - return; 725 - } 726 - 727 - /* Clear the parent PMD. */ 728 - ipmmu_clear_pmd(mmu, pud, pmd); 729 - } 730 - 731 - static int ipmmu_split_pmd(struct ipmmu_vmsa_device *mmu, pmd_t *pmd) 732 - { 733 - pte_t *pte, *start; 734 - pteval_t pteval; 735 - unsigned long pfn; 736 - unsigned int i; 737 - 738 - pte = (pte_t *)get_zeroed_page(GFP_ATOMIC); 739 - if (!pte) 740 - return -ENOMEM; 741 - 742 - /* Copy the PMD attributes. */ 743 - pteval = (pmd_val(*pmd) & ARM_VMSA_PTE_ATTRS_MASK) 744 - | ARM_VMSA_PTE_CONT | ARM_VMSA_PTE_PAGE; 745 - 746 - pfn = pmd_pfn(*pmd); 747 - start = pte; 748 - 749 - for (i = IPMMU_PTRS_PER_PTE; i; --i) 750 - *pte++ = pfn_pte(pfn++, __pgprot(pteval)); 751 - 752 - ipmmu_flush_pgtable(mmu, start, PAGE_SIZE); 753 - *pmd = __pmd(__pa(start) | PMD_NSTABLE | PMD_TYPE_TABLE); 754 - ipmmu_flush_pgtable(mmu, pmd, sizeof(*pmd)); 755 - 756 - return 0; 757 - } 758 - 759 - static void ipmmu_split_pte(struct ipmmu_vmsa_device *mmu, pte_t *pte) 760 - { 761 - unsigned int i; 762 - 763 - for (i = ARM_VMSA_PTE_CONT_ENTRIES; i; --i) 764 - pte[i-1] = __pte(pte_val(*pte) & ~ARM_VMSA_PTE_CONT); 765 - 766 - ipmmu_flush_pgtable(mmu, pte, sizeof(*pte) * ARM_VMSA_PTE_CONT_ENTRIES); 767 - } 768 - 769 - static int ipmmu_clear_mapping(struct ipmmu_vmsa_domain *domain, 770 - unsigned long iova, size_t size) 771 - { 772 - struct ipmmu_vmsa_device *mmu = domain->mmu; 773 - unsigned long flags; 774 - pgd_t *pgd = domain->pgd; 775 - pud_t *pud; 776 - pmd_t *pmd; 777 - pte_t *pte; 778 - int ret = 0; 779 - 780 - if (!pgd) 781 - return -EINVAL; 782 - 783 - if (size & ~PAGE_MASK) 784 - return -EINVAL; 785 - 786 - pgd += pgd_index(iova); 787 - pud = (pud_t *)pgd; 788 - 789 - spin_lock_irqsave(&domain->lock, flags); 790 - 791 - /* If there's no PUD or PMD we're done. */ 792 - if (pud_none(*pud)) 793 - goto done; 794 - 795 - pmd = pmd_offset(pud, iova); 796 - if (pmd_none(*pmd)) 797 - goto done; 798 - 799 - /* 800 - * When freeing a 2MB block just clear the PMD. In the unlikely case the 801 - * block is mapped as individual pages this will free the corresponding 802 - * PTE page table. 803 - */ 804 - if (size == SZ_2M) { 805 - ipmmu_clear_pmd(mmu, pud, pmd); 806 - goto done; 807 - } 808 - 809 - /* 810 - * If the PMD has been mapped as a section remap it as pages to allow 811 - * freeing individual pages. 812 - */ 813 - if (pmd_sect(*pmd)) 814 - ipmmu_split_pmd(mmu, pmd); 815 - 816 - pte = pte_offset_kernel(pmd, iova); 817 - 818 - /* 819 - * When freeing a 64kB block just clear the PTE entries. We don't have 820 - * to care about the contiguous hint of the surrounding entries. 821 - */ 822 - if (size == SZ_64K) { 823 - ipmmu_clear_pte(mmu, pud, pmd, pte, ARM_VMSA_PTE_CONT_ENTRIES); 824 - goto done; 825 - } 826 - 827 - /* 828 - * If the PTE has been mapped with the contiguous hint set remap it and 829 - * its surrounding PTEs to allow unmapping a single page. 830 - */ 831 - if (pte_val(*pte) & ARM_VMSA_PTE_CONT) 832 - ipmmu_split_pte(mmu, pte); 833 - 834 - /* Clear the PTE. */ 835 - ipmmu_clear_pte(mmu, pud, pmd, pte, 1); 836 - 837 - done: 838 - spin_unlock_irqrestore(&domain->lock, flags); 839 - 840 - if (ret) 841 - ipmmu_tlb_invalidate(domain); 842 - 843 - return 0; 844 - } 845 - 846 - /* ----------------------------------------------------------------------------- 847 464 * IOMMU Operations 848 465 */ 849 466 ··· 466 863 return -ENOMEM; 467 864 468 865 spin_lock_init(&domain->lock); 469 - 470 - domain->pgd = kzalloc(IPMMU_PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL); 471 - if (!domain->pgd) { 472 - kfree(domain); 473 - return -ENOMEM; 474 - } 475 866 476 867 io_domain->priv = domain; 477 868 domain->io_domain = io_domain; ··· 482 885 * been detached. 483 886 */ 484 887 ipmmu_domain_destroy_context(domain); 485 - ipmmu_free_pgtables(domain); 888 + free_io_pgtable_ops(domain->iop); 486 889 kfree(domain); 487 890 } 488 891 ··· 493 896 struct ipmmu_vmsa_device *mmu = archdata->mmu; 494 897 struct ipmmu_vmsa_domain *domain = io_domain->priv; 495 898 unsigned long flags; 899 + unsigned int i; 496 900 int ret = 0; 497 901 498 902 if (!mmu) { ··· 522 924 if (ret < 0) 523 925 return ret; 524 926 525 - ipmmu_utlb_enable(domain, archdata->utlb); 927 + for (i = 0; i < archdata->num_utlbs; ++i) 928 + ipmmu_utlb_enable(domain, archdata->utlbs[i]); 526 929 527 930 return 0; 528 931 } ··· 533 934 { 534 935 struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu; 535 936 struct ipmmu_vmsa_domain *domain = io_domain->priv; 937 + unsigned int i; 536 938 537 - ipmmu_utlb_disable(domain, archdata->utlb); 939 + for (i = 0; i < archdata->num_utlbs; ++i) 940 + ipmmu_utlb_disable(domain, archdata->utlbs[i]); 538 941 539 942 /* 540 943 * TODO: Optimize by disabling the context when no device is attached. ··· 551 950 if (!domain) 552 951 return -ENODEV; 553 952 554 - return ipmmu_create_mapping(domain, iova, paddr, size, prot); 953 + return domain->iop->map(domain->iop, iova, paddr, size, prot); 555 954 } 556 955 557 956 static size_t ipmmu_unmap(struct iommu_domain *io_domain, unsigned long iova, 558 957 size_t size) 559 958 { 560 959 struct ipmmu_vmsa_domain *domain = io_domain->priv; 561 - int ret; 562 960 563 - ret = ipmmu_clear_mapping(domain, iova, size); 564 - return ret ? 0 : size; 961 + return domain->iop->unmap(domain->iop, iova, size); 565 962 } 566 963 567 964 static phys_addr_t ipmmu_iova_to_phys(struct iommu_domain *io_domain, 568 965 dma_addr_t iova) 569 966 { 570 967 struct ipmmu_vmsa_domain *domain = io_domain->priv; 571 - pgd_t pgd; 572 - pud_t pud; 573 - pmd_t pmd; 574 - pte_t pte; 575 968 576 969 /* TODO: Is locking needed ? */ 577 970 578 - if (!domain->pgd) 579 - return 0; 580 - 581 - pgd = *(domain->pgd + pgd_index(iova)); 582 - if (pgd_none(pgd)) 583 - return 0; 584 - 585 - pud = *pud_offset(&pgd, iova); 586 - if (pud_none(pud)) 587 - return 0; 588 - 589 - pmd = *pmd_offset(&pud, iova); 590 - if (pmd_none(pmd)) 591 - return 0; 592 - 593 - if (pmd_sect(pmd)) 594 - return __pfn_to_phys(pmd_pfn(pmd)) | (iova & ~PMD_MASK); 595 - 596 - pte = *(pmd_page_vaddr(pmd) + pte_index(iova)); 597 - if (pte_none(pte)) 598 - return 0; 599 - 600 - return __pfn_to_phys(pte_pfn(pte)) | (iova & ~PAGE_MASK); 971 + return domain->iop->iova_to_phys(domain->iop, iova); 601 972 } 602 973 603 - static int ipmmu_find_utlb(struct ipmmu_vmsa_device *mmu, struct device *dev) 974 + static int ipmmu_find_utlbs(struct ipmmu_vmsa_device *mmu, struct device *dev, 975 + unsigned int *utlbs, unsigned int num_utlbs) 604 976 { 605 - const struct ipmmu_vmsa_master *master = mmu->pdata->masters; 606 - const char *devname = dev_name(dev); 607 977 unsigned int i; 608 978 609 - for (i = 0; i < mmu->pdata->num_masters; ++i, ++master) { 610 - if (strcmp(master->name, devname) == 0) 611 - return master->utlb; 979 + for (i = 0; i < num_utlbs; ++i) { 980 + struct of_phandle_args args; 981 + int ret; 982 + 983 + ret = of_parse_phandle_with_args(dev->of_node, "iommus", 984 + "#iommu-cells", i, &args); 985 + if (ret < 0) 986 + return ret; 987 + 988 + of_node_put(args.np); 989 + 990 + if (args.np != mmu->dev->of_node || args.args_count != 1) 991 + return -EINVAL; 992 + 993 + utlbs[i] = args.args[0]; 612 994 } 613 995 614 - return -1; 996 + return 0; 615 997 } 616 998 617 999 static int ipmmu_add_device(struct device *dev) 618 1000 { 619 1001 struct ipmmu_vmsa_archdata *archdata; 620 1002 struct ipmmu_vmsa_device *mmu; 621 - struct iommu_group *group; 622 - int utlb = -1; 623 - int ret; 1003 + struct iommu_group *group = NULL; 1004 + unsigned int *utlbs; 1005 + unsigned int i; 1006 + int num_utlbs; 1007 + int ret = -ENODEV; 624 1008 625 1009 if (dev->archdata.iommu) { 626 1010 dev_warn(dev, "IOMMU driver already assigned to device %s\n", ··· 614 1028 } 615 1029 616 1030 /* Find the master corresponding to the device. */ 1031 + 1032 + num_utlbs = of_count_phandle_with_args(dev->of_node, "iommus", 1033 + "#iommu-cells"); 1034 + if (num_utlbs < 0) 1035 + return -ENODEV; 1036 + 1037 + utlbs = kcalloc(num_utlbs, sizeof(*utlbs), GFP_KERNEL); 1038 + if (!utlbs) 1039 + return -ENOMEM; 1040 + 617 1041 spin_lock(&ipmmu_devices_lock); 618 1042 619 1043 list_for_each_entry(mmu, &ipmmu_devices, list) { 620 - utlb = ipmmu_find_utlb(mmu, dev); 621 - if (utlb >= 0) { 1044 + ret = ipmmu_find_utlbs(mmu, dev, utlbs, num_utlbs); 1045 + if (!ret) { 622 1046 /* 623 1047 * TODO Take a reference to the MMU to protect 624 1048 * against device removal. ··· 639 1043 640 1044 spin_unlock(&ipmmu_devices_lock); 641 1045 642 - if (utlb < 0) 1046 + if (ret < 0) 643 1047 return -ENODEV; 644 1048 645 - if (utlb >= mmu->num_utlbs) 646 - return -EINVAL; 1049 + for (i = 0; i < num_utlbs; ++i) { 1050 + if (utlbs[i] >= mmu->num_utlbs) { 1051 + ret = -EINVAL; 1052 + goto error; 1053 + } 1054 + } 647 1055 648 1056 /* Create a device group and add the device to it. */ 649 1057 group = iommu_group_alloc(); 650 1058 if (IS_ERR(group)) { 651 1059 dev_err(dev, "Failed to allocate IOMMU group\n"); 652 - return PTR_ERR(group); 1060 + ret = PTR_ERR(group); 1061 + goto error; 653 1062 } 654 1063 655 1064 ret = iommu_group_add_device(group, dev); ··· 662 1061 663 1062 if (ret < 0) { 664 1063 dev_err(dev, "Failed to add device to IPMMU group\n"); 665 - return ret; 1064 + group = NULL; 1065 + goto error; 666 1066 } 667 1067 668 1068 archdata = kzalloc(sizeof(*archdata), GFP_KERNEL); ··· 673 1071 } 674 1072 675 1073 archdata->mmu = mmu; 676 - archdata->utlb = utlb; 1074 + archdata->utlbs = utlbs; 1075 + archdata->num_utlbs = num_utlbs; 677 1076 dev->archdata.iommu = archdata; 678 1077 679 1078 /* ··· 693 1090 SZ_1G, SZ_2G); 694 1091 if (IS_ERR(mapping)) { 695 1092 dev_err(mmu->dev, "failed to create ARM IOMMU mapping\n"); 696 - return PTR_ERR(mapping); 1093 + ret = PTR_ERR(mapping); 1094 + goto error; 697 1095 } 698 1096 699 1097 mmu->mapping = mapping; ··· 710 1106 return 0; 711 1107 712 1108 error: 1109 + arm_iommu_release_mapping(mmu->mapping); 1110 + 713 1111 kfree(dev->archdata.iommu); 1112 + kfree(utlbs); 1113 + 714 1114 dev->archdata.iommu = NULL; 715 - iommu_group_remove_device(dev); 1115 + 1116 + if (!IS_ERR_OR_NULL(group)) 1117 + iommu_group_remove_device(dev); 1118 + 716 1119 return ret; 717 1120 } 718 1121 719 1122 static void ipmmu_remove_device(struct device *dev) 720 1123 { 1124 + struct ipmmu_vmsa_archdata *archdata = dev->archdata.iommu; 1125 + 721 1126 arm_iommu_detach_device(dev); 722 1127 iommu_group_remove_device(dev); 723 - kfree(dev->archdata.iommu); 1128 + 1129 + kfree(archdata->utlbs); 1130 + kfree(archdata); 1131 + 724 1132 dev->archdata.iommu = NULL; 725 1133 } 726 1134 ··· 747 1131 .iova_to_phys = ipmmu_iova_to_phys, 748 1132 .add_device = ipmmu_add_device, 749 1133 .remove_device = ipmmu_remove_device, 750 - .pgsize_bitmap = SZ_2M | SZ_64K | SZ_4K, 1134 + .pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K, 751 1135 }; 752 1136 753 1137 /* ----------------------------------------------------------------------------- ··· 770 1154 int irq; 771 1155 int ret; 772 1156 773 - if (!pdev->dev.platform_data) { 1157 + if (!IS_ENABLED(CONFIG_OF) && !pdev->dev.platform_data) { 774 1158 dev_err(&pdev->dev, "missing platform data\n"); 775 1159 return -EINVAL; 776 1160 } ··· 782 1166 } 783 1167 784 1168 mmu->dev = &pdev->dev; 785 - mmu->pdata = pdev->dev.platform_data; 786 1169 mmu->num_utlbs = 32; 787 1170 788 1171 /* Map I/O memory and request IRQ. */ ··· 789 1174 mmu->base = devm_ioremap_resource(&pdev->dev, res); 790 1175 if (IS_ERR(mmu->base)) 791 1176 return PTR_ERR(mmu->base); 1177 + 1178 + /* 1179 + * The IPMMU has two register banks, for secure and non-secure modes. 1180 + * The bank mapped at the beginning of the IPMMU address space 1181 + * corresponds to the running mode of the CPU. When running in secure 1182 + * mode the non-secure register bank is also available at an offset. 1183 + * 1184 + * Secure mode operation isn't clearly documented and is thus currently 1185 + * not implemented in the driver. Furthermore, preliminary tests of 1186 + * non-secure operation with the main register bank were not successful. 1187 + * Offset the registers base unconditionally to point to the non-secure 1188 + * alias space for now. 1189 + */ 1190 + mmu->base += IM_NS_ALIAS_OFFSET; 792 1191 793 1192 irq = platform_get_irq(pdev, 0); 794 1193 if (irq < 0) { ··· 849 1220 return 0; 850 1221 } 851 1222 1223 + static const struct of_device_id ipmmu_of_ids[] = { 1224 + { .compatible = "renesas,ipmmu-vmsa", }, 1225 + }; 1226 + 852 1227 static struct platform_driver ipmmu_driver = { 853 1228 .driver = { 854 1229 .name = "ipmmu-vmsa", 1230 + .of_match_table = of_match_ptr(ipmmu_of_ids), 855 1231 }, 856 1232 .probe = ipmmu_probe, 857 1233 .remove = ipmmu_remove,
+1 -1
drivers/iommu/irq_remapping.h
··· 1 1 /* 2 2 * Copyright (C) 2012 Advanced Micro Devices, Inc. 3 - * Author: Joerg Roedel <joerg.roedel@amd.com> 3 + * Author: Joerg Roedel <jroedel@suse.de> 4 4 * 5 5 * This program is free software; you can redistribute it and/or modify it 6 6 * under the terms of the GNU General Public License version 2 as published
+1 -1
drivers/iommu/omap-iommu.c
··· 1126 1126 return -EINVAL; 1127 1127 } 1128 1128 1129 - dev_dbg(dev, "mapping da 0x%lx to pa 0x%x size 0x%x\n", da, pa, bytes); 1129 + dev_dbg(dev, "mapping da 0x%lx to pa %pa size 0x%x\n", da, &pa, bytes); 1130 1130 1131 1131 iotlb_init_entry(&e, da, pa, omap_pgsz); 1132 1132
+144
include/linux/iopoll.h
··· 1 + /* 2 + * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved. 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 and 6 + * only version 2 as 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 + */ 14 + 15 + #ifndef _LINUX_IOPOLL_H 16 + #define _LINUX_IOPOLL_H 17 + 18 + #include <linux/kernel.h> 19 + #include <linux/types.h> 20 + #include <linux/hrtimer.h> 21 + #include <linux/delay.h> 22 + #include <linux/errno.h> 23 + #include <linux/io.h> 24 + 25 + /** 26 + * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs 27 + * @op: accessor function (takes @addr as its only argument) 28 + * @addr: Address to poll 29 + * @val: Variable to read the value into 30 + * @cond: Break condition (usually involving @val) 31 + * @sleep_us: Maximum time to sleep between reads in us (0 32 + * tight-loops). Should be less than ~20ms since usleep_range 33 + * is used (see Documentation/timers/timers-howto.txt). 34 + * @timeout_us: Timeout in us, 0 means never timeout 35 + * 36 + * Returns 0 on success and -ETIMEDOUT upon a timeout. In either 37 + * case, the last read value at @addr is stored in @val. Must not 38 + * be called from atomic context if sleep_us or timeout_us are used. 39 + * 40 + * When available, you'll probably want to use one of the specialized 41 + * macros defined below rather than this macro directly. 42 + */ 43 + #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \ 44 + ({ \ 45 + ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \ 46 + might_sleep_if(sleep_us); \ 47 + for (;;) { \ 48 + (val) = op(addr); \ 49 + if (cond) \ 50 + break; \ 51 + if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \ 52 + (val) = op(addr); \ 53 + break; \ 54 + } \ 55 + if (sleep_us) \ 56 + usleep_range((sleep_us >> 2) + 1, sleep_us); \ 57 + } \ 58 + (cond) ? 0 : -ETIMEDOUT; \ 59 + }) 60 + 61 + /** 62 + * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs 63 + * @op: accessor function (takes @addr as its only argument) 64 + * @addr: Address to poll 65 + * @val: Variable to read the value into 66 + * @cond: Break condition (usually involving @val) 67 + * @delay_us: Time to udelay between reads in us (0 tight-loops). Should 68 + * be less than ~10us since udelay is used (see 69 + * Documentation/timers/timers-howto.txt). 70 + * @timeout_us: Timeout in us, 0 means never timeout 71 + * 72 + * Returns 0 on success and -ETIMEDOUT upon a timeout. In either 73 + * case, the last read value at @addr is stored in @val. 74 + * 75 + * When available, you'll probably want to use one of the specialized 76 + * macros defined below rather than this macro directly. 77 + */ 78 + #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \ 79 + ({ \ 80 + ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \ 81 + for (;;) { \ 82 + (val) = op(addr); \ 83 + if (cond) \ 84 + break; \ 85 + if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \ 86 + (val) = op(addr); \ 87 + break; \ 88 + } \ 89 + if (delay_us) \ 90 + udelay(delay_us); \ 91 + } \ 92 + (cond) ? 0 : -ETIMEDOUT; \ 93 + }) 94 + 95 + 96 + #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \ 97 + readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us) 98 + 99 + #define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ 100 + readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us) 101 + 102 + #define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \ 103 + readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us) 104 + 105 + #define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ 106 + readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us) 107 + 108 + #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \ 109 + readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us) 110 + 111 + #define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ 112 + readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us) 113 + 114 + #define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \ 115 + readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us) 116 + 117 + #define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ 118 + readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us) 119 + 120 + #define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \ 121 + readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us) 122 + 123 + #define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ 124 + readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us) 125 + 126 + #define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \ 127 + readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us) 128 + 129 + #define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ 130 + readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us) 131 + 132 + #define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \ 133 + readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us) 134 + 135 + #define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ 136 + readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us) 137 + 138 + #define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \ 139 + readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us) 140 + 141 + #define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \ 142 + readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us) 143 + 144 + #endif /* _LINUX_IOPOLL_H */
+37 -4
include/linux/iova.h
··· 16 16 #include <linux/rbtree.h> 17 17 #include <linux/dma-mapping.h> 18 18 19 - /* IO virtual address start page frame number */ 20 - #define IOVA_START_PFN (1) 21 - 22 19 /* iova structure */ 23 20 struct iova { 24 21 struct rb_node node; ··· 28 31 spinlock_t iova_rbtree_lock; /* Lock to protect update of rbtree */ 29 32 struct rb_root rbroot; /* iova domain rbtree root */ 30 33 struct rb_node *cached32_node; /* Save last alloced node */ 34 + unsigned long granule; /* pfn granularity for this domain */ 35 + unsigned long start_pfn; /* Lower limit for this domain */ 31 36 unsigned long dma_32bit_pfn; 32 37 }; 33 38 ··· 37 38 { 38 39 return iova->pfn_hi - iova->pfn_lo + 1; 39 40 } 41 + 42 + static inline unsigned long iova_shift(struct iova_domain *iovad) 43 + { 44 + return __ffs(iovad->granule); 45 + } 46 + 47 + static inline unsigned long iova_mask(struct iova_domain *iovad) 48 + { 49 + return iovad->granule - 1; 50 + } 51 + 52 + static inline size_t iova_offset(struct iova_domain *iovad, dma_addr_t iova) 53 + { 54 + return iova & iova_mask(iovad); 55 + } 56 + 57 + static inline size_t iova_align(struct iova_domain *iovad, size_t size) 58 + { 59 + return ALIGN(size, iovad->granule); 60 + } 61 + 62 + static inline dma_addr_t iova_dma_addr(struct iova_domain *iovad, struct iova *iova) 63 + { 64 + return (dma_addr_t)iova->pfn_lo << iova_shift(iovad); 65 + } 66 + 67 + static inline unsigned long iova_pfn(struct iova_domain *iovad, dma_addr_t iova) 68 + { 69 + return iova >> iova_shift(iovad); 70 + } 71 + 72 + int iommu_iova_cache_init(void); 73 + void iommu_iova_cache_destroy(void); 40 74 41 75 struct iova *alloc_iova_mem(void); 42 76 void free_iova_mem(struct iova *iova); ··· 81 49 struct iova *reserve_iova(struct iova_domain *iovad, unsigned long pfn_lo, 82 50 unsigned long pfn_hi); 83 51 void copy_reserved_iova(struct iova_domain *from, struct iova_domain *to); 84 - void init_iova_domain(struct iova_domain *iovad, unsigned long pfn_32bit); 52 + void init_iova_domain(struct iova_domain *iovad, unsigned long granule, 53 + unsigned long start_pfn, unsigned long pfn_32bit); 85 54 struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn); 86 55 void put_iova_domain(struct iova_domain *iovad); 87 56 struct iova *split_and_remove_iova(struct iova_domain *iovad,
-24
include/linux/platform_data/ipmmu-vmsa.h
··· 1 - /* 2 - * IPMMU VMSA Platform Data 3 - * 4 - * Copyright (C) 2014 Renesas Electronics Corporation 5 - * 6 - * This program is free software; you can redistribute it and/or modify 7 - * it under the terms of the GNU General Public License as published by 8 - * the Free Software Foundation; version 2 of the License. 9 - */ 10 - 11 - #ifndef __IPMMU_VMSA_H__ 12 - #define __IPMMU_VMSA_H__ 13 - 14 - struct ipmmu_vmsa_master { 15 - const char *name; 16 - unsigned int utlb; 17 - }; 18 - 19 - struct ipmmu_vmsa_platform_data { 20 - const struct ipmmu_vmsa_master *masters; 21 - unsigned int num_masters; 22 - }; 23 - 24 - #endif /* __IPMMU_VMSA_H__ */
+18 -13
include/trace/events/iommu.h
··· 83 83 TP_ARGS(dev) 84 84 ); 85 85 86 - DECLARE_EVENT_CLASS(iommu_map_unmap, 86 + TRACE_EVENT(map, 87 87 88 88 TP_PROTO(unsigned long iova, phys_addr_t paddr, size_t size), 89 89 ··· 92 92 TP_STRUCT__entry( 93 93 __field(u64, iova) 94 94 __field(u64, paddr) 95 - __field(int, size) 95 + __field(size_t, size) 96 96 ), 97 97 98 98 TP_fast_assign( ··· 101 101 __entry->size = size; 102 102 ), 103 103 104 - TP_printk("IOMMU: iova=0x%016llx paddr=0x%016llx size=0x%x", 104 + TP_printk("IOMMU: iova=0x%016llx paddr=0x%016llx size=%zu", 105 105 __entry->iova, __entry->paddr, __entry->size 106 106 ) 107 107 ); 108 108 109 - DEFINE_EVENT(iommu_map_unmap, map, 109 + TRACE_EVENT(unmap, 110 110 111 - TP_PROTO(unsigned long iova, phys_addr_t paddr, size_t size), 111 + TP_PROTO(unsigned long iova, size_t size, size_t unmapped_size), 112 112 113 - TP_ARGS(iova, paddr, size) 114 - ); 113 + TP_ARGS(iova, size, unmapped_size), 115 114 116 - DEFINE_EVENT_PRINT(iommu_map_unmap, unmap, 115 + TP_STRUCT__entry( 116 + __field(u64, iova) 117 + __field(size_t, size) 118 + __field(size_t, unmapped_size) 119 + ), 117 120 118 - TP_PROTO(unsigned long iova, phys_addr_t paddr, size_t size), 121 + TP_fast_assign( 122 + __entry->iova = iova; 123 + __entry->size = size; 124 + __entry->unmapped_size = unmapped_size; 125 + ), 119 126 120 - TP_ARGS(iova, paddr, size), 121 - 122 - TP_printk("IOMMU: iova=0x%016llx size=0x%x", 123 - __entry->iova, __entry->size 127 + TP_printk("IOMMU: iova=0x%016llx size=%zu unmapped_size=%zu", 128 + __entry->iova, __entry->size, __entry->unmapped_size 124 129 ) 125 130 ); 126 131