1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 2 /* 3 * Internals of the DMA direct mapping impleme 3 * Internals of the DMA direct mapping implementation. Only for use by the 4 * DMA mapping code and IOMMU drivers. 4 * DMA mapping code and IOMMU drivers. 5 */ 5 */ 6 #ifndef _LINUX_DMA_DIRECT_H 6 #ifndef _LINUX_DMA_DIRECT_H 7 #define _LINUX_DMA_DIRECT_H 1 7 #define _LINUX_DMA_DIRECT_H 1 8 8 9 #include <linux/dma-mapping.h> 9 #include <linux/dma-mapping.h> 10 #include <linux/dma-map-ops.h> 10 #include <linux/dma-map-ops.h> 11 #include <linux/memblock.h> /* for min_low_pfn 11 #include <linux/memblock.h> /* for min_low_pfn */ 12 #include <linux/mem_encrypt.h> 12 #include <linux/mem_encrypt.h> 13 #include <linux/swiotlb.h> 13 #include <linux/swiotlb.h> 14 14 15 extern unsigned int zone_dma_bits; 15 extern unsigned int zone_dma_bits; 16 16 17 /* 17 /* 18 * Record the mapping of CPU physical to DMA a 18 * Record the mapping of CPU physical to DMA addresses for a given region. 19 */ 19 */ 20 struct bus_dma_region { 20 struct bus_dma_region { 21 phys_addr_t cpu_start; 21 phys_addr_t cpu_start; 22 dma_addr_t dma_start; 22 dma_addr_t dma_start; 23 u64 size; 23 u64 size; >> 24 u64 offset; 24 }; 25 }; 25 26 26 static inline dma_addr_t translate_phys_to_dma 27 static inline dma_addr_t translate_phys_to_dma(struct device *dev, 27 phys_addr_t paddr) 28 phys_addr_t paddr) 28 { 29 { 29 const struct bus_dma_region *m; 30 const struct bus_dma_region *m; 30 31 31 for (m = dev->dma_range_map; m->size; !! 32 for (m = dev->dma_range_map; m->size; m++) 32 u64 offset = paddr - m->cpu_st !! 33 if (paddr >= m->cpu_start && paddr - m->cpu_start < m->size) 33 !! 34 return (dma_addr_t)paddr - m->offset; 34 if (paddr >= m->cpu_start && o << 35 return m->dma_start + << 36 } << 37 35 38 /* make sure dma_capable fails when no 36 /* make sure dma_capable fails when no translation is available */ 39 return DMA_MAPPING_ERROR; 37 return DMA_MAPPING_ERROR; 40 } 38 } 41 39 42 static inline phys_addr_t translate_dma_to_phy 40 static inline phys_addr_t translate_dma_to_phys(struct device *dev, 43 dma_addr_t dma_addr) 41 dma_addr_t dma_addr) 44 { 42 { 45 const struct bus_dma_region *m; 43 const struct bus_dma_region *m; 46 44 47 for (m = dev->dma_range_map; m->size; !! 45 for (m = dev->dma_range_map; m->size; m++) 48 u64 offset = dma_addr - m->dma !! 46 if (dma_addr >= m->dma_start && dma_addr - m->dma_start < m->size) 49 !! 47 return (phys_addr_t)dma_addr + m->offset; 50 if (dma_addr >= m->dma_start & << 51 return m->cpu_start + << 52 } << 53 48 54 return (phys_addr_t)-1; 49 return (phys_addr_t)-1; 55 } << 56 << 57 static inline dma_addr_t dma_range_map_min(con << 58 { << 59 dma_addr_t ret = (dma_addr_t)U64_MAX; << 60 << 61 for (; map->size; map++) << 62 ret = min(ret, map->dma_start) << 63 return ret; << 64 } << 65 << 66 static inline dma_addr_t dma_range_map_max(con << 67 { << 68 dma_addr_t ret = 0; << 69 << 70 for (; map->size; map++) << 71 ret = max(ret, map->dma_start << 72 return ret; << 73 } 50 } 74 51 75 #ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA 52 #ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA 76 #include <asm/dma-direct.h> 53 #include <asm/dma-direct.h> 77 #ifndef phys_to_dma_unencrypted 54 #ifndef phys_to_dma_unencrypted 78 #define phys_to_dma_unencrypted phys_t 55 #define phys_to_dma_unencrypted phys_to_dma 79 #endif 56 #endif 80 #else 57 #else 81 static inline dma_addr_t phys_to_dma_unencrypt 58 static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev, 82 phys_addr_t paddr) 59 phys_addr_t paddr) 83 { 60 { 84 if (dev->dma_range_map) 61 if (dev->dma_range_map) 85 return translate_phys_to_dma(d 62 return translate_phys_to_dma(dev, paddr); 86 return paddr; 63 return paddr; 87 } 64 } 88 65 89 /* 66 /* 90 * If memory encryption is supported, phys_to_ 67 * If memory encryption is supported, phys_to_dma will set the memory encryption 91 * bit in the DMA address, and dma_to_phys wil 68 * bit in the DMA address, and dma_to_phys will clear it. 92 * phys_to_dma_unencrypted is for use on speci 69 * phys_to_dma_unencrypted is for use on special unencrypted memory like swiotlb 93 * buffers. 70 * buffers. 94 */ 71 */ 95 static inline dma_addr_t phys_to_dma(struct de 72 static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) 96 { 73 { 97 return __sme_set(phys_to_dma_unencrypt 74 return __sme_set(phys_to_dma_unencrypted(dev, paddr)); 98 } 75 } 99 76 100 static inline phys_addr_t dma_to_phys(struct d 77 static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dma_addr) 101 { 78 { 102 phys_addr_t paddr; 79 phys_addr_t paddr; 103 80 104 if (dev->dma_range_map) 81 if (dev->dma_range_map) 105 paddr = translate_dma_to_phys( 82 paddr = translate_dma_to_phys(dev, dma_addr); 106 else 83 else 107 paddr = dma_addr; 84 paddr = dma_addr; 108 85 109 return __sme_clr(paddr); 86 return __sme_clr(paddr); 110 } 87 } 111 #endif /* !CONFIG_ARCH_HAS_PHYS_TO_DMA */ 88 #endif /* !CONFIG_ARCH_HAS_PHYS_TO_DMA */ 112 89 113 #ifdef CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED 90 #ifdef CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED 114 bool force_dma_unencrypted(struct device *dev) 91 bool force_dma_unencrypted(struct device *dev); 115 #else 92 #else 116 static inline bool force_dma_unencrypted(struc 93 static inline bool force_dma_unencrypted(struct device *dev) 117 { 94 { 118 return false; 95 return false; 119 } 96 } 120 #endif /* CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTE 97 #endif /* CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED */ 121 98 122 static inline bool dma_capable(struct device * 99 static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size, 123 bool is_ram) 100 bool is_ram) 124 { 101 { 125 dma_addr_t end = addr + size - 1; 102 dma_addr_t end = addr + size - 1; 126 103 127 if (addr == DMA_MAPPING_ERROR) 104 if (addr == DMA_MAPPING_ERROR) 128 return false; 105 return false; 129 if (is_ram && !IS_ENABLED(CONFIG_ARCH_ 106 if (is_ram && !IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) && 130 min(addr, end) < phys_to_dma(dev, 107 min(addr, end) < phys_to_dma(dev, PFN_PHYS(min_low_pfn))) 131 return false; 108 return false; 132 109 133 return end <= min_not_zero(*dev->dma_m 110 return end <= min_not_zero(*dev->dma_mask, dev->bus_dma_limit); 134 } 111 } 135 112 136 u64 dma_direct_get_required_mask(struct device 113 u64 dma_direct_get_required_mask(struct device *dev); 137 void *dma_direct_alloc(struct device *dev, siz 114 void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, 138 gfp_t gfp, unsigned long attrs 115 gfp_t gfp, unsigned long attrs); 139 void dma_direct_free(struct device *dev, size_ 116 void dma_direct_free(struct device *dev, size_t size, void *cpu_addr, 140 dma_addr_t dma_addr, unsigned 117 dma_addr_t dma_addr, unsigned long attrs); 141 struct page *dma_direct_alloc_pages(struct dev 118 struct page *dma_direct_alloc_pages(struct device *dev, size_t size, 142 dma_addr_t *dma_handle, enum d 119 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp); 143 void dma_direct_free_pages(struct device *dev, 120 void dma_direct_free_pages(struct device *dev, size_t size, 144 struct page *page, dma_addr_t 121 struct page *page, dma_addr_t dma_addr, 145 enum dma_data_direction dir); 122 enum dma_data_direction dir); 146 int dma_direct_supported(struct device *dev, u 123 int dma_direct_supported(struct device *dev, u64 mask); 147 dma_addr_t dma_direct_map_resource(struct devi 124 dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr, 148 size_t size, enum dma_data_dir 125 size_t size, enum dma_data_direction dir, unsigned long attrs); 149 126 150 #endif /* _LINUX_DMA_DIRECT_H */ 127 #endif /* _LINUX_DMA_DIRECT_H */ 151 128
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.