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

dma/direct: Handle the memory encryption bit in common code

Give the basic phys_to_dma() and dma_to_phys() helpers a __-prefix and add
the memory encryption mask to the non-prefixed versions. Use the
__-prefixed versions directly instead of clearing the mask again in
various places.

Tested-by: Tom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Joerg Roedel <joro@8bytes.org>
Cc: Jon Mason <jdmason@kudzu.us>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Muli Ben-Yehuda <mulix@mulix.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: iommu@lists.linux-foundation.org
Link: http://lkml.kernel.org/r/20180319103826.12853-13-hch@lst.de
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Christoph Hellwig and committed by
Ingo Molnar
b6e05477 e7de6c7c

+53 -64
+2 -2
arch/arm/include/asm/dma-direct.h
··· 2 2 #ifndef ASM_ARM_DMA_DIRECT_H 3 3 #define ASM_ARM_DMA_DIRECT_H 1 4 4 5 - static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 5 + static inline dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) 6 6 { 7 7 unsigned int offset = paddr & ~PAGE_MASK; 8 8 return pfn_to_dma(dev, __phys_to_pfn(paddr)) + offset; 9 9 } 10 10 11 - static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr) 11 + static inline phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t dev_addr) 12 12 { 13 13 unsigned int offset = dev_addr & ~PAGE_MASK; 14 14 return __pfn_to_phys(dma_to_pfn(dev, dev_addr)) + offset;
+5 -5
arch/mips/cavium-octeon/dma-octeon.c
··· 10 10 * IP32 changes by Ilya. 11 11 * Copyright (C) 2010 Cavium Networks, Inc. 12 12 */ 13 - #include <linux/dma-mapping.h> 13 + #include <linux/dma-direct.h> 14 14 #include <linux/scatterlist.h> 15 15 #include <linux/bootmem.h> 16 16 #include <linux/export.h> ··· 182 182 phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr); 183 183 }; 184 184 185 - dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 185 + dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) 186 186 { 187 187 struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev), 188 188 struct octeon_dma_map_ops, ··· 190 190 191 191 return ops->phys_to_dma(dev, paddr); 192 192 } 193 - EXPORT_SYMBOL(phys_to_dma); 193 + EXPORT_SYMBOL(__phys_to_dma); 194 194 195 - phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) 195 + phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr) 196 196 { 197 197 struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev), 198 198 struct octeon_dma_map_ops, ··· 200 200 201 201 return ops->dma_to_phys(dev, daddr); 202 202 } 203 - EXPORT_SYMBOL(dma_to_phys); 203 + EXPORT_SYMBOL(__dma_to_phys); 204 204 205 205 static struct octeon_dma_map_ops octeon_linear_dma_map_ops = { 206 206 .dma_map_ops = {
+2 -2
arch/mips/include/asm/mach-cavium-octeon/dma-coherence.h
··· 69 69 return addr + size - 1 <= *dev->dma_mask; 70 70 } 71 71 72 - dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr); 73 - phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr); 72 + dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr); 73 + phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr); 74 74 75 75 struct dma_map_ops; 76 76 extern const struct dma_map_ops *octeon_pci_dma_map_ops;
+5 -5
arch/mips/include/asm/mach-loongson64/dma-coherence.h
··· 25 25 return addr + size - 1 <= *dev->dma_mask; 26 26 } 27 27 28 - extern dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr); 29 - extern phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr); 28 + extern dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr); 29 + extern phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr); 30 30 static inline dma_addr_t plat_map_dma_mem(struct device *dev, void *addr, 31 31 size_t size) 32 32 { 33 33 #ifdef CONFIG_CPU_LOONGSON3 34 - return phys_to_dma(dev, virt_to_phys(addr)); 34 + return __phys_to_dma(dev, virt_to_phys(addr)); 35 35 #else 36 36 return virt_to_phys(addr) | 0x80000000; 37 37 #endif ··· 41 41 struct page *page) 42 42 { 43 43 #ifdef CONFIG_CPU_LOONGSON3 44 - return phys_to_dma(dev, page_to_phys(page)); 44 + return __phys_to_dma(dev, page_to_phys(page)); 45 45 #else 46 46 return page_to_phys(page) | 0x80000000; 47 47 #endif ··· 51 51 dma_addr_t dma_addr) 52 52 { 53 53 #if defined(CONFIG_CPU_LOONGSON3) && defined(CONFIG_64BIT) 54 - return dma_to_phys(dev, dma_addr); 54 + return __dma_to_phys(dev, dma_addr); 55 55 #elif defined(CONFIG_CPU_LOONGSON2F) && defined(CONFIG_64BIT) 56 56 return (dma_addr > 0x8fffffff) ? dma_addr : (dma_addr & 0x0fffffff); 57 57 #else
+2 -2
arch/mips/loongson64/common/dma-swiotlb.c
··· 63 63 return swiotlb_dma_supported(dev, mask); 64 64 } 65 65 66 - dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 66 + dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) 67 67 { 68 68 long nid; 69 69 #ifdef CONFIG_PHYS48_TO_HT40 ··· 75 75 return paddr; 76 76 } 77 77 78 - phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) 78 + phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr) 79 79 { 80 80 long nid; 81 81 #ifdef CONFIG_PHYS48_TO_HT40
+2 -2
arch/powerpc/include/asm/dma-direct.h
··· 17 17 return addr + size - 1 <= *dev->dma_mask; 18 18 } 19 19 20 - static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 20 + static inline dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) 21 21 { 22 22 return paddr + get_dma_offset(dev); 23 23 } 24 24 25 - static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) 25 + static inline phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr) 26 26 { 27 27 return daddr - get_dma_offset(dev); 28 28 }
+1 -1
arch/x86/Kconfig
··· 54 54 select ARCH_HAS_FORTIFY_SOURCE 55 55 select ARCH_HAS_GCOV_PROFILE_ALL 56 56 select ARCH_HAS_KCOV if X86_64 57 - select ARCH_HAS_PHYS_TO_DMA 58 57 select ARCH_HAS_MEMBARRIER_SYNC_CORE 59 58 select ARCH_HAS_PMEM_API if X86_64 60 59 select ARCH_HAS_REFCOUNT ··· 691 692 config STA2X11 692 693 bool "STA2X11 Companion Chip Support" 693 694 depends on X86_32_NON_STANDARD && PCI 695 + select ARCH_HAS_PHYS_TO_DMA 694 696 select X86_DEV_DMA_OPS 695 697 select X86_DMA_REMAP 696 698 select SWIOTLB
+2 -23
arch/x86/include/asm/dma-direct.h
··· 2 2 #ifndef ASM_X86_DMA_DIRECT_H 3 3 #define ASM_X86_DMA_DIRECT_H 1 4 4 5 - #include <linux/mem_encrypt.h> 6 - 7 - #ifdef CONFIG_X86_DMA_REMAP /* Platform code defines bridge-specific code */ 8 5 bool dma_capable(struct device *dev, dma_addr_t addr, size_t size); 9 - dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr); 10 - phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr); 11 - #else 12 - static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) 13 - { 14 - if (!dev->dma_mask) 15 - return 0; 6 + dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr); 7 + phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr); 16 8 17 - return addr + size - 1 <= *dev->dma_mask; 18 - } 19 - 20 - static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 21 - { 22 - return __sme_set(paddr); 23 - } 24 - 25 - static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) 26 - { 27 - return __sme_clr(daddr); 28 - } 29 - #endif /* CONFIG_X86_DMA_REMAP */ 30 9 #endif /* ASM_X86_DMA_DIRECT_H */
+1 -1
arch/x86/mm/mem_encrypt.c
··· 211 211 * Since we will be clearing the encryption bit, check the 212 212 * mask with it already cleared. 213 213 */ 214 - addr = __sme_clr(phys_to_dma(dev, page_to_phys(page))); 214 + addr = __phys_to_dma(dev, page_to_phys(page)); 215 215 if ((addr + size) > dev->coherent_dma_mask) { 216 216 __free_pages(page, get_order(size)); 217 217 } else {
+3 -3
arch/x86/pci/sta2x11-fixup.c
··· 207 207 } 208 208 209 209 /** 210 - * phys_to_dma - Return the DMA AMBA address used for this STA2x11 device 210 + * __phys_to_dma - Return the DMA AMBA address used for this STA2x11 device 211 211 * @dev: device for a PCI device 212 212 * @paddr: Physical address 213 213 */ 214 - dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 214 + dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) 215 215 { 216 216 if (!dev->archdata.is_sta2x11) 217 217 return paddr; ··· 223 223 * @dev: device for a PCI device 224 224 * @daddr: STA2x11 AMBA DMA address 225 225 */ 226 - phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) 226 + phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr) 227 227 { 228 228 if (!dev->archdata.is_sta2x11) 229 229 return daddr;
+19 -2
include/linux/dma-direct.h
··· 3 3 #define _LINUX_DMA_DIRECT_H 1 4 4 5 5 #include <linux/dma-mapping.h> 6 + #include <linux/mem_encrypt.h> 6 7 7 8 #ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA 8 9 #include <asm/dma-direct.h> 9 10 #else 10 - static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 11 + static inline dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) 11 12 { 12 13 dma_addr_t dev_addr = (dma_addr_t)paddr; 13 14 14 15 return dev_addr - ((dma_addr_t)dev->dma_pfn_offset << PAGE_SHIFT); 15 16 } 16 17 17 - static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr) 18 + static inline phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t dev_addr) 18 19 { 19 20 phys_addr_t paddr = (phys_addr_t)dev_addr; 20 21 ··· 30 29 return addr + size - 1 <= *dev->dma_mask; 31 30 } 32 31 #endif /* !CONFIG_ARCH_HAS_PHYS_TO_DMA */ 32 + 33 + /* 34 + * If memory encryption is supported, phys_to_dma will set the memory encryption 35 + * bit in the DMA address, and dma_to_phys will clear it. The raw __phys_to_dma 36 + * and __dma_to_phys versions should only be used on non-encrypted memory for 37 + * special occasions like DMA coherent buffers. 38 + */ 39 + static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 40 + { 41 + return __sme_set(__phys_to_dma(dev, paddr)); 42 + } 43 + 44 + static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) 45 + { 46 + return __sme_clr(__dma_to_phys(dev, daddr)); 47 + } 33 48 34 49 #ifdef CONFIG_ARCH_HAS_DMA_MARK_CLEAN 35 50 void dma_mark_clean(void *addr, size_t size);
+9 -16
lib/swiotlb.c
··· 157 157 return size ? size : (IO_TLB_DEFAULT_SIZE); 158 158 } 159 159 160 - /* For swiotlb, clear memory encryption mask from dma addresses */ 161 - static dma_addr_t swiotlb_phys_to_dma(struct device *hwdev, 162 - phys_addr_t address) 163 - { 164 - return __sme_clr(phys_to_dma(hwdev, address)); 165 - } 166 - 167 160 /* Note that this doesn't work with highmem page */ 168 161 static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev, 169 162 volatile void *address) ··· 615 622 return SWIOTLB_MAP_ERROR; 616 623 } 617 624 618 - start_dma_addr = swiotlb_phys_to_dma(hwdev, io_tlb_start); 625 + start_dma_addr = __phys_to_dma(hwdev, io_tlb_start); 619 626 return swiotlb_tbl_map_single(hwdev, start_dma_addr, phys, size, 620 627 dir, attrs); 621 628 } ··· 719 726 goto out_warn; 720 727 721 728 phys_addr = swiotlb_tbl_map_single(dev, 722 - swiotlb_phys_to_dma(dev, io_tlb_start), 729 + __phys_to_dma(dev, io_tlb_start), 723 730 0, size, DMA_FROM_DEVICE, 0); 724 731 if (phys_addr == SWIOTLB_MAP_ERROR) 725 732 goto out_warn; 726 733 727 - *dma_handle = swiotlb_phys_to_dma(dev, phys_addr); 734 + *dma_handle = __phys_to_dma(dev, phys_addr); 728 735 if (dma_coherent_ok(dev, *dma_handle, size)) 729 736 goto out_unmap; 730 737 ··· 860 867 map = map_single(dev, phys, size, dir, attrs); 861 868 if (map == SWIOTLB_MAP_ERROR) { 862 869 swiotlb_full(dev, size, dir, 1); 863 - return swiotlb_phys_to_dma(dev, io_tlb_overflow_buffer); 870 + return __phys_to_dma(dev, io_tlb_overflow_buffer); 864 871 } 865 872 866 - dev_addr = swiotlb_phys_to_dma(dev, map); 873 + dev_addr = __phys_to_dma(dev, map); 867 874 868 875 /* Ensure that the address returned is DMA'ble */ 869 876 if (dma_capable(dev, dev_addr, size)) ··· 872 879 attrs |= DMA_ATTR_SKIP_CPU_SYNC; 873 880 swiotlb_tbl_unmap_single(dev, map, size, dir, attrs); 874 881 875 - return swiotlb_phys_to_dma(dev, io_tlb_overflow_buffer); 882 + return __phys_to_dma(dev, io_tlb_overflow_buffer); 876 883 } 877 884 878 885 /* ··· 1002 1009 sg_dma_len(sgl) = 0; 1003 1010 return 0; 1004 1011 } 1005 - sg->dma_address = swiotlb_phys_to_dma(hwdev, map); 1012 + sg->dma_address = __phys_to_dma(hwdev, map); 1006 1013 } else 1007 1014 sg->dma_address = dev_addr; 1008 1015 sg_dma_len(sg) = sg->length; ··· 1066 1073 int 1067 1074 swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr) 1068 1075 { 1069 - return (dma_addr == swiotlb_phys_to_dma(hwdev, io_tlb_overflow_buffer)); 1076 + return (dma_addr == __phys_to_dma(hwdev, io_tlb_overflow_buffer)); 1070 1077 } 1071 1078 1072 1079 /* ··· 1078 1085 int 1079 1086 swiotlb_dma_supported(struct device *hwdev, u64 mask) 1080 1087 { 1081 - return swiotlb_phys_to_dma(hwdev, io_tlb_end - 1) <= mask; 1088 + return __phys_to_dma(hwdev, io_tlb_end - 1) <= mask; 1082 1089 } 1083 1090 1084 1091 #ifdef CONFIG_DMA_DIRECT_OPS