.. | .. |
---|
1 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
---|
| 2 | +/* |
---|
| 3 | + * Internals of the DMA direct mapping implementation. Only for use by the |
---|
| 4 | + * DMA mapping code and IOMMU drivers. |
---|
| 5 | + */ |
---|
2 | 6 | #ifndef _LINUX_DMA_DIRECT_H |
---|
3 | 7 | #define _LINUX_DMA_DIRECT_H 1 |
---|
4 | 8 | |
---|
5 | 9 | #include <linux/dma-mapping.h> |
---|
| 10 | +#include <linux/dma-map-ops.h> |
---|
| 11 | +#include <linux/memblock.h> /* for min_low_pfn */ |
---|
6 | 12 | #include <linux/mem_encrypt.h> |
---|
| 13 | +#include <linux/swiotlb.h> |
---|
| 14 | + |
---|
| 15 | +extern unsigned int zone_dma_bits; |
---|
| 16 | + |
---|
| 17 | +/* |
---|
| 18 | + * Record the mapping of CPU physical to DMA addresses for a given region. |
---|
| 19 | + */ |
---|
| 20 | +struct bus_dma_region { |
---|
| 21 | + phys_addr_t cpu_start; |
---|
| 22 | + dma_addr_t dma_start; |
---|
| 23 | + u64 size; |
---|
| 24 | + u64 offset; |
---|
| 25 | +}; |
---|
| 26 | + |
---|
| 27 | +static inline bool zone_dma32_is_empty(int node) |
---|
| 28 | +{ |
---|
| 29 | +#ifdef CONFIG_ZONE_DMA32 |
---|
| 30 | + pg_data_t *pgdat = NODE_DATA(node); |
---|
| 31 | + |
---|
| 32 | + return zone_is_empty(&pgdat->node_zones[ZONE_DMA32]); |
---|
| 33 | +#else |
---|
| 34 | + return true; |
---|
| 35 | +#endif |
---|
| 36 | +} |
---|
| 37 | + |
---|
| 38 | +static inline bool zone_dma32_are_empty(void) |
---|
| 39 | +{ |
---|
| 40 | +#ifdef CONFIG_NUMA |
---|
| 41 | + int node; |
---|
| 42 | + |
---|
| 43 | + for_each_node(node) |
---|
| 44 | + if (!zone_dma32_is_empty(node)) |
---|
| 45 | + return false; |
---|
| 46 | +#else |
---|
| 47 | + if (!zone_dma32_is_empty(numa_node_id())) |
---|
| 48 | + return false; |
---|
| 49 | +#endif |
---|
| 50 | + |
---|
| 51 | + return true; |
---|
| 52 | +} |
---|
| 53 | + |
---|
| 54 | +static inline dma_addr_t translate_phys_to_dma(struct device *dev, |
---|
| 55 | + phys_addr_t paddr) |
---|
| 56 | +{ |
---|
| 57 | + const struct bus_dma_region *m; |
---|
| 58 | + |
---|
| 59 | + for (m = dev->dma_range_map; m->size; m++) |
---|
| 60 | + if (paddr >= m->cpu_start && paddr - m->cpu_start < m->size) |
---|
| 61 | + return (dma_addr_t)paddr - m->offset; |
---|
| 62 | + |
---|
| 63 | + /* make sure dma_capable fails when no translation is available */ |
---|
| 64 | + return DMA_MAPPING_ERROR; |
---|
| 65 | +} |
---|
| 66 | + |
---|
| 67 | +static inline phys_addr_t translate_dma_to_phys(struct device *dev, |
---|
| 68 | + dma_addr_t dma_addr) |
---|
| 69 | +{ |
---|
| 70 | + const struct bus_dma_region *m; |
---|
| 71 | + |
---|
| 72 | + for (m = dev->dma_range_map; m->size; m++) |
---|
| 73 | + if (dma_addr >= m->dma_start && dma_addr - m->dma_start < m->size) |
---|
| 74 | + return (phys_addr_t)dma_addr + m->offset; |
---|
| 75 | + |
---|
| 76 | + return (phys_addr_t)-1; |
---|
| 77 | +} |
---|
7 | 78 | |
---|
8 | 79 | #ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA |
---|
9 | 80 | #include <asm/dma-direct.h> |
---|
| 81 | +#ifndef phys_to_dma_unencrypted |
---|
| 82 | +#define phys_to_dma_unencrypted phys_to_dma |
---|
| 83 | +#endif |
---|
10 | 84 | #else |
---|
11 | | -static inline dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) |
---|
| 85 | +static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev, |
---|
| 86 | + phys_addr_t paddr) |
---|
12 | 87 | { |
---|
13 | | - dma_addr_t dev_addr = (dma_addr_t)paddr; |
---|
14 | | - |
---|
15 | | - return dev_addr - ((dma_addr_t)dev->dma_pfn_offset << PAGE_SHIFT); |
---|
| 88 | + if (dev->dma_range_map) |
---|
| 89 | + return translate_phys_to_dma(dev, paddr); |
---|
| 90 | + return paddr; |
---|
16 | 91 | } |
---|
17 | | - |
---|
18 | | -static inline phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t dev_addr) |
---|
19 | | -{ |
---|
20 | | - phys_addr_t paddr = (phys_addr_t)dev_addr; |
---|
21 | | - |
---|
22 | | - return paddr + ((phys_addr_t)dev->dma_pfn_offset << PAGE_SHIFT); |
---|
23 | | -} |
---|
24 | | - |
---|
25 | | -static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) |
---|
26 | | -{ |
---|
27 | | - if (!dev->dma_mask) |
---|
28 | | - return false; |
---|
29 | | - |
---|
30 | | - return addr + size - 1 <= *dev->dma_mask; |
---|
31 | | -} |
---|
32 | | -#endif /* !CONFIG_ARCH_HAS_PHYS_TO_DMA */ |
---|
33 | 92 | |
---|
34 | 93 | /* |
---|
35 | 94 | * If memory encryption is supported, phys_to_dma will set the memory encryption |
---|
36 | | - * bit in the DMA address, and dma_to_phys will clear it. The raw __phys_to_dma |
---|
37 | | - * and __dma_to_phys versions should only be used on non-encrypted memory for |
---|
38 | | - * special occasions like DMA coherent buffers. |
---|
| 95 | + * bit in the DMA address, and dma_to_phys will clear it. |
---|
| 96 | + * phys_to_dma_unencrypted is for use on special unencrypted memory like swiotlb |
---|
| 97 | + * buffers. |
---|
39 | 98 | */ |
---|
40 | 99 | static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr) |
---|
41 | 100 | { |
---|
42 | | - return __sme_set(__phys_to_dma(dev, paddr)); |
---|
| 101 | + return __sme_set(phys_to_dma_unencrypted(dev, paddr)); |
---|
43 | 102 | } |
---|
44 | 103 | |
---|
45 | | -static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr) |
---|
| 104 | +static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dma_addr) |
---|
46 | 105 | { |
---|
47 | | - return __sme_clr(__dma_to_phys(dev, daddr)); |
---|
48 | | -} |
---|
| 106 | + phys_addr_t paddr; |
---|
49 | 107 | |
---|
50 | | -#ifdef CONFIG_ARCH_HAS_DMA_MARK_CLEAN |
---|
51 | | -void dma_mark_clean(void *addr, size_t size); |
---|
| 108 | + if (dev->dma_range_map) |
---|
| 109 | + paddr = translate_dma_to_phys(dev, dma_addr); |
---|
| 110 | + else |
---|
| 111 | + paddr = dma_addr; |
---|
| 112 | + |
---|
| 113 | + return __sme_clr(paddr); |
---|
| 114 | +} |
---|
| 115 | +#endif /* !CONFIG_ARCH_HAS_PHYS_TO_DMA */ |
---|
| 116 | + |
---|
| 117 | +#ifdef CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED |
---|
| 118 | +bool force_dma_unencrypted(struct device *dev); |
---|
52 | 119 | #else |
---|
53 | | -static inline void dma_mark_clean(void *addr, size_t size) |
---|
| 120 | +static inline bool force_dma_unencrypted(struct device *dev) |
---|
54 | 121 | { |
---|
| 122 | + return false; |
---|
55 | 123 | } |
---|
56 | | -#endif /* CONFIG_ARCH_HAS_DMA_MARK_CLEAN */ |
---|
| 124 | +#endif /* CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED */ |
---|
57 | 125 | |
---|
| 126 | +static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size, |
---|
| 127 | + bool is_ram) |
---|
| 128 | +{ |
---|
| 129 | + dma_addr_t end = addr + size - 1; |
---|
| 130 | + |
---|
| 131 | + if (addr == DMA_MAPPING_ERROR) |
---|
| 132 | + return false; |
---|
| 133 | + if (is_ram && !IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) && |
---|
| 134 | + min(addr, end) < phys_to_dma(dev, PFN_PHYS(min_low_pfn))) |
---|
| 135 | + return false; |
---|
| 136 | + |
---|
| 137 | + return end <= min_not_zero(*dev->dma_mask, dev->bus_dma_limit); |
---|
| 138 | +} |
---|
| 139 | + |
---|
| 140 | +u64 dma_direct_get_required_mask(struct device *dev); |
---|
58 | 141 | void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, |
---|
59 | 142 | gfp_t gfp, unsigned long attrs); |
---|
60 | 143 | void dma_direct_free(struct device *dev, size_t size, void *cpu_addr, |
---|
61 | 144 | dma_addr_t dma_addr, unsigned long attrs); |
---|
62 | | -dma_addr_t dma_direct_map_page(struct device *dev, struct page *page, |
---|
63 | | - unsigned long offset, size_t size, enum dma_data_direction dir, |
---|
64 | | - unsigned long attrs); |
---|
65 | | -int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, |
---|
66 | | - enum dma_data_direction dir, unsigned long attrs); |
---|
| 145 | +struct page *dma_direct_alloc_pages(struct device *dev, size_t size, |
---|
| 146 | + dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp); |
---|
| 147 | +void dma_direct_free_pages(struct device *dev, size_t size, |
---|
| 148 | + struct page *page, dma_addr_t dma_addr, |
---|
| 149 | + enum dma_data_direction dir); |
---|
67 | 150 | int dma_direct_supported(struct device *dev, u64 mask); |
---|
68 | | -int dma_direct_mapping_error(struct device *dev, dma_addr_t dma_addr); |
---|
| 151 | +dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr, |
---|
| 152 | + size_t size, enum dma_data_direction dir, unsigned long attrs); |
---|
| 153 | + |
---|
69 | 154 | #endif /* _LINUX_DMA_DIRECT_H */ |
---|