| 1 |
|
| 2 | |
| 3 | * Internals of the DMA direct mapping implementation. Only for use by the
|
| 4 | * DMA mapping code and IOMMU drivers.
|
| 5 |
|
| 6 | #ifndef _LINUX_DMA_DIRECT_H
|
| 7 | #define _LINUX_DMA_DIRECT_H 1
|
| 8 |
|
| 9 | #include <linux/dma-mapping.h>
|
| 10 | #include <linux/dma-map-ops.h>
|
| 11 | #include <linux/memblock.h>
|
| 12 | #include <linux/mem_encrypt.h>
|
| 13 | #include <linux/swiotlb.h>
|
| 14 |
|
| 15 | extern u64 zone_dma_limit;
|
| 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 | };
|
| 25 |
|
| 26 | static inline dma_addr_t translate_phys_to_dma(struct device *dev,
|
| 27 | phys_addr_t paddr)
|
| 28 | {
|
| 29 | const struct bus_dma_region *m;
|
| 30 |
|
| 31 | for (m = dev->dma_range_map; m->size; m++) {
|
| 32 | u64 offset = paddr - m->cpu_start;
|
| 33 |
|
| 34 | if (paddr >= m->cpu_start && offset < m->size)
|
| 35 | return m->dma_start + offset;
|
| 36 | }
|
| 37 |
|
| 38 |
|
| 39 | return DMA_MAPPING_ERROR;
|
| 40 | }
|
| 41 |
|
| 42 | static inline phys_addr_t translate_dma_to_phys(struct device *dev,
|
| 43 | dma_addr_t dma_addr)
|
| 44 | {
|
| 45 | const struct bus_dma_region *m;
|
| 46 |
|
| 47 | for (m = dev->dma_range_map; m->size; m++) {
|
| 48 | u64 offset = dma_addr - m->dma_start;
|
| 49 |
|
| 50 | if (dma_addr >= m->dma_start && offset < m->size)
|
| 51 | return m->cpu_start + offset;
|
| 52 | }
|
| 53 |
|
| 54 | return (phys_addr_t)-1;
|
| 55 | }
|
| 56 |
|
| 57 | static inline dma_addr_t dma_range_map_min(const struct bus_dma_region *map)
|
| 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(const struct bus_dma_region *map)
|
| 67 | {
|
| 68 | dma_addr_t ret = 0;
|
| 69 |
|
| 70 | for (; map->size; map++)
|
| 71 | ret = max(ret, map->dma_start + map->size - 1);
|
| 72 | return ret;
|
| 73 | }
|
| 74 |
|
| 75 | #ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA
|
| 76 | #include <asm/dma-direct.h>
|
| 77 | #ifndef phys_to_dma_unencrypted
|
| 78 | #define phys_to_dma_unencrypted phys_to_dma
|
| 79 | #endif
|
| 80 |
|
| 81 | #ifndef phys_to_dma_encrypted
|
| 82 | #define phys_to_dma_encrypted phys_to_dma
|
| 83 | #endif
|
| 84 | #else
|
| 85 | static inline dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr)
|
| 86 | {
|
| 87 | if (dev->dma_range_map)
|
| 88 | return translate_phys_to_dma(dev, paddr);
|
| 89 | return paddr;
|
| 90 | }
|
| 91 |
|
| 92 | static inline dma_addr_t phys_to_dma_unencrypted(struct device *dev,
|
| 93 | phys_addr_t paddr)
|
| 94 | {
|
| 95 | return dma_addr_unencrypted(__phys_to_dma(dev, paddr));
|
| 96 | }
|
| 97 |
|
| 98 | static inline dma_addr_t phys_to_dma_encrypted(struct device *dev,
|
| 99 | phys_addr_t paddr)
|
| 100 | {
|
| 101 | return dma_addr_encrypted(__phys_to_dma(dev, paddr));
|
| 102 | }
|
| 103 | |
| 104 | * If memory encryption is supported, phys_to_dma will set the memory encryption
|
| 105 | * bit in the DMA address, and dma_to_phys will clear it.
|
| 106 | * phys_to_dma_unencrypted is for use on special unencrypted memory like swiotlb
|
| 107 | * buffers.
|
| 108 |
|
| 109 | static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
|
| 110 | {
|
| 111 | return dma_addr_encrypted(__phys_to_dma(dev, paddr));
|
| 112 | }
|
| 113 |
|
| 114 | static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dma_addr)
|
| 115 | {
|
| 116 | phys_addr_t paddr;
|
| 117 |
|
| 118 | dma_addr = dma_addr_canonical(dma_addr);
|
| 119 | if (dev->dma_range_map)
|
| 120 | paddr = translate_dma_to_phys(dev, dma_addr);
|
| 121 | else
|
| 122 | paddr = dma_addr;
|
| 123 |
|
| 124 | return paddr;
|
| 125 | }
|
| 126 | #endif
|
| 127 |
|
| 128 | #ifdef CONFIG_ARCH_HAS_FORCE_DMA_UNENCRYPTED
|
| 129 | bool force_dma_unencrypted(struct device *dev);
|
| 130 | #else
|
| 131 | static inline bool force_dma_unencrypted(struct device *dev)
|
| 132 | {
|
| 133 | return false;
|
| 134 | }
|
| 135 | #endif
|
| 136 |
|
| 137 | static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size,
|
| 138 | bool is_ram, unsigned long attrs)
|
| 139 | {
|
| 140 | dma_addr_t end = addr + size - 1;
|
| 141 |
|
| 142 | if (addr == DMA_MAPPING_ERROR)
|
| 143 | return false;
|
| 144 | |
| 145 | * The DMA address was derived from encrypted RAM, but this device
|
| 146 | * requires unencrypted DMA addresses. Treat it as not DMA-capable
|
| 147 | * so the caller can fall back to a suitable SWIOTLB pool.
|
| 148 |
|
| 149 | if (!(attrs & DMA_ATTR_CC_SHARED) && force_dma_unencrypted(dev))
|
| 150 | return false;
|
| 151 |
|
| 152 | if (is_ram && !IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) &&
|
| 153 | min(addr, end) < phys_to_dma(dev, PFN_PHYS(min_low_pfn)))
|
| 154 | return false;
|
| 155 |
|
| 156 | return end <= min_not_zero(*dev->dma_mask, dev->bus_dma_limit);
|
| 157 | }
|
| 158 |
|
| 159 | u64 dma_direct_get_required_mask(struct device *dev);
|
| 160 | void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
|
| 161 | gfp_t gfp, unsigned long attrs);
|
| 162 | void dma_direct_free(struct device *dev, size_t size, void *cpu_addr,
|
| 163 | dma_addr_t dma_addr, unsigned long attrs);
|
| 164 | struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
|
| 165 | dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp);
|
| 166 | void dma_direct_free_pages(struct device *dev, size_t size,
|
| 167 | struct page *page, dma_addr_t dma_addr,
|
| 168 | enum dma_data_direction dir);
|
| 169 | int dma_direct_supported(struct device *dev, u64 mask);
|
| 170 |
|
| 171 | #endif
|