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

Merge tag 'dma-mapping-5.19-2022-05-25' of git://git.infradead.org/users/hch/dma-mapping

Pull dma-mapping updates from Christoph Hellwig:

- don't over-decrypt memory (Robin Murphy)

- takes min align mask into account for the swiotlb max mapping size
(Tianyu Lan)

- use GFP_ATOMIC in dma-debug (Mikulas Patocka)

- fix DMA_ATTR_NO_KERNEL_MAPPING on xen/arm (me)

- don't fail on highmem CMA pages in dma_direct_alloc_pages (me)

- cleanup swiotlb initialization and share more code with swiotlb-xen
(me, Stefano Stabellini)

* tag 'dma-mapping-5.19-2022-05-25' of git://git.infradead.org/users/hch/dma-mapping: (23 commits)
dma-direct: don't over-decrypt memory
swiotlb: max mapping size takes min align mask into account
swiotlb: use the right nslabs-derived sizes in swiotlb_init_late
swiotlb: use the right nslabs value in swiotlb_init_remap
swiotlb: don't panic when the swiotlb buffer can't be allocated
dma-debug: change allocation mode from GFP_NOWAIT to GFP_ATIOMIC
dma-direct: don't fail on highmem CMA pages in dma_direct_alloc_pages
swiotlb-xen: fix DMA_ATTR_NO_KERNEL_MAPPING on arm
x86: remove cruft from <asm/dma-mapping.h>
swiotlb: remove swiotlb_init_with_tbl and swiotlb_init_late_with_tbl
swiotlb: merge swiotlb-xen initialization into swiotlb
swiotlb: provide swiotlb_init variants that remap the buffer
swiotlb: pass a gfp_mask argument to swiotlb_init_late
swiotlb: add a SWIOTLB_ANY flag to lift the low memory restriction
swiotlb: make the swiotlb_init interface more useful
x86: centralize setting SWIOTLB_FORCE when guest memory encryption is enabled
x86: remove the IOMMU table infrastructure
MIPS/octeon: use swiotlb_init instead of open coding it
arm/xen: don't check for xen_initial_domain() in xen_create_contiguous_region
swiotlb: rename swiotlb_late_init_with_default_size
...

+318 -982
-2
arch/arm/include/asm/xen/page-coherent.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #include <xen/arm/page-coherent.h>
+1 -5
arch/arm/mm/init.c
··· 271 271 void __init mem_init(void) 272 272 { 273 273 #ifdef CONFIG_ARM_LPAE 274 - if (swiotlb_force == SWIOTLB_FORCE || 275 - max_pfn > arm_dma_pfn_limit) 276 - swiotlb_init(1); 277 - else 278 - swiotlb_force = SWIOTLB_NO_FORCE; 274 + swiotlb_init(max_pfn > arm_dma_pfn_limit, SWIOTLB_VERBOSE); 279 275 #endif 280 276 281 277 set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
+11 -27
arch/arm/xen/mm.c
··· 23 23 #include <asm/xen/hypercall.h> 24 24 #include <asm/xen/interface.h> 25 25 26 - unsigned long xen_get_swiotlb_free_pages(unsigned int order) 26 + static gfp_t xen_swiotlb_gfp(void) 27 27 { 28 28 phys_addr_t base; 29 - gfp_t flags = __GFP_NOWARN|__GFP_KSWAPD_RECLAIM; 30 29 u64 i; 31 30 32 31 for_each_mem_range(i, &base, NULL) { 33 32 if (base < (phys_addr_t)0xffffffff) { 34 33 if (IS_ENABLED(CONFIG_ZONE_DMA32)) 35 - flags |= __GFP_DMA32; 36 - else 37 - flags |= __GFP_DMA; 38 - break; 34 + return __GFP_DMA32; 35 + return __GFP_DMA; 39 36 } 40 37 } 41 - return __get_free_pages(flags, order); 38 + 39 + return GFP_KERNEL; 42 40 } 43 41 44 42 static bool hypercall_cflush = false; ··· 116 118 !dev_is_dma_coherent(dev)); 117 119 } 118 120 119 - int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order, 120 - unsigned int address_bits, 121 - dma_addr_t *dma_handle) 122 - { 123 - if (!xen_initial_domain()) 124 - return -EINVAL; 125 - 126 - /* we assume that dom0 is mapped 1:1 for now */ 127 - *dma_handle = pstart; 128 - return 0; 129 - } 130 - 131 - void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order) 132 - { 133 - return; 134 - } 135 - 136 121 static int __init xen_mm_init(void) 137 122 { 138 123 struct gnttab_cache_flush cflush; ··· 124 143 if (!xen_swiotlb_detect()) 125 144 return 0; 126 145 127 - rc = xen_swiotlb_init(); 128 146 /* we can work with the default swiotlb */ 129 - if (rc < 0 && rc != -EEXIST) 130 - return rc; 147 + if (!io_tlb_default_mem.nslabs) { 148 + rc = swiotlb_init_late(swiotlb_size_or_default(), 149 + xen_swiotlb_gfp(), NULL); 150 + if (rc < 0) 151 + return rc; 152 + } 131 153 132 154 cflush.op = 0; 133 155 cflush.a.dev_bus_addr = 0;
-2
arch/arm64/include/asm/xen/page-coherent.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #include <xen/arm/page-coherent.h>
+1 -5
arch/arm64/mm/init.c
··· 451 451 */ 452 452 void __init mem_init(void) 453 453 { 454 - if (swiotlb_force == SWIOTLB_FORCE || 455 - max_pfn > PFN_DOWN(arm64_dma_phys_limit)) 456 - swiotlb_init(1); 457 - else if (!xen_swiotlb_detect()) 458 - swiotlb_force = SWIOTLB_NO_FORCE; 454 + swiotlb_init(max_pfn > PFN_DOWN(arm64_dma_phys_limit), SWIOTLB_VERBOSE); 459 455 460 456 /* this will put all unused low memory onto the freelists */ 461 457 memblock_free_all();
-7
arch/ia64/include/asm/iommu_table.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #ifndef _ASM_IA64_IOMMU_TABLE_H 3 - #define _ASM_IA64_IOMMU_TABLE_H 4 - 5 - #define IOMMU_INIT_POST(_detect) 6 - 7 - #endif /* _ASM_IA64_IOMMU_TABLE_H */
+1 -3
arch/ia64/mm/init.c
··· 437 437 if (iommu_detected) 438 438 break; 439 439 #endif 440 - #ifdef CONFIG_SWIOTLB 441 - swiotlb_init(1); 442 - #endif 440 + swiotlb_init(true, SWIOTLB_VERBOSE); 443 441 } while (0); 444 442 445 443 #ifdef CONFIG_FLATMEM
+2 -13
arch/mips/cavium-octeon/dma-octeon.c
··· 186 186 return daddr; 187 187 } 188 188 189 - char *octeon_swiotlb; 190 - 191 189 void __init plat_swiotlb_setup(void) 192 190 { 193 191 phys_addr_t start, end; 194 192 phys_addr_t max_addr; 195 193 phys_addr_t addr_size; 196 194 size_t swiotlbsize; 197 - unsigned long swiotlb_nslabs; 198 195 u64 i; 199 196 200 197 max_addr = 0; ··· 233 236 if (OCTEON_IS_OCTEON2() && max_addr >= 0x100000000ul) 234 237 swiotlbsize = 64 * (1<<20); 235 238 #endif 236 - swiotlb_nslabs = swiotlbsize >> IO_TLB_SHIFT; 237 - swiotlb_nslabs = ALIGN(swiotlb_nslabs, IO_TLB_SEGSIZE); 238 - swiotlbsize = swiotlb_nslabs << IO_TLB_SHIFT; 239 239 240 - octeon_swiotlb = memblock_alloc_low(swiotlbsize, PAGE_SIZE); 241 - if (!octeon_swiotlb) 242 - panic("%s: Failed to allocate %zu bytes align=%lx\n", 243 - __func__, swiotlbsize, PAGE_SIZE); 244 - 245 - if (swiotlb_init_with_tbl(octeon_swiotlb, swiotlb_nslabs, 1) == -ENOMEM) 246 - panic("Cannot allocate SWIOTLB buffer"); 240 + swiotlb_adjust_size(swiotlbsize); 241 + swiotlb_init(true, SWIOTLB_VERBOSE); 247 242 }
+1 -1
arch/mips/loongson64/dma.c
··· 24 24 25 25 void __init plat_swiotlb_setup(void) 26 26 { 27 - swiotlb_init(1); 27 + swiotlb_init(true, SWIOTLB_VERBOSE); 28 28 }
+1 -1
arch/mips/pci/pci-octeon.c
··· 664 664 665 665 /* BAR1 movable regions contiguous to cover the swiotlb */ 666 666 octeon_bar1_pci_phys = 667 - virt_to_phys(octeon_swiotlb) & ~((1ull << 22) - 1); 667 + io_tlb_default_mem.start & ~((1ull << 22) - 1); 668 668 669 669 for (index = 0; index < 32; index++) { 670 670 union cvmx_pci_bar1_indexx bar1_index;
+1 -1
arch/mips/sibyte/common/dma.c
··· 10 10 11 11 void __init plat_swiotlb_setup(void) 12 12 { 13 - swiotlb_init(1); 13 + swiotlb_init(true, SWIOTLB_VERBOSE); 14 14 }
-4
arch/powerpc/include/asm/svm.h
··· 15 15 return mfmsr() & MSR_S; 16 16 } 17 17 18 - void __init svm_swiotlb_init(void); 19 - 20 18 void dtl_cache_ctor(void *addr); 21 19 #define get_dtl_cache_ctor() (is_secure_guest() ? dtl_cache_ctor : NULL) 22 20 ··· 24 26 { 25 27 return false; 26 28 } 27 - 28 - static inline void svm_swiotlb_init(void) {} 29 29 30 30 #define get_dtl_cache_ctor() NULL 31 31
+1
arch/powerpc/include/asm/swiotlb.h
··· 9 9 #include <linux/swiotlb.h> 10 10 11 11 extern unsigned int ppc_swiotlb_enable; 12 + extern unsigned int ppc_swiotlb_flags; 12 13 13 14 #ifdef CONFIG_SWIOTLB 14 15 void swiotlb_detect_4g(void);
+1
arch/powerpc/kernel/dma-swiotlb.c
··· 10 10 #include <asm/swiotlb.h> 11 11 12 12 unsigned int ppc_swiotlb_enable; 13 + unsigned int ppc_swiotlb_flags; 13 14 14 15 void __init swiotlb_detect_4g(void) 15 16 {
+2 -4
arch/powerpc/mm/mem.c
··· 17 17 #include <linux/suspend.h> 18 18 #include <linux/dma-direct.h> 19 19 20 + #include <asm/swiotlb.h> 20 21 #include <asm/machdep.h> 21 22 #include <asm/rtas.h> 22 23 #include <asm/kasan.h> ··· 249 248 * back to to-down. 250 249 */ 251 250 memblock_set_bottom_up(true); 252 - if (is_secure_guest()) 253 - svm_swiotlb_init(); 254 - else 255 - swiotlb_init(0); 251 + swiotlb_init(ppc_swiotlb_enable, ppc_swiotlb_flags); 256 252 #endif 257 253 258 254 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
-3
arch/powerpc/platforms/pseries/setup.c
··· 849 849 } 850 850 851 851 ppc_md.pcibios_root_bridge_prepare = pseries_root_bridge_prepare; 852 - 853 - if (swiotlb_force == SWIOTLB_FORCE) 854 - ppc_swiotlb_enable = 1; 855 852 } 856 853 857 854 static void pseries_panic(char *str)
+1 -25
arch/powerpc/platforms/pseries/svm.c
··· 28 28 * need to use the SWIOTLB buffer for DMA even if dma_capable() says 29 29 * otherwise. 30 30 */ 31 - swiotlb_force = SWIOTLB_FORCE; 31 + ppc_swiotlb_flags |= SWIOTLB_ANY | SWIOTLB_FORCE; 32 32 33 33 /* Share the SWIOTLB buffer with the host. */ 34 34 swiotlb_update_mem_attributes(); ··· 36 36 return 0; 37 37 } 38 38 machine_early_initcall(pseries, init_svm); 39 - 40 - /* 41 - * Initialize SWIOTLB. Essentially the same as swiotlb_init(), except that it 42 - * can allocate the buffer anywhere in memory. Since the hypervisor doesn't have 43 - * any addressing limitation, we don't need to allocate it in low addresses. 44 - */ 45 - void __init svm_swiotlb_init(void) 46 - { 47 - unsigned char *vstart; 48 - unsigned long bytes, io_tlb_nslabs; 49 - 50 - io_tlb_nslabs = (swiotlb_size_or_default() >> IO_TLB_SHIFT); 51 - io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); 52 - 53 - bytes = io_tlb_nslabs << IO_TLB_SHIFT; 54 - 55 - vstart = memblock_alloc(PAGE_ALIGN(bytes), PAGE_SIZE); 56 - if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, false)) 57 - return; 58 - 59 - 60 - memblock_free(vstart, PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT)); 61 - panic("SVM: Cannot allocate SWIOTLB buffer"); 62 - } 63 39 64 40 int set_memory_encrypted(unsigned long addr, int numpages) 65 41 {
+1 -7
arch/riscv/mm/init.c
··· 120 120 BUG_ON(!mem_map); 121 121 #endif /* CONFIG_FLATMEM */ 122 122 123 - #ifdef CONFIG_SWIOTLB 124 - if (swiotlb_force == SWIOTLB_FORCE || 125 - max_pfn > PFN_DOWN(dma32_phys_limit)) 126 - swiotlb_init(1); 127 - else 128 - swiotlb_force = SWIOTLB_NO_FORCE; 129 - #endif 123 + swiotlb_init(max_pfn > PFN_DOWN(dma32_phys_limit), SWIOTLB_VERBOSE); 130 124 memblock_free_all(); 131 125 132 126 print_vm_layout();
+1 -2
arch/s390/mm/init.c
··· 185 185 return; 186 186 187 187 /* make sure bounce buffers are shared */ 188 - swiotlb_force = SWIOTLB_FORCE; 189 - swiotlb_init(1); 188 + swiotlb_init(true, SWIOTLB_FORCE | SWIOTLB_VERBOSE); 190 189 swiotlb_update_mem_attributes(); 191 190 } 192 191
-12
arch/x86/include/asm/dma-mapping.h
··· 2 2 #ifndef _ASM_X86_DMA_MAPPING_H 3 3 #define _ASM_X86_DMA_MAPPING_H 4 4 5 - /* 6 - * IOMMU interface. See Documentation/core-api/dma-api-howto.rst and 7 - * Documentation/core-api/dma-api.rst for documentation. 8 - */ 9 - 10 - #include <linux/scatterlist.h> 11 - #include <asm/io.h> 12 - #include <asm/swiotlb.h> 13 - 14 - extern int iommu_merge; 15 - extern int panic_on_overflow; 16 - 17 5 extern const struct dma_map_ops *dma_ops; 18 6 19 7 static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
+2 -3
arch/x86/include/asm/gart.h
··· 38 38 extern void early_gart_iommu_check(void); 39 39 extern int gart_iommu_init(void); 40 40 extern void __init gart_parse_options(char *); 41 - extern int gart_iommu_hole_init(void); 41 + void gart_iommu_hole_init(void); 42 42 43 43 #else 44 44 #define gart_iommu_aperture 0 ··· 51 51 static inline void gart_parse_options(char *options) 52 52 { 53 53 } 54 - static inline int gart_iommu_hole_init(void) 54 + static inline void gart_iommu_hole_init(void) 55 55 { 56 - return -ENODEV; 57 56 } 58 57 #endif 59 58
+8
arch/x86/include/asm/iommu.h
··· 8 8 9 9 extern int force_iommu, no_iommu; 10 10 extern int iommu_detected; 11 + extern int iommu_merge; 12 + extern int panic_on_overflow; 13 + 14 + #ifdef CONFIG_SWIOTLB 15 + extern bool x86_swiotlb_enable; 16 + #else 17 + #define x86_swiotlb_enable false 18 + #endif 11 19 12 20 /* 10 seconds */ 13 21 #define DMAR_OPERATION_TIMEOUT ((cycles_t) tsc_khz*10*1000)
-102
arch/x86/include/asm/iommu_table.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #ifndef _ASM_X86_IOMMU_TABLE_H 3 - #define _ASM_X86_IOMMU_TABLE_H 4 - 5 - #include <asm/swiotlb.h> 6 - 7 - /* 8 - * History lesson: 9 - * The execution chain of IOMMUs in 2.6.36 looks as so: 10 - * 11 - * [xen-swiotlb] 12 - * | 13 - * +----[swiotlb *]--+ 14 - * / | \ 15 - * / | \ 16 - * [GART] [Calgary] [Intel VT-d] 17 - * / 18 - * / 19 - * [AMD-Vi] 20 - * 21 - * *: if SWIOTLB detected 'iommu=soft'/'swiotlb=force' it would skip 22 - * over the rest of IOMMUs and unconditionally initialize the SWIOTLB. 23 - * Also it would surreptitiously initialize set the swiotlb=1 if there were 24 - * more than 4GB and if the user did not pass in 'iommu=off'. The swiotlb 25 - * flag would be turned off by all IOMMUs except the Calgary one. 26 - * 27 - * The IOMMU_INIT* macros allow a similar tree (or more complex if desired) 28 - * to be built by defining who we depend on. 29 - * 30 - * And all that needs to be done is to use one of the macros in the IOMMU 31 - * and the pci-dma.c will take care of the rest. 32 - */ 33 - 34 - struct iommu_table_entry { 35 - initcall_t detect; 36 - initcall_t depend; 37 - void (*early_init)(void); /* No memory allocate available. */ 38 - void (*late_init)(void); /* Yes, can allocate memory. */ 39 - #define IOMMU_FINISH_IF_DETECTED (1<<0) 40 - #define IOMMU_DETECTED (1<<1) 41 - int flags; 42 - }; 43 - /* 44 - * Macro fills out an entry in the .iommu_table that is equivalent 45 - * to the fields that 'struct iommu_table_entry' has. The entries 46 - * that are put in the .iommu_table section are not put in any order 47 - * hence during boot-time we will have to resort them based on 48 - * dependency. */ 49 - 50 - 51 - #define __IOMMU_INIT(_detect, _depend, _early_init, _late_init, _finish)\ 52 - static const struct iommu_table_entry \ 53 - __iommu_entry_##_detect __used \ 54 - __attribute__ ((unused, __section__(".iommu_table"), \ 55 - aligned((sizeof(void *))))) \ 56 - = {_detect, _depend, _early_init, _late_init, \ 57 - _finish ? IOMMU_FINISH_IF_DETECTED : 0} 58 - /* 59 - * The simplest IOMMU definition. Provide the detection routine 60 - * and it will be run after the SWIOTLB and the other IOMMUs 61 - * that utilize this macro. If the IOMMU is detected (ie, the 62 - * detect routine returns a positive value), the other IOMMUs 63 - * are also checked. You can use IOMMU_INIT_POST_FINISH if you prefer 64 - * to stop detecting the other IOMMUs after yours has been detected. 65 - */ 66 - #define IOMMU_INIT_POST(_detect) \ 67 - __IOMMU_INIT(_detect, pci_swiotlb_detect_4gb, NULL, NULL, 0) 68 - 69 - #define IOMMU_INIT_POST_FINISH(detect) \ 70 - __IOMMU_INIT(_detect, pci_swiotlb_detect_4gb, NULL, NULL, 1) 71 - 72 - /* 73 - * A more sophisticated version of IOMMU_INIT. This variant requires: 74 - * a). A detection routine function. 75 - * b). The name of the detection routine we depend on to get called 76 - * before us. 77 - * c). The init routine which gets called if the detection routine 78 - * returns a positive value from the pci_iommu_alloc. This means 79 - * no presence of a memory allocator. 80 - * d). Similar to the 'init', except that this gets called from pci_iommu_init 81 - * where we do have a memory allocator. 82 - * 83 - * The standard IOMMU_INIT differs from the IOMMU_INIT_FINISH variant 84 - * in that the former will continue detecting other IOMMUs in the call 85 - * list after the detection routine returns a positive number, while the 86 - * latter will stop the execution chain upon first successful detection. 87 - * Both variants will still call the 'init' and 'late_init' functions if 88 - * they are set. 89 - */ 90 - #define IOMMU_INIT_FINISH(_detect, _depend, _init, _late_init) \ 91 - __IOMMU_INIT(_detect, _depend, _init, _late_init, 1) 92 - 93 - #define IOMMU_INIT(_detect, _depend, _init, _late_init) \ 94 - __IOMMU_INIT(_detect, _depend, _init, _late_init, 0) 95 - 96 - void sort_iommu_table(struct iommu_table_entry *start, 97 - struct iommu_table_entry *finish); 98 - 99 - void check_iommu_entries(struct iommu_table_entry *start, 100 - struct iommu_table_entry *finish); 101 - 102 - #endif /* _ASM_X86_IOMMU_TABLE_H */
-30
arch/x86/include/asm/swiotlb.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #ifndef _ASM_X86_SWIOTLB_H 3 - #define _ASM_X86_SWIOTLB_H 4 - 5 - #include <linux/swiotlb.h> 6 - 7 - #ifdef CONFIG_SWIOTLB 8 - extern int swiotlb; 9 - extern int __init pci_swiotlb_detect_override(void); 10 - extern int __init pci_swiotlb_detect_4gb(void); 11 - extern void __init pci_swiotlb_init(void); 12 - extern void __init pci_swiotlb_late_init(void); 13 - #else 14 - #define swiotlb 0 15 - static inline int pci_swiotlb_detect_override(void) 16 - { 17 - return 0; 18 - } 19 - static inline int pci_swiotlb_detect_4gb(void) 20 - { 21 - return 0; 22 - } 23 - static inline void pci_swiotlb_init(void) 24 - { 25 - } 26 - static inline void pci_swiotlb_late_init(void) 27 - { 28 - } 29 - #endif 30 - #endif /* _ASM_X86_SWIOTLB_H */
-24
arch/x86/include/asm/xen/page-coherent.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #ifndef _ASM_X86_XEN_PAGE_COHERENT_H 3 - #define _ASM_X86_XEN_PAGE_COHERENT_H 4 - 5 - #include <asm/page.h> 6 - #include <linux/dma-mapping.h> 7 - 8 - static inline void *xen_alloc_coherent_pages(struct device *hwdev, size_t size, 9 - dma_addr_t *dma_handle, gfp_t flags, 10 - unsigned long attrs) 11 - { 12 - void *vstart = (void*)__get_free_pages(flags, get_order(size)); 13 - *dma_handle = virt_to_phys(vstart); 14 - return vstart; 15 - } 16 - 17 - static inline void xen_free_coherent_pages(struct device *hwdev, size_t size, 18 - void *cpu_addr, dma_addr_t dma_handle, 19 - unsigned long attrs) 20 - { 21 - free_pages((unsigned long) cpu_addr, get_order(size)); 22 - } 23 - 24 - #endif /* _ASM_X86_XEN_PAGE_COHERENT_H */
-5
arch/x86/include/asm/xen/page.h
··· 357 357 return false; 358 358 } 359 359 360 - static inline unsigned long xen_get_swiotlb_free_pages(unsigned int order) 361 - { 362 - return __get_free_pages(__GFP_NOWARN, order); 363 - } 364 - 365 360 #endif /* _ASM_X86_XEN_PAGE_H */
+6 -2
arch/x86/include/asm/xen/swiotlb-xen.h
··· 3 3 #define _ASM_X86_SWIOTLB_XEN_H 4 4 5 5 #ifdef CONFIG_SWIOTLB_XEN 6 - extern int __init pci_xen_swiotlb_detect(void); 7 6 extern int pci_xen_swiotlb_init_late(void); 8 7 #else 9 - #define pci_xen_swiotlb_detect NULL 10 8 static inline int pci_xen_swiotlb_init_late(void) { return -ENXIO; } 11 9 #endif 10 + 11 + int xen_swiotlb_fixup(void *buf, unsigned long nslabs); 12 + int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order, 13 + unsigned int address_bits, 14 + dma_addr_t *dma_handle); 15 + void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order); 12 16 13 17 #endif /* _ASM_X86_SWIOTLB_XEN_H */
-2
arch/x86/kernel/Makefile
··· 66 66 obj-y += pci-dma.o quirks.o topology.o kdebugfs.o 67 67 obj-y += alternative.o i8253.o hw_breakpoint.o 68 68 obj-y += tsc.o tsc_msr.o io_delay.o rtc.o 69 - obj-y += pci-iommu_table.o 70 69 obj-y += resource.o 71 70 obj-y += irqflags.o 72 71 obj-y += static_call.o ··· 131 132 132 133 obj-$(CONFIG_X86_CHECK_BIOS_CORRUPTION) += check.o 133 134 134 - obj-$(CONFIG_SWIOTLB) += pci-swiotlb.o 135 135 obj-$(CONFIG_OF) += devicetree.o 136 136 obj-$(CONFIG_UPROBES) += uprobes.o 137 137
+1 -4
arch/x86/kernel/amd_gart_64.c
··· 38 38 #include <asm/iommu.h> 39 39 #include <asm/gart.h> 40 40 #include <asm/set_memory.h> 41 - #include <asm/swiotlb.h> 42 41 #include <asm/dma.h> 43 42 #include <asm/amd_nb.h> 44 43 #include <asm/x86_init.h> 45 - #include <asm/iommu_table.h> 46 44 47 45 static unsigned long iommu_bus_base; /* GART remapping area (physical) */ 48 46 static unsigned long iommu_size; /* size of remapping area bytes */ ··· 806 808 flush_gart(); 807 809 dma_ops = &gart_dma_ops; 808 810 x86_platform.iommu_shutdown = gart_iommu_shutdown; 809 - swiotlb = 0; 811 + x86_swiotlb_enable = false; 810 812 811 813 return 0; 812 814 } ··· 840 842 } 841 843 } 842 844 } 843 - IOMMU_INIT_POST(gart_iommu_hole_init);
+5 -9
arch/x86/kernel/aperture_64.c
··· 392 392 393 393 static int __initdata printed_gart_size_msg; 394 394 395 - int __init gart_iommu_hole_init(void) 395 + void __init gart_iommu_hole_init(void) 396 396 { 397 397 u32 agp_aper_base = 0, agp_aper_order = 0; 398 398 u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0; ··· 401 401 int i, node; 402 402 403 403 if (!amd_gart_present()) 404 - return -ENODEV; 404 + return; 405 405 406 406 if (gart_iommu_aperture_disabled || !fix_aperture || 407 407 !early_pci_allowed()) 408 - return -ENODEV; 408 + return; 409 409 410 410 pr_info("Checking aperture...\n"); 411 411 ··· 491 491 * and fixed up the northbridge 492 492 */ 493 493 exclude_from_core(last_aper_base, last_aper_order); 494 - 495 - return 1; 496 494 } 497 - return 0; 495 + return; 498 496 } 499 497 500 498 if (!fallback_aper_force) { ··· 525 527 panic("Not enough memory for aperture"); 526 528 } 527 529 } else { 528 - return 0; 530 + return; 529 531 } 530 532 531 533 /* ··· 559 561 } 560 562 561 563 set_up_gart_resume(aper_order, aper_alloc); 562 - 563 - return 1; 564 564 }
-8
arch/x86/kernel/cpu/mshyperv.c
··· 337 337 swiotlb_unencrypted_base = ms_hyperv.shared_gpa_boundary; 338 338 #endif 339 339 } 340 - 341 - #ifdef CONFIG_SWIOTLB 342 - /* 343 - * Enable swiotlb force mode in Isolation VM to 344 - * use swiotlb bounce buffer for dma transaction. 345 - */ 346 - swiotlb_force = SWIOTLB_FORCE; 347 - #endif 348 340 /* Isolation VMs are unenlightened SEV-based VMs, thus this check: */ 349 341 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) { 350 342 if (hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE)
+93 -21
arch/x86/kernel/pci-dma.c
··· 7 7 #include <linux/memblock.h> 8 8 #include <linux/gfp.h> 9 9 #include <linux/pci.h> 10 + #include <linux/amd-iommu.h> 10 11 11 12 #include <asm/proto.h> 12 13 #include <asm/dma.h> 13 14 #include <asm/iommu.h> 14 15 #include <asm/gart.h> 15 16 #include <asm/x86_init.h> 16 - #include <asm/iommu_table.h> 17 + 18 + #include <xen/xen.h> 19 + #include <xen/swiotlb-xen.h> 17 20 18 21 static bool disable_dac_quirk __read_mostly; 19 22 ··· 37 34 /* Set this to 1 if there is a HW IOMMU in the system */ 38 35 int iommu_detected __read_mostly = 0; 39 36 40 - extern struct iommu_table_entry __iommu_table[], __iommu_table_end[]; 37 + #ifdef CONFIG_SWIOTLB 38 + bool x86_swiotlb_enable; 39 + static unsigned int x86_swiotlb_flags; 40 + 41 + static void __init pci_swiotlb_detect(void) 42 + { 43 + /* don't initialize swiotlb if iommu=off (no_iommu=1) */ 44 + if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) 45 + x86_swiotlb_enable = true; 46 + 47 + /* 48 + * Set swiotlb to 1 so that bounce buffers are allocated and used for 49 + * devices that can't support DMA to encrypted memory. 50 + */ 51 + if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) 52 + x86_swiotlb_enable = true; 53 + 54 + /* 55 + * Guest with guest memory encryption currently perform all DMA through 56 + * bounce buffers as the hypervisor can't access arbitrary VM memory 57 + * that is not explicitly shared with it. 58 + */ 59 + if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { 60 + x86_swiotlb_enable = true; 61 + x86_swiotlb_flags |= SWIOTLB_FORCE; 62 + } 63 + } 64 + #else 65 + static inline void __init pci_swiotlb_detect(void) 66 + { 67 + } 68 + #define x86_swiotlb_flags 0 69 + #endif /* CONFIG_SWIOTLB */ 70 + 71 + #ifdef CONFIG_SWIOTLB_XEN 72 + static void __init pci_xen_swiotlb_init(void) 73 + { 74 + if (!xen_initial_domain() && !x86_swiotlb_enable) 75 + return; 76 + x86_swiotlb_enable = true; 77 + x86_swiotlb_flags |= SWIOTLB_ANY; 78 + swiotlb_init_remap(true, x86_swiotlb_flags, xen_swiotlb_fixup); 79 + dma_ops = &xen_swiotlb_dma_ops; 80 + if (IS_ENABLED(CONFIG_PCI)) 81 + pci_request_acs(); 82 + } 83 + 84 + int pci_xen_swiotlb_init_late(void) 85 + { 86 + if (dma_ops == &xen_swiotlb_dma_ops) 87 + return 0; 88 + 89 + /* we can work with the default swiotlb */ 90 + if (!io_tlb_default_mem.nslabs) { 91 + int rc = swiotlb_init_late(swiotlb_size_or_default(), 92 + GFP_KERNEL, xen_swiotlb_fixup); 93 + if (rc < 0) 94 + return rc; 95 + } 96 + 97 + /* XXX: this switches the dma ops under live devices! */ 98 + dma_ops = &xen_swiotlb_dma_ops; 99 + if (IS_ENABLED(CONFIG_PCI)) 100 + pci_request_acs(); 101 + return 0; 102 + } 103 + EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late); 104 + #else 105 + static inline void __init pci_xen_swiotlb_init(void) 106 + { 107 + } 108 + #endif /* CONFIG_SWIOTLB_XEN */ 41 109 42 110 void __init pci_iommu_alloc(void) 43 111 { 44 - struct iommu_table_entry *p; 45 - 46 - sort_iommu_table(__iommu_table, __iommu_table_end); 47 - check_iommu_entries(__iommu_table, __iommu_table_end); 48 - 49 - for (p = __iommu_table; p < __iommu_table_end; p++) { 50 - if (p && p->detect && p->detect() > 0) { 51 - p->flags |= IOMMU_DETECTED; 52 - if (p->early_init) 53 - p->early_init(); 54 - if (p->flags & IOMMU_FINISH_IF_DETECTED) 55 - break; 56 - } 112 + if (xen_pv_domain()) { 113 + pci_xen_swiotlb_init(); 114 + return; 57 115 } 116 + pci_swiotlb_detect(); 117 + gart_iommu_hole_init(); 118 + amd_iommu_detect(); 119 + detect_intel_iommu(); 120 + swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags); 58 121 } 59 122 60 123 /* ··· 171 102 } 172 103 #ifdef CONFIG_SWIOTLB 173 104 if (!strncmp(p, "soft", 4)) 174 - swiotlb = 1; 105 + x86_swiotlb_enable = true; 175 106 #endif 176 107 if (!strncmp(p, "pt", 2)) 177 108 iommu_set_default_passthrough(true); ··· 190 121 191 122 static int __init pci_iommu_init(void) 192 123 { 193 - struct iommu_table_entry *p; 194 - 195 124 x86_init.iommu.iommu_init(); 196 125 197 - for (p = __iommu_table; p < __iommu_table_end; p++) { 198 - if (p && (p->flags & IOMMU_DETECTED) && p->late_init) 199 - p->late_init(); 126 + #ifdef CONFIG_SWIOTLB 127 + /* An IOMMU turned us off. */ 128 + if (x86_swiotlb_enable) { 129 + pr_info("PCI-DMA: Using software bounce buffering for IO (SWIOTLB)\n"); 130 + swiotlb_print_info(); 131 + } else { 132 + swiotlb_exit(); 200 133 } 134 + #endif 201 135 202 136 return 0; 203 137 }
-77
arch/x86/kernel/pci-iommu_table.c
··· 1 - // SPDX-License-Identifier: GPL-2.0 2 - #include <linux/dma-mapping.h> 3 - #include <asm/iommu_table.h> 4 - #include <linux/string.h> 5 - #include <linux/kallsyms.h> 6 - 7 - static struct iommu_table_entry * __init 8 - find_dependents_of(struct iommu_table_entry *start, 9 - struct iommu_table_entry *finish, 10 - struct iommu_table_entry *q) 11 - { 12 - struct iommu_table_entry *p; 13 - 14 - if (!q) 15 - return NULL; 16 - 17 - for (p = start; p < finish; p++) 18 - if (p->detect == q->depend) 19 - return p; 20 - 21 - return NULL; 22 - } 23 - 24 - 25 - void __init sort_iommu_table(struct iommu_table_entry *start, 26 - struct iommu_table_entry *finish) { 27 - 28 - struct iommu_table_entry *p, *q, tmp; 29 - 30 - for (p = start; p < finish; p++) { 31 - again: 32 - q = find_dependents_of(start, finish, p); 33 - /* We are bit sneaky here. We use the memory address to figure 34 - * out if the node we depend on is past our point, if so, swap. 35 - */ 36 - if (q > p) { 37 - tmp = *p; 38 - memmove(p, q, sizeof(*p)); 39 - *q = tmp; 40 - goto again; 41 - } 42 - } 43 - 44 - } 45 - 46 - #ifdef DEBUG 47 - void __init check_iommu_entries(struct iommu_table_entry *start, 48 - struct iommu_table_entry *finish) 49 - { 50 - struct iommu_table_entry *p, *q, *x; 51 - 52 - /* Simple cyclic dependency checker. */ 53 - for (p = start; p < finish; p++) { 54 - q = find_dependents_of(start, finish, p); 55 - x = find_dependents_of(start, finish, q); 56 - if (p == x) { 57 - printk(KERN_ERR "CYCLIC DEPENDENCY FOUND! %pS depends on %pS and vice-versa. BREAKING IT.\n", 58 - p->detect, q->detect); 59 - /* Heavy handed way..*/ 60 - x->depend = NULL; 61 - } 62 - } 63 - 64 - for (p = start; p < finish; p++) { 65 - q = find_dependents_of(p, finish, p); 66 - if (q && q > p) { 67 - printk(KERN_ERR "EXECUTION ORDER INVALID! %pS should be called before %pS!\n", 68 - p->detect, q->detect); 69 - } 70 - } 71 - } 72 - #else 73 - void __init check_iommu_entries(struct iommu_table_entry *start, 74 - struct iommu_table_entry *finish) 75 - { 76 - } 77 - #endif
-77
arch/x86/kernel/pci-swiotlb.c
··· 1 - // SPDX-License-Identifier: GPL-2.0 2 - 3 - #include <linux/pci.h> 4 - #include <linux/cache.h> 5 - #include <linux/init.h> 6 - #include <linux/swiotlb.h> 7 - #include <linux/memblock.h> 8 - #include <linux/dma-direct.h> 9 - #include <linux/cc_platform.h> 10 - 11 - #include <asm/iommu.h> 12 - #include <asm/swiotlb.h> 13 - #include <asm/dma.h> 14 - #include <asm/xen/swiotlb-xen.h> 15 - #include <asm/iommu_table.h> 16 - 17 - int swiotlb __read_mostly; 18 - 19 - /* 20 - * pci_swiotlb_detect_override - set swiotlb to 1 if necessary 21 - * 22 - * This returns non-zero if we are forced to use swiotlb (by the boot 23 - * option). 24 - */ 25 - int __init pci_swiotlb_detect_override(void) 26 - { 27 - if (swiotlb_force == SWIOTLB_FORCE) 28 - swiotlb = 1; 29 - 30 - return swiotlb; 31 - } 32 - IOMMU_INIT_FINISH(pci_swiotlb_detect_override, 33 - pci_xen_swiotlb_detect, 34 - pci_swiotlb_init, 35 - pci_swiotlb_late_init); 36 - 37 - /* 38 - * If 4GB or more detected (and iommu=off not set) or if SME is active 39 - * then set swiotlb to 1 and return 1. 40 - */ 41 - int __init pci_swiotlb_detect_4gb(void) 42 - { 43 - /* don't initialize swiotlb if iommu=off (no_iommu=1) */ 44 - if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) 45 - swiotlb = 1; 46 - 47 - /* 48 - * Set swiotlb to 1 so that bounce buffers are allocated and used for 49 - * devices that can't support DMA to encrypted memory. 50 - */ 51 - if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) 52 - swiotlb = 1; 53 - 54 - return swiotlb; 55 - } 56 - IOMMU_INIT(pci_swiotlb_detect_4gb, 57 - pci_swiotlb_detect_override, 58 - pci_swiotlb_init, 59 - pci_swiotlb_late_init); 60 - 61 - void __init pci_swiotlb_init(void) 62 - { 63 - if (swiotlb) 64 - swiotlb_init(0); 65 - } 66 - 67 - void __init pci_swiotlb_late_init(void) 68 - { 69 - /* An IOMMU turned us off. */ 70 - if (!swiotlb) 71 - swiotlb_exit(); 72 - else { 73 - printk(KERN_INFO "PCI-DMA: " 74 - "Using software bounce buffering for IO (SWIOTLB)\n"); 75 - swiotlb_print_info(); 76 - } 77 - }
-1
arch/x86/kernel/tboot.c
··· 24 24 #include <asm/processor.h> 25 25 #include <asm/bootparam.h> 26 26 #include <asm/pgalloc.h> 27 - #include <asm/swiotlb.h> 28 27 #include <asm/fixmap.h> 29 28 #include <asm/proto.h> 30 29 #include <asm/setup.h>
-12
arch/x86/kernel/vmlinux.lds.S
··· 315 315 *(.altinstr_replacement) 316 316 } 317 317 318 - /* 319 - * struct iommu_table_entry entries are injected in this section. 320 - * It is an array of IOMMUs which during run time gets sorted depending 321 - * on its dependency order. After rootfs_initcall is complete 322 - * this section can be safely removed. 323 - */ 324 - .iommu_table : AT(ADDR(.iommu_table) - LOAD_OFFSET) { 325 - __iommu_table = .; 326 - *(.iommu_table) 327 - __iommu_table_end = .; 328 - } 329 - 330 318 . = ALIGN(8); 331 319 .apicdrivers : AT(ADDR(.apicdrivers) - LOAD_OFFSET) { 332 320 __apicdrivers = .;
-3
arch/x86/mm/mem_encrypt_amd.c
··· 495 495 for (i = 0; i < ARRAY_SIZE(protection_map); i++) 496 496 protection_map[i] = pgprot_encrypted(protection_map[i]); 497 497 498 - if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) 499 - swiotlb_force = SWIOTLB_FORCE; 500 - 501 498 x86_platform.guest.enc_status_change_prepare = amd_enc_status_change_prepare; 502 499 x86_platform.guest.enc_status_change_finish = amd_enc_status_change_finish; 503 500 x86_platform.guest.enc_tlb_flush_required = amd_enc_tlb_flush_required;
+1 -1
arch/x86/pci/sta2x11-fixup.c
··· 57 57 int size = STA2X11_SWIOTLB_SIZE; 58 58 /* First instance: register your own swiotlb area */ 59 59 dev_info(&pdev->dev, "Using SWIOTLB (size %i)\n", size); 60 - if (swiotlb_late_init_with_default_size(size)) 60 + if (swiotlb_init_late(size, GFP_DMA, NULL)) 61 61 dev_emerg(&pdev->dev, "init swiotlb failed\n"); 62 62 } 63 63 list_add(&instance->list, &sta2x11_instance_list);
-2
arch/x86/xen/Makefile
··· 47 47 48 48 obj-$(CONFIG_XEN_PV_DOM0) += vga.o 49 49 50 - obj-$(CONFIG_SWIOTLB_XEN) += pci-swiotlb-xen.o 51 - 52 50 obj-$(CONFIG_XEN_EFI) += efi.o
+1
arch/x86/xen/mmu_pv.c
··· 80 80 #include <xen/interface/version.h> 81 81 #include <xen/interface/memory.h> 82 82 #include <xen/hvc-console.h> 83 + #include <xen/swiotlb-xen.h> 83 84 84 85 #include "multicalls.h" 85 86 #include "mmu.h"
-96
arch/x86/xen/pci-swiotlb-xen.c
··· 1 - // SPDX-License-Identifier: GPL-2.0 2 - 3 - /* Glue code to lib/swiotlb-xen.c */ 4 - 5 - #include <linux/dma-map-ops.h> 6 - #include <linux/pci.h> 7 - #include <xen/swiotlb-xen.h> 8 - 9 - #include <asm/xen/hypervisor.h> 10 - #include <xen/xen.h> 11 - #include <asm/iommu_table.h> 12 - 13 - 14 - #include <asm/xen/swiotlb-xen.h> 15 - #ifdef CONFIG_X86_64 16 - #include <asm/iommu.h> 17 - #include <asm/dma.h> 18 - #endif 19 - #include <linux/export.h> 20 - 21 - static int xen_swiotlb __read_mostly; 22 - 23 - /* 24 - * pci_xen_swiotlb_detect - set xen_swiotlb to 1 if necessary 25 - * 26 - * This returns non-zero if we are forced to use xen_swiotlb (by the boot 27 - * option). 28 - */ 29 - int __init pci_xen_swiotlb_detect(void) 30 - { 31 - 32 - if (!xen_pv_domain()) 33 - return 0; 34 - 35 - /* If running as PV guest, either iommu=soft, or swiotlb=force will 36 - * activate this IOMMU. If running as PV privileged, activate it 37 - * irregardless. 38 - */ 39 - if (xen_initial_domain() || swiotlb || swiotlb_force == SWIOTLB_FORCE) 40 - xen_swiotlb = 1; 41 - 42 - /* If we are running under Xen, we MUST disable the native SWIOTLB. 43 - * Don't worry about swiotlb_force flag activating the native, as 44 - * the 'swiotlb' flag is the only one turning it on. */ 45 - swiotlb = 0; 46 - 47 - #ifdef CONFIG_X86_64 48 - /* pci_swiotlb_detect_4gb turns on native SWIOTLB if no_iommu == 0 49 - * (so no iommu=X command line over-writes). 50 - * Considering that PV guests do not want the *native SWIOTLB* but 51 - * only Xen SWIOTLB it is not useful to us so set no_iommu=1 here. 52 - */ 53 - if (max_pfn > MAX_DMA32_PFN) 54 - no_iommu = 1; 55 - #endif 56 - return xen_swiotlb; 57 - } 58 - 59 - static void __init pci_xen_swiotlb_init(void) 60 - { 61 - if (xen_swiotlb) { 62 - xen_swiotlb_init_early(); 63 - dma_ops = &xen_swiotlb_dma_ops; 64 - 65 - #ifdef CONFIG_PCI 66 - /* Make sure ACS will be enabled */ 67 - pci_request_acs(); 68 - #endif 69 - } 70 - } 71 - 72 - int pci_xen_swiotlb_init_late(void) 73 - { 74 - int rc; 75 - 76 - if (xen_swiotlb) 77 - return 0; 78 - 79 - rc = xen_swiotlb_init(); 80 - if (rc) 81 - return rc; 82 - 83 - dma_ops = &xen_swiotlb_dma_ops; 84 - #ifdef CONFIG_PCI 85 - /* Make sure ACS will be enabled */ 86 - pci_request_acs(); 87 - #endif 88 - 89 - return 0; 90 - } 91 - EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late); 92 - 93 - IOMMU_INIT_FINISH(pci_xen_swiotlb_detect, 94 - NULL, 95 - pci_xen_swiotlb_init, 96 - NULL);
-6
drivers/iommu/amd/init.c
··· 27 27 #include <asm/apic.h> 28 28 #include <asm/gart.h> 29 29 #include <asm/x86_init.h> 30 - #include <asm/iommu_table.h> 31 30 #include <asm/io_apic.h> 32 31 #include <asm/irq_remapping.h> 33 32 #include <asm/set_memory.h> ··· 3255 3256 __setup("ivrs_ioapic", parse_ivrs_ioapic); 3256 3257 __setup("ivrs_hpet", parse_ivrs_hpet); 3257 3258 __setup("ivrs_acpihid", parse_ivrs_acpihid); 3258 - 3259 - IOMMU_INIT_FINISH(amd_iommu_detect, 3260 - gart_iommu_hole_init, 3261 - NULL, 3262 - NULL); 3263 3259 3264 3260 bool amd_iommu_v2_supported(void) 3265 3261 {
+4 -1
drivers/iommu/amd/iommu.c
··· 1840 1840 1841 1841 static void __init amd_iommu_init_dma_ops(void) 1842 1842 { 1843 - swiotlb = (iommu_default_passthrough() || sme_me_mask) ? 1 : 0; 1843 + if (iommu_default_passthrough() || sme_me_mask) 1844 + x86_swiotlb_enable = true; 1845 + else 1846 + x86_swiotlb_enable = false; 1844 1847 } 1845 1848 1846 1849 int __init amd_iommu_init_api(void)
+1 -5
drivers/iommu/intel/dmar.c
··· 30 30 #include <linux/numa.h> 31 31 #include <linux/limits.h> 32 32 #include <asm/irq_remapping.h> 33 - #include <asm/iommu_table.h> 34 33 #include <trace/events/intel_iommu.h> 35 34 36 35 #include "../irq_remapping.h" ··· 911 912 return 0; 912 913 } 913 914 914 - int __init detect_intel_iommu(void) 915 + void __init detect_intel_iommu(void) 915 916 { 916 917 int ret; 917 918 struct dmar_res_callback validate_drhd_cb = { ··· 944 945 dmar_tbl = NULL; 945 946 } 946 947 up_write(&dmar_global_lock); 947 - 948 - return ret ? ret : 1; 949 948 } 950 949 951 950 static void unmap_iommu(struct intel_iommu *iommu) ··· 2161 2164 } 2162 2165 2163 2166 late_initcall(dmar_free_unused_resources); 2164 - IOMMU_INIT_POST(detect_intel_iommu); 2165 2167 2166 2168 /* 2167 2169 * DMAR Hotplug Support
+35 -194
drivers/xen/swiotlb-xen.c
··· 36 36 #include <xen/hvc-console.h> 37 37 38 38 #include <asm/dma-mapping.h> 39 - #include <asm/xen/page-coherent.h> 40 39 41 40 #include <trace/events/swiotlb.h> 42 41 #define MAX_DMA_BITS 32 ··· 103 104 return 0; 104 105 } 105 106 106 - static int xen_swiotlb_fixup(void *buf, unsigned long nslabs) 107 + #ifdef CONFIG_X86 108 + int xen_swiotlb_fixup(void *buf, unsigned long nslabs) 107 109 { 108 110 int rc; 109 111 unsigned int order = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT); ··· 130 130 return 0; 131 131 } 132 132 133 - enum xen_swiotlb_err { 134 - XEN_SWIOTLB_UNKNOWN = 0, 135 - XEN_SWIOTLB_ENOMEM, 136 - XEN_SWIOTLB_EFIXUP 137 - }; 138 - 139 - static const char *xen_swiotlb_error(enum xen_swiotlb_err err) 140 - { 141 - switch (err) { 142 - case XEN_SWIOTLB_ENOMEM: 143 - return "Cannot allocate Xen-SWIOTLB buffer\n"; 144 - case XEN_SWIOTLB_EFIXUP: 145 - return "Failed to get contiguous memory for DMA from Xen!\n"\ 146 - "You either: don't have the permissions, do not have"\ 147 - " enough free memory under 4GB, or the hypervisor memory"\ 148 - " is too fragmented!"; 149 - default: 150 - break; 151 - } 152 - return ""; 153 - } 154 - 155 - int xen_swiotlb_init(void) 156 - { 157 - enum xen_swiotlb_err m_ret = XEN_SWIOTLB_UNKNOWN; 158 - unsigned long bytes = swiotlb_size_or_default(); 159 - unsigned long nslabs = bytes >> IO_TLB_SHIFT; 160 - unsigned int order, repeat = 3; 161 - int rc = -ENOMEM; 162 - char *start; 163 - 164 - if (io_tlb_default_mem.nslabs) { 165 - pr_warn("swiotlb buffer already initialized\n"); 166 - return -EEXIST; 167 - } 168 - 169 - retry: 170 - m_ret = XEN_SWIOTLB_ENOMEM; 171 - order = get_order(bytes); 172 - 173 - /* 174 - * Get IO TLB memory from any location. 175 - */ 176 - #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT)) 177 - #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT) 178 - while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { 179 - start = (void *)xen_get_swiotlb_free_pages(order); 180 - if (start) 181 - break; 182 - order--; 183 - } 184 - if (!start) 185 - goto exit; 186 - if (order != get_order(bytes)) { 187 - pr_warn("Warning: only able to allocate %ld MB for software IO TLB\n", 188 - (PAGE_SIZE << order) >> 20); 189 - nslabs = SLABS_PER_PAGE << order; 190 - bytes = nslabs << IO_TLB_SHIFT; 191 - } 192 - 193 - /* 194 - * And replace that memory with pages under 4GB. 195 - */ 196 - rc = xen_swiotlb_fixup(start, nslabs); 197 - if (rc) { 198 - free_pages((unsigned long)start, order); 199 - m_ret = XEN_SWIOTLB_EFIXUP; 200 - goto error; 201 - } 202 - rc = swiotlb_late_init_with_tbl(start, nslabs); 203 - if (rc) 204 - return rc; 205 - swiotlb_set_max_segment(PAGE_SIZE); 206 - return 0; 207 - error: 208 - if (nslabs > 1024 && repeat--) { 209 - /* Min is 2MB */ 210 - nslabs = max(1024UL, ALIGN(nslabs >> 1, IO_TLB_SEGSIZE)); 211 - bytes = nslabs << IO_TLB_SHIFT; 212 - pr_info("Lowering to %luMB\n", bytes >> 20); 213 - goto retry; 214 - } 215 - exit: 216 - pr_err("%s (rc:%d)\n", xen_swiotlb_error(m_ret), rc); 217 - return rc; 218 - } 219 - 220 - #ifdef CONFIG_X86 221 - void __init xen_swiotlb_init_early(void) 222 - { 223 - unsigned long bytes = swiotlb_size_or_default(); 224 - unsigned long nslabs = bytes >> IO_TLB_SHIFT; 225 - unsigned int repeat = 3; 226 - char *start; 227 - int rc; 228 - 229 - retry: 230 - /* 231 - * Get IO TLB memory from any location. 232 - */ 233 - start = memblock_alloc(PAGE_ALIGN(bytes), 234 - IO_TLB_SEGSIZE << IO_TLB_SHIFT); 235 - if (!start) 236 - panic("%s: Failed to allocate %lu bytes\n", 237 - __func__, PAGE_ALIGN(bytes)); 238 - 239 - /* 240 - * And replace that memory with pages under 4GB. 241 - */ 242 - rc = xen_swiotlb_fixup(start, nslabs); 243 - if (rc) { 244 - memblock_free(start, PAGE_ALIGN(bytes)); 245 - if (nslabs > 1024 && repeat--) { 246 - /* Min is 2MB */ 247 - nslabs = max(1024UL, ALIGN(nslabs >> 1, IO_TLB_SEGSIZE)); 248 - bytes = nslabs << IO_TLB_SHIFT; 249 - pr_info("Lowering to %luMB\n", bytes >> 20); 250 - goto retry; 251 - } 252 - panic("%s (rc:%d)", xen_swiotlb_error(XEN_SWIOTLB_EFIXUP), rc); 253 - } 254 - 255 - if (swiotlb_init_with_tbl(start, nslabs, true)) 256 - panic("Cannot allocate SWIOTLB buffer"); 257 - swiotlb_set_max_segment(PAGE_SIZE); 258 - } 259 - #endif /* CONFIG_X86 */ 260 - 261 133 static void * 262 - xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size, 263 - dma_addr_t *dma_handle, gfp_t flags, 264 - unsigned long attrs) 134 + xen_swiotlb_alloc_coherent(struct device *dev, size_t size, 135 + dma_addr_t *dma_handle, gfp_t flags, unsigned long attrs) 265 136 { 266 - void *ret; 137 + u64 dma_mask = dev->coherent_dma_mask; 267 138 int order = get_order(size); 268 - u64 dma_mask = DMA_BIT_MASK(32); 269 139 phys_addr_t phys; 270 - dma_addr_t dev_addr; 140 + void *ret; 271 141 272 - /* 273 - * Ignore region specifiers - the kernel's ideas of 274 - * pseudo-phys memory layout has nothing to do with the 275 - * machine physical layout. We can't allocate highmem 276 - * because we can't return a pointer to it. 277 - */ 278 - flags &= ~(__GFP_DMA | __GFP_HIGHMEM); 279 - 280 - /* Convert the size to actually allocated. */ 142 + /* Align the allocation to the Xen page size */ 281 143 size = 1UL << (order + XEN_PAGE_SHIFT); 282 144 283 - /* On ARM this function returns an ioremap'ped virtual address for 284 - * which virt_to_phys doesn't return the corresponding physical 285 - * address. In fact on ARM virt_to_phys only works for kernel direct 286 - * mapped RAM memory. Also see comment below. 287 - */ 288 - ret = xen_alloc_coherent_pages(hwdev, size, dma_handle, flags, attrs); 289 - 145 + ret = (void *)__get_free_pages(flags, get_order(size)); 290 146 if (!ret) 291 147 return ret; 148 + phys = virt_to_phys(ret); 292 149 293 - if (hwdev && hwdev->coherent_dma_mask) 294 - dma_mask = hwdev->coherent_dma_mask; 295 - 296 - /* At this point dma_handle is the dma address, next we are 297 - * going to set it to the machine address. 298 - * Do not use virt_to_phys(ret) because on ARM it doesn't correspond 299 - * to *dma_handle. */ 300 - phys = dma_to_phys(hwdev, *dma_handle); 301 - dev_addr = xen_phys_to_dma(hwdev, phys); 302 - if (((dev_addr + size - 1 <= dma_mask)) && 303 - !range_straddles_page_boundary(phys, size)) 304 - *dma_handle = dev_addr; 305 - else { 306 - if (xen_create_contiguous_region(phys, order, 307 - fls64(dma_mask), dma_handle) != 0) { 308 - xen_free_coherent_pages(hwdev, size, ret, (dma_addr_t)phys, attrs); 309 - return NULL; 310 - } 311 - *dma_handle = phys_to_dma(hwdev, *dma_handle); 150 + *dma_handle = xen_phys_to_dma(dev, phys); 151 + if (*dma_handle + size - 1 > dma_mask || 152 + range_straddles_page_boundary(phys, size)) { 153 + if (xen_create_contiguous_region(phys, order, fls64(dma_mask), 154 + dma_handle) != 0) 155 + goto out_free_pages; 312 156 SetPageXenRemapped(virt_to_page(ret)); 313 157 } 158 + 314 159 memset(ret, 0, size); 315 160 return ret; 161 + 162 + out_free_pages: 163 + free_pages((unsigned long)ret, get_order(size)); 164 + return NULL; 316 165 } 317 166 318 167 static void 319 - xen_swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr, 320 - dma_addr_t dev_addr, unsigned long attrs) 168 + xen_swiotlb_free_coherent(struct device *dev, size_t size, void *vaddr, 169 + dma_addr_t dma_handle, unsigned long attrs) 321 170 { 171 + phys_addr_t phys = virt_to_phys(vaddr); 322 172 int order = get_order(size); 323 - phys_addr_t phys; 324 - u64 dma_mask = DMA_BIT_MASK(32); 325 - struct page *page; 326 - 327 - if (hwdev && hwdev->coherent_dma_mask) 328 - dma_mask = hwdev->coherent_dma_mask; 329 - 330 - /* do not use virt_to_phys because on ARM it doesn't return you the 331 - * physical address */ 332 - phys = xen_dma_to_phys(hwdev, dev_addr); 333 173 334 174 /* Convert the size to actually allocated. */ 335 175 size = 1UL << (order + XEN_PAGE_SHIFT); 336 176 337 - if (is_vmalloc_addr(vaddr)) 338 - page = vmalloc_to_page(vaddr); 339 - else 340 - page = virt_to_page(vaddr); 177 + if (WARN_ON_ONCE(dma_handle + size - 1 > dev->coherent_dma_mask) || 178 + WARN_ON_ONCE(range_straddles_page_boundary(phys, size))) 179 + return; 341 180 342 - if (!WARN_ON((dev_addr + size - 1 > dma_mask) || 343 - range_straddles_page_boundary(phys, size)) && 344 - TestClearPageXenRemapped(page)) 181 + if (TestClearPageXenRemapped(virt_to_page(vaddr))) 345 182 xen_destroy_contiguous_region(phys, order); 346 - 347 - xen_free_coherent_pages(hwdev, size, vaddr, phys_to_dma(hwdev, phys), 348 - attrs); 183 + free_pages((unsigned long)vaddr, get_order(size)); 349 184 } 185 + #endif /* CONFIG_X86 */ 350 186 351 187 /* 352 188 * Map a single buffer of the indicated size for DMA in streaming mode. The ··· 214 378 /* 215 379 * Oh well, have to allocate and map a bounce buffer. 216 380 */ 217 - trace_swiotlb_bounced(dev, dev_addr, size, swiotlb_force); 381 + trace_swiotlb_bounced(dev, dev_addr, size); 218 382 219 383 map = swiotlb_tbl_map_single(dev, phys, size, size, 0, dir, attrs); 220 384 if (map == (phys_addr_t)DMA_MAPPING_ERROR) ··· 385 549 } 386 550 387 551 const struct dma_map_ops xen_swiotlb_dma_ops = { 552 + #ifdef CONFIG_X86 388 553 .alloc = xen_swiotlb_alloc_coherent, 389 554 .free = xen_swiotlb_free_coherent, 555 + #else 556 + .alloc = dma_direct_alloc, 557 + .free = dma_direct_free, 558 + #endif 390 559 .sync_single_for_cpu = xen_swiotlb_sync_single_for_cpu, 391 560 .sync_single_for_device = xen_swiotlb_sync_single_for_device, 392 561 .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu,
+5 -1
include/linux/dmar.h
··· 121 121 u16 segment, struct dmar_dev_scope *devices, 122 122 int count); 123 123 /* Intel IOMMU detection */ 124 - extern int detect_intel_iommu(void); 124 + void detect_intel_iommu(void); 125 125 extern int enable_drhd_fault_handling(void); 126 126 extern int dmar_device_add(acpi_handle handle); 127 127 extern int dmar_device_remove(acpi_handle handle); ··· 195 195 static inline bool dmar_platform_optin(void) 196 196 { 197 197 return false; 198 + } 199 + 200 + static inline void detect_intel_iommu(void) 201 + { 198 202 } 199 203 200 204 #endif /* CONFIG_DMAR_TABLE */
+11 -11
include/linux/swiotlb.h
··· 13 13 struct page; 14 14 struct scatterlist; 15 15 16 - enum swiotlb_force { 17 - SWIOTLB_NORMAL, /* Default - depending on HW DMA mask etc. */ 18 - SWIOTLB_FORCE, /* swiotlb=force */ 19 - SWIOTLB_NO_FORCE, /* swiotlb=noforce */ 20 - }; 16 + #define SWIOTLB_VERBOSE (1 << 0) /* verbose initialization */ 17 + #define SWIOTLB_FORCE (1 << 1) /* force bounce buffering */ 18 + #define SWIOTLB_ANY (1 << 2) /* allow any memory for the buffer */ 21 19 22 20 /* 23 21 * Maximum allowable number of contiguous slabs to map, ··· 34 36 /* default to 64MB */ 35 37 #define IO_TLB_DEFAULT_SIZE (64UL<<20) 36 38 37 - extern void swiotlb_init(int verbose); 38 - int swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose); 39 39 unsigned long swiotlb_size_or_default(void); 40 - extern int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs); 41 - extern int swiotlb_late_init_with_default_size(size_t default_size); 40 + void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags, 41 + int (*remap)(void *tlb, unsigned long nslabs)); 42 + int swiotlb_init_late(size_t size, gfp_t gfp_mask, 43 + int (*remap)(void *tlb, unsigned long nslabs)); 42 44 extern void __init swiotlb_update_mem_attributes(void); 43 45 44 46 phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t phys, ··· 124 126 return mem && mem->force_bounce; 125 127 } 126 128 129 + void swiotlb_init(bool addressing_limited, unsigned int flags); 127 130 void __init swiotlb_exit(void); 128 131 unsigned int swiotlb_max_segment(void); 129 132 size_t swiotlb_max_mapping_size(struct device *dev); 130 133 bool is_swiotlb_active(struct device *dev); 131 134 void __init swiotlb_adjust_size(unsigned long size); 132 135 #else 133 - #define swiotlb_force SWIOTLB_NO_FORCE 136 + static inline void swiotlb_init(bool addressing_limited, unsigned int flags) 137 + { 138 + } 134 139 static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) 135 140 { 136 141 return false; ··· 165 164 #endif /* CONFIG_SWIOTLB */ 166 165 167 166 extern void swiotlb_print_info(void); 168 - extern void swiotlb_set_max_segment(unsigned int); 169 167 170 168 #ifdef CONFIG_DMA_RESTRICTED_POOL 171 169 struct page *swiotlb_alloc(struct device *dev, size_t size);
+10 -19
include/trace/events/swiotlb.h
··· 8 8 #include <linux/tracepoint.h> 9 9 10 10 TRACE_EVENT(swiotlb_bounced, 11 - 12 - TP_PROTO(struct device *dev, 13 - dma_addr_t dev_addr, 14 - size_t size, 15 - enum swiotlb_force swiotlb_force), 16 - 17 - TP_ARGS(dev, dev_addr, size, swiotlb_force), 11 + TP_PROTO(struct device *dev, dma_addr_t dev_addr, size_t size), 12 + TP_ARGS(dev, dev_addr, size), 18 13 19 14 TP_STRUCT__entry( 20 - __string( dev_name, dev_name(dev) ) 21 - __field( u64, dma_mask ) 22 - __field( dma_addr_t, dev_addr ) 23 - __field( size_t, size ) 24 - __field( enum swiotlb_force, swiotlb_force ) 15 + __string(dev_name, dev_name(dev)) 16 + __field(u64, dma_mask) 17 + __field(dma_addr_t, dev_addr) 18 + __field(size_t, size) 19 + __field(bool, force) 25 20 ), 26 21 27 22 TP_fast_assign( ··· 24 29 __entry->dma_mask = (dev->dma_mask ? *dev->dma_mask : 0); 25 30 __entry->dev_addr = dev_addr; 26 31 __entry->size = size; 27 - __entry->swiotlb_force = swiotlb_force; 32 + __entry->force = is_swiotlb_force_bounce(dev); 28 33 ), 29 34 30 - TP_printk("dev_name: %s dma_mask=%llx dev_addr=%llx " 31 - "size=%zu %s", 35 + TP_printk("dev_name: %s dma_mask=%llx dev_addr=%llx size=%zu %s", 32 36 __get_str(dev_name), 33 37 __entry->dma_mask, 34 38 (unsigned long long)__entry->dev_addr, 35 39 __entry->size, 36 - __print_symbolic(__entry->swiotlb_force, 37 - { SWIOTLB_NORMAL, "NORMAL" }, 38 - { SWIOTLB_FORCE, "FORCE" }, 39 - { SWIOTLB_NO_FORCE, "NO_FORCE" })) 40 + __entry->force ? "FORCE" : "NORMAL") 40 41 ); 41 42 42 43 #endif /* _TRACE_SWIOTLB_H */
-20
include/xen/arm/page-coherent.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #ifndef _XEN_ARM_PAGE_COHERENT_H 3 - #define _XEN_ARM_PAGE_COHERENT_H 4 - 5 - #include <linux/dma-mapping.h> 6 - #include <asm/page.h> 7 - 8 - static inline void *xen_alloc_coherent_pages(struct device *hwdev, size_t size, 9 - dma_addr_t *dma_handle, gfp_t flags, unsigned long attrs) 10 - { 11 - return dma_direct_alloc(hwdev, size, dma_handle, flags, attrs); 12 - } 13 - 14 - static inline void xen_free_coherent_pages(struct device *hwdev, size_t size, 15 - void *cpu_addr, dma_addr_t dma_handle, unsigned long attrs) 16 - { 17 - dma_direct_free(hwdev, size, cpu_addr, dma_handle, attrs); 18 - } 19 - 20 - #endif /* _XEN_ARM_PAGE_COHERENT_H */
-1
include/xen/arm/page.h
··· 115 115 bool xen_arch_need_swiotlb(struct device *dev, 116 116 phys_addr_t phys, 117 117 dma_addr_t dev_addr); 118 - unsigned long xen_get_swiotlb_free_pages(unsigned int order); 119 118 120 119 #endif /* _ASM_ARM_XEN_PAGE_H */
-2
include/xen/swiotlb-xen.h
··· 10 10 void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle, 11 11 size_t size, enum dma_data_direction dir); 12 12 13 - int xen_swiotlb_init(void); 14 - void __init xen_swiotlb_init_early(void); 15 13 extern const struct dma_map_ops xen_swiotlb_dma_ops; 16 14 17 15 #endif /* __LINUX_SWIOTLB_XEN_H */
-7
include/xen/xen-ops.h
··· 42 42 43 43 extern unsigned long *xen_contiguous_bitmap; 44 44 45 - #if defined(CONFIG_XEN_PV) || defined(CONFIG_ARM) || defined(CONFIG_ARM64) 46 - int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order, 47 - unsigned int address_bits, 48 - dma_addr_t *dma_handle); 49 - void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order); 50 - #endif 51 - 52 45 #if defined(CONFIG_XEN_PV) 53 46 int xen_remap_pfn(struct vm_area_struct *vma, unsigned long addr, 54 47 xen_pfn_t *pfn, int nr, int *err_ptr, pgprot_t prot,
+1 -1
kernel/dma/debug.c
··· 448 448 * other hand, consumes a single dma_debug_entry, but inserts 'nents' 449 449 * entries into the tree. 450 450 */ 451 - static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT); 451 + static RADIX_TREE(dma_active_cacheline, GFP_ATOMIC); 452 452 static DEFINE_SPINLOCK(radix_lock); 453 453 #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1) 454 454 #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
+12 -19
kernel/dma/direct.c
··· 79 79 { 80 80 if (!force_dma_unencrypted(dev)) 81 81 return 0; 82 - return set_memory_decrypted((unsigned long)vaddr, 1 << get_order(size)); 82 + return set_memory_decrypted((unsigned long)vaddr, PFN_UP(size)); 83 83 } 84 84 85 85 static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size) ··· 88 88 89 89 if (!force_dma_unencrypted(dev)) 90 90 return 0; 91 - ret = set_memory_encrypted((unsigned long)vaddr, 1 << get_order(size)); 91 + ret = set_memory_encrypted((unsigned long)vaddr, PFN_UP(size)); 92 92 if (ret) 93 93 pr_warn_ratelimited("leaking DMA memory that can't be re-encrypted\n"); 94 94 return ret; ··· 115 115 } 116 116 117 117 static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, 118 - gfp_t gfp) 118 + gfp_t gfp, bool allow_highmem) 119 119 { 120 120 int node = dev_to_node(dev); 121 121 struct page *page = NULL; ··· 129 129 gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask, 130 130 &phys_limit); 131 131 page = dma_alloc_contiguous(dev, size, gfp); 132 - if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) { 133 - dma_free_contiguous(dev, page, size); 134 - page = NULL; 132 + if (page) { 133 + if (!dma_coherent_ok(dev, page_to_phys(page), size) || 134 + (!allow_highmem && PageHighMem(page))) { 135 + dma_free_contiguous(dev, page, size); 136 + page = NULL; 137 + } 135 138 } 136 139 again: 137 140 if (!page) ··· 192 189 { 193 190 struct page *page; 194 191 195 - page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO); 192 + page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true); 196 193 if (!page) 197 194 return NULL; 198 195 ··· 265 262 return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp); 266 263 267 264 /* we always manually zero the memory once we are done */ 268 - page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO); 265 + page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true); 269 266 if (!page) 270 267 return NULL; 271 268 ··· 373 370 if (force_dma_unencrypted(dev) && dma_direct_use_pool(dev, gfp)) 374 371 return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp); 375 372 376 - page = __dma_direct_alloc_pages(dev, size, gfp); 373 + page = __dma_direct_alloc_pages(dev, size, gfp, false); 377 374 if (!page) 378 375 return NULL; 379 - if (PageHighMem(page)) { 380 - /* 381 - * Depending on the cma= arguments and per-arch setup 382 - * dma_alloc_contiguous could return highmem pages. 383 - * Without remapping there is no way to return them here, 384 - * so log an error and fail. 385 - */ 386 - dev_info(dev, "Rejecting highmem page from CMA.\n"); 387 - goto out_free_pages; 388 - } 389 376 390 377 ret = page_address(page); 391 378 if (dma_set_decrypted(dev, ret, size))
+1 -1
kernel/dma/direct.h
··· 91 91 return swiotlb_map(dev, phys, size, dir, attrs); 92 92 93 93 if (unlikely(!dma_capable(dev, dma_addr, size, true))) { 94 - if (swiotlb_force != SWIOTLB_NO_FORCE) 94 + if (is_swiotlb_active(dev)) 95 95 return swiotlb_map(dev, phys, size, dir, attrs); 96 96 97 97 dev_WARN_ONCE(dev, 1,
+95 -91
kernel/dma/swiotlb.c
··· 62 62 63 63 #define INVALID_PHYS_ADDR (~(phys_addr_t)0) 64 64 65 - enum swiotlb_force swiotlb_force; 65 + static bool swiotlb_force_bounce; 66 + static bool swiotlb_force_disable; 66 67 67 68 struct io_tlb_mem io_tlb_default_mem; 68 69 69 70 phys_addr_t swiotlb_unencrypted_base; 70 - 71 - /* 72 - * Max segment that we can provide which (if pages are contingous) will 73 - * not be bounced (unless SWIOTLB_FORCE is set). 74 - */ 75 - static unsigned int max_segment; 76 71 77 72 static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT; 78 73 ··· 82 87 if (*str == ',') 83 88 ++str; 84 89 if (!strcmp(str, "force")) 85 - swiotlb_force = SWIOTLB_FORCE; 90 + swiotlb_force_bounce = true; 86 91 else if (!strcmp(str, "noforce")) 87 - swiotlb_force = SWIOTLB_NO_FORCE; 92 + swiotlb_force_disable = true; 88 93 89 94 return 0; 90 95 } ··· 92 97 93 98 unsigned int swiotlb_max_segment(void) 94 99 { 95 - return io_tlb_default_mem.nslabs ? max_segment : 0; 100 + if (!io_tlb_default_mem.nslabs) 101 + return 0; 102 + return rounddown(io_tlb_default_mem.nslabs << IO_TLB_SHIFT, PAGE_SIZE); 96 103 } 97 104 EXPORT_SYMBOL_GPL(swiotlb_max_segment); 98 - 99 - void swiotlb_set_max_segment(unsigned int val) 100 - { 101 - if (swiotlb_force == SWIOTLB_FORCE) 102 - max_segment = 1; 103 - else 104 - max_segment = rounddown(val, PAGE_SIZE); 105 - } 106 105 107 106 unsigned long swiotlb_size_or_default(void) 108 107 { ··· 203 214 mem->index = 0; 204 215 mem->late_alloc = late_alloc; 205 216 206 - if (swiotlb_force == SWIOTLB_FORCE) 217 + if (swiotlb_force_bounce) 207 218 mem->force_bounce = true; 208 219 209 220 spin_lock_init(&mem->lock); ··· 225 236 return; 226 237 } 227 238 228 - int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose) 239 + /* 240 + * Statically reserve bounce buffer space and initialize bounce buffer data 241 + * structures for the software IO TLB used to implement the DMA API. 242 + */ 243 + void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags, 244 + int (*remap)(void *tlb, unsigned long nslabs)) 229 245 { 230 246 struct io_tlb_mem *mem = &io_tlb_default_mem; 247 + unsigned long nslabs = default_nslabs; 231 248 size_t alloc_size; 249 + size_t bytes; 250 + void *tlb; 232 251 233 - if (swiotlb_force == SWIOTLB_NO_FORCE) 234 - return 0; 252 + if (!addressing_limit && !swiotlb_force_bounce) 253 + return; 254 + if (swiotlb_force_disable) 255 + return; 235 256 236 - /* protect against double initialization */ 237 - if (WARN_ON_ONCE(mem->nslabs)) 238 - return -ENOMEM; 257 + /* 258 + * By default allocate the bounce buffer memory from low memory, but 259 + * allow to pick a location everywhere for hypervisors with guest 260 + * memory encryption. 261 + */ 262 + retry: 263 + bytes = PAGE_ALIGN(nslabs << IO_TLB_SHIFT); 264 + if (flags & SWIOTLB_ANY) 265 + tlb = memblock_alloc(bytes, PAGE_SIZE); 266 + else 267 + tlb = memblock_alloc_low(bytes, PAGE_SIZE); 268 + if (!tlb) { 269 + pr_warn("%s: failed to allocate tlb structure\n", __func__); 270 + return; 271 + } 272 + 273 + if (remap && remap(tlb, nslabs) < 0) { 274 + memblock_free(tlb, PAGE_ALIGN(bytes)); 275 + 276 + nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE); 277 + if (nslabs < IO_TLB_MIN_SLABS) 278 + panic("%s: Failed to remap %zu bytes\n", 279 + __func__, bytes); 280 + goto retry; 281 + } 239 282 240 283 alloc_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), nslabs)); 241 284 mem->slots = memblock_alloc(alloc_size, PAGE_SIZE); ··· 276 255 __func__, alloc_size, PAGE_SIZE); 277 256 278 257 swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false); 258 + mem->force_bounce = flags & SWIOTLB_FORCE; 279 259 280 - if (verbose) 260 + if (flags & SWIOTLB_VERBOSE) 281 261 swiotlb_print_info(); 282 - swiotlb_set_max_segment(mem->nslabs << IO_TLB_SHIFT); 283 - return 0; 284 262 } 285 263 286 - /* 287 - * Statically reserve bounce buffer space and initialize bounce buffer data 288 - * structures for the software IO TLB used to implement the DMA API. 289 - */ 290 - void __init 291 - swiotlb_init(int verbose) 264 + void __init swiotlb_init(bool addressing_limit, unsigned int flags) 292 265 { 293 - size_t bytes = PAGE_ALIGN(default_nslabs << IO_TLB_SHIFT); 294 - void *tlb; 295 - 296 - if (swiotlb_force == SWIOTLB_NO_FORCE) 297 - return; 298 - 299 - /* Get IO TLB memory from the low pages */ 300 - tlb = memblock_alloc_low(bytes, PAGE_SIZE); 301 - if (!tlb) 302 - goto fail; 303 - if (swiotlb_init_with_tbl(tlb, default_nslabs, verbose)) 304 - goto fail_free_mem; 305 - return; 306 - 307 - fail_free_mem: 308 - memblock_free(tlb, bytes); 309 - fail: 310 - pr_warn("Cannot allocate buffer"); 266 + return swiotlb_init_remap(addressing_limit, flags, NULL); 311 267 } 312 268 313 269 /* ··· 292 294 * initialize the swiotlb later using the slab allocator if needed. 293 295 * This should be just like above, but with some error catching. 294 296 */ 295 - int 296 - swiotlb_late_init_with_default_size(size_t default_size) 297 + int swiotlb_init_late(size_t size, gfp_t gfp_mask, 298 + int (*remap)(void *tlb, unsigned long nslabs)) 297 299 { 298 - unsigned long nslabs = 299 - ALIGN(default_size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE); 300 - unsigned long bytes; 300 + struct io_tlb_mem *mem = &io_tlb_default_mem; 301 + unsigned long nslabs = ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE); 301 302 unsigned char *vstart = NULL; 302 303 unsigned int order; 304 + bool retried = false; 303 305 int rc = 0; 304 306 305 - if (swiotlb_force == SWIOTLB_NO_FORCE) 307 + if (swiotlb_force_disable) 306 308 return 0; 307 309 308 - /* 309 - * Get IO TLB memory from the low pages 310 - */ 310 + retry: 311 311 order = get_order(nslabs << IO_TLB_SHIFT); 312 312 nslabs = SLABS_PER_PAGE << order; 313 - bytes = nslabs << IO_TLB_SHIFT; 314 313 315 314 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { 316 - vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, 315 + vstart = (void *)__get_free_pages(gfp_mask | __GFP_NOWARN, 317 316 order); 318 317 if (vstart) 319 318 break; 320 319 order--; 320 + nslabs = SLABS_PER_PAGE << order; 321 + retried = true; 321 322 } 322 323 323 324 if (!vstart) 324 325 return -ENOMEM; 325 326 326 - if (order != get_order(bytes)) { 327 - pr_warn("only able to allocate %ld MB\n", 328 - (PAGE_SIZE << order) >> 20); 329 - nslabs = SLABS_PER_PAGE << order; 330 - } 331 - rc = swiotlb_late_init_with_tbl(vstart, nslabs); 332 - if (rc) 327 + if (remap) 328 + rc = remap(vstart, nslabs); 329 + if (rc) { 333 330 free_pages((unsigned long)vstart, order); 334 331 335 - return rc; 336 - } 332 + nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE); 333 + if (nslabs < IO_TLB_MIN_SLABS) 334 + return rc; 335 + retried = true; 336 + goto retry; 337 + } 337 338 338 - int 339 - swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) 340 - { 341 - struct io_tlb_mem *mem = &io_tlb_default_mem; 342 - unsigned long bytes = nslabs << IO_TLB_SHIFT; 343 - 344 - if (swiotlb_force == SWIOTLB_NO_FORCE) 345 - return 0; 346 - 347 - /* protect against double initialization */ 348 - if (WARN_ON_ONCE(mem->nslabs)) 349 - return -ENOMEM; 339 + if (retried) { 340 + pr_warn("only able to allocate %ld MB\n", 341 + (PAGE_SIZE << order) >> 20); 342 + } 350 343 351 344 mem->slots = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 352 345 get_order(array_size(sizeof(*mem->slots), nslabs))); 353 - if (!mem->slots) 346 + if (!mem->slots) { 347 + free_pages((unsigned long)vstart, order); 354 348 return -ENOMEM; 349 + } 355 350 356 - set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT); 357 - swiotlb_init_io_tlb_mem(mem, virt_to_phys(tlb), nslabs, true); 351 + set_memory_decrypted((unsigned long)vstart, 352 + (nslabs << IO_TLB_SHIFT) >> PAGE_SHIFT); 353 + swiotlb_init_io_tlb_mem(mem, virt_to_phys(vstart), nslabs, true); 358 354 359 355 swiotlb_print_info(); 360 - swiotlb_set_max_segment(mem->nslabs << IO_TLB_SHIFT); 361 356 return 0; 362 357 } 363 358 ··· 359 368 struct io_tlb_mem *mem = &io_tlb_default_mem; 360 369 unsigned long tbl_vaddr; 361 370 size_t tbl_size, slots_size; 371 + 372 + if (swiotlb_force_bounce) 373 + return; 362 374 363 375 if (!mem->nslabs) 364 376 return; ··· 711 717 phys_addr_t swiotlb_addr; 712 718 dma_addr_t dma_addr; 713 719 714 - trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size, 715 - swiotlb_force); 720 + trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size); 716 721 717 722 swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, 0, dir, 718 723 attrs); ··· 736 743 737 744 size_t swiotlb_max_mapping_size(struct device *dev) 738 745 { 739 - return ((size_t)IO_TLB_SIZE) * IO_TLB_SEGSIZE; 746 + int min_align_mask = dma_get_min_align_mask(dev); 747 + int min_align = 0; 748 + 749 + /* 750 + * swiotlb_find_slots() skips slots according to 751 + * min align mask. This affects max mapping size. 752 + * Take it into acount here. 753 + */ 754 + if (min_align_mask) 755 + min_align = roundup(min_align_mask, IO_TLB_SIZE); 756 + 757 + return ((size_t)IO_TLB_SIZE) * IO_TLB_SEGSIZE - min_align; 740 758 } 741 759 742 760 bool is_swiotlb_active(struct device *dev)