Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 3 | * Copyright (C) 2018 Christoph Hellwig. |
| 4 | * |
| 5 | * DMA operations that map physical memory directly without using an IOMMU. |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 6 | */ |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 7 | #include <linux/memblock.h> /* for max_pfn */ |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 8 | #include <linux/export.h> |
| 9 | #include <linux/mm.h> |
Christoph Hellwig | 2e86a04 | 2017-12-22 11:29:51 +0100 | [diff] [blame] | 10 | #include <linux/dma-direct.h> |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 11 | #include <linux/scatterlist.h> |
Christoph Hellwig | 080321d | 2017-12-22 11:51:44 +0100 | [diff] [blame] | 12 | #include <linux/dma-contiguous.h> |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 13 | #include <linux/dma-noncoherent.h> |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 14 | #include <linux/pfn.h> |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 15 | #include <linux/set_memory.h> |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 16 | #include <linux/swiotlb.h> |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 17 | |
Christoph Hellwig | c61e963 | 2018-01-09 23:39:03 +0100 | [diff] [blame] | 18 | /* |
| 19 | * Most architectures use ZONE_DMA for the first 16 Megabytes, but |
| 20 | * some use it for entirely different regions: |
| 21 | */ |
| 22 | #ifndef ARCH_ZONE_DMA_BITS |
| 23 | #define ARCH_ZONE_DMA_BITS 24 |
| 24 | #endif |
| 25 | |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 26 | /* |
| 27 | * For AMD SEV all DMA must be to unencrypted addresses. |
| 28 | */ |
| 29 | static inline bool force_dma_unencrypted(void) |
| 30 | { |
| 31 | return sev_active(); |
| 32 | } |
| 33 | |
Christoph Hellwig | 58dfd4a | 2018-12-03 07:43:05 +0100 | [diff] [blame] | 34 | static void report_addr(struct device *dev, dma_addr_t dma_addr, size_t size) |
Christoph Hellwig | 2797596 | 2018-01-09 16:30:47 +0100 | [diff] [blame] | 35 | { |
Christoph Hellwig | 58dfd4a | 2018-12-03 07:43:05 +0100 | [diff] [blame] | 36 | if (!dev->dma_mask) { |
| 37 | dev_err_once(dev, "DMA map on device without dma_mask\n"); |
| 38 | } else if (*dev->dma_mask >= DMA_BIT_MASK(32) || dev->bus_dma_mask) { |
| 39 | dev_err_once(dev, |
| 40 | "overflow %pad+%zu of DMA mask %llx bus mask %llx\n", |
| 41 | &dma_addr, size, *dev->dma_mask, dev->bus_dma_mask); |
Christoph Hellwig | 2797596 | 2018-01-09 16:30:47 +0100 | [diff] [blame] | 42 | } |
Christoph Hellwig | 58dfd4a | 2018-12-03 07:43:05 +0100 | [diff] [blame] | 43 | WARN_ON_ONCE(1); |
Christoph Hellwig | 2797596 | 2018-01-09 16:30:47 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Christoph Hellwig | a20bb05 | 2018-09-20 13:26:13 +0200 | [diff] [blame] | 46 | static inline dma_addr_t phys_to_dma_direct(struct device *dev, |
| 47 | phys_addr_t phys) |
| 48 | { |
| 49 | if (force_dma_unencrypted()) |
| 50 | return __phys_to_dma(dev, phys); |
| 51 | return phys_to_dma(dev, phys); |
| 52 | } |
| 53 | |
| 54 | u64 dma_direct_get_required_mask(struct device *dev) |
| 55 | { |
| 56 | u64 max_dma = phys_to_dma_direct(dev, (max_pfn - 1) << PAGE_SHIFT); |
| 57 | |
Christoph Hellwig | b4ebe60 | 2018-09-20 14:04:08 +0200 | [diff] [blame] | 58 | if (dev->bus_dma_mask && dev->bus_dma_mask < max_dma) |
| 59 | max_dma = dev->bus_dma_mask; |
| 60 | |
Christoph Hellwig | a20bb05 | 2018-09-20 13:26:13 +0200 | [diff] [blame] | 61 | return (1ULL << (fls64(max_dma) - 1)) * 2 - 1; |
| 62 | } |
| 63 | |
Christoph Hellwig | 7d21ee4 | 2018-09-06 20:30:54 -0400 | [diff] [blame] | 64 | static gfp_t __dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask, |
| 65 | u64 *phys_mask) |
| 66 | { |
Christoph Hellwig | b4ebe60 | 2018-09-20 14:04:08 +0200 | [diff] [blame] | 67 | if (dev->bus_dma_mask && dev->bus_dma_mask < dma_mask) |
| 68 | dma_mask = dev->bus_dma_mask; |
| 69 | |
Christoph Hellwig | 7d21ee4 | 2018-09-06 20:30:54 -0400 | [diff] [blame] | 70 | if (force_dma_unencrypted()) |
| 71 | *phys_mask = __dma_to_phys(dev, dma_mask); |
| 72 | else |
| 73 | *phys_mask = dma_to_phys(dev, dma_mask); |
| 74 | |
Christoph Hellwig | 79ac32a | 2018-10-01 07:40:53 -0700 | [diff] [blame] | 75 | /* |
| 76 | * Optimistically try the zone that the physical address mask falls |
| 77 | * into first. If that returns memory that isn't actually addressable |
| 78 | * we will fallback to the next lower zone and try again. |
| 79 | * |
| 80 | * Note that GFP_DMA32 and GFP_DMA are no ops without the corresponding |
| 81 | * zones. |
| 82 | */ |
Christoph Hellwig | 7d21ee4 | 2018-09-06 20:30:54 -0400 | [diff] [blame] | 83 | if (*phys_mask <= DMA_BIT_MASK(ARCH_ZONE_DMA_BITS)) |
| 84 | return GFP_DMA; |
| 85 | if (*phys_mask <= DMA_BIT_MASK(32)) |
| 86 | return GFP_DMA32; |
| 87 | return 0; |
| 88 | } |
| 89 | |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 90 | static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size) |
| 91 | { |
Christoph Hellwig | a20bb05 | 2018-09-20 13:26:13 +0200 | [diff] [blame] | 92 | return phys_to_dma_direct(dev, phys) + size - 1 <= |
Christoph Hellwig | b4ebe60 | 2018-09-20 14:04:08 +0200 | [diff] [blame] | 93 | min_not_zero(dev->coherent_dma_mask, dev->bus_dma_mask); |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Christoph Hellwig | b18814e7 | 2018-11-04 17:27:56 +0100 | [diff] [blame] | 96 | struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 97 | dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 98 | { |
Christoph Hellwig | 080321d | 2017-12-22 11:51:44 +0100 | [diff] [blame] | 99 | unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 100 | int page_order = get_order(size); |
| 101 | struct page *page = NULL; |
Christoph Hellwig | 7d21ee4 | 2018-09-06 20:30:54 -0400 | [diff] [blame] | 102 | u64 phys_mask; |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 103 | |
Christoph Hellwig | b9fd042 | 2018-09-24 13:10:34 +0200 | [diff] [blame] | 104 | if (attrs & DMA_ATTR_NO_WARN) |
| 105 | gfp |= __GFP_NOWARN; |
| 106 | |
Christoph Hellwig | e89f5b3 | 2018-03-28 15:35:35 +0200 | [diff] [blame] | 107 | /* we always manually zero the memory once we are done: */ |
| 108 | gfp &= ~__GFP_ZERO; |
Christoph Hellwig | 7d21ee4 | 2018-09-06 20:30:54 -0400 | [diff] [blame] | 109 | gfp |= __dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask, |
| 110 | &phys_mask); |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 111 | again: |
Christoph Hellwig | 080321d | 2017-12-22 11:51:44 +0100 | [diff] [blame] | 112 | /* CMA can be used only in the context which permits sleeping */ |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 113 | if (gfpflags_allow_blocking(gfp)) { |
Marek Szyprowski | d834c5a | 2018-08-17 15:49:00 -0700 | [diff] [blame] | 114 | page = dma_alloc_from_contiguous(dev, count, page_order, |
| 115 | gfp & __GFP_NOWARN); |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 116 | if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) { |
| 117 | dma_release_from_contiguous(dev, page, count); |
| 118 | page = NULL; |
| 119 | } |
| 120 | } |
Christoph Hellwig | 080321d | 2017-12-22 11:51:44 +0100 | [diff] [blame] | 121 | if (!page) |
Christoph Hellwig | 21f237e | 2017-12-22 11:55:23 +0100 | [diff] [blame] | 122 | page = alloc_pages_node(dev_to_node(dev), gfp, page_order); |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 123 | |
| 124 | if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) { |
| 125 | __free_pages(page, page_order); |
| 126 | page = NULL; |
| 127 | |
Takashi Iwai | de7eab3 | 2018-04-16 17:18:19 +0200 | [diff] [blame] | 128 | if (IS_ENABLED(CONFIG_ZONE_DMA32) && |
Christoph Hellwig | 7d21ee4 | 2018-09-06 20:30:54 -0400 | [diff] [blame] | 129 | phys_mask < DMA_BIT_MASK(64) && |
Takashi Iwai | de7eab3 | 2018-04-16 17:18:19 +0200 | [diff] [blame] | 130 | !(gfp & (GFP_DMA32 | GFP_DMA))) { |
| 131 | gfp |= GFP_DMA32; |
| 132 | goto again; |
| 133 | } |
| 134 | |
Takashi Iwai | 504a918 | 2018-04-15 11:08:07 +0200 | [diff] [blame] | 135 | if (IS_ENABLED(CONFIG_ZONE_DMA) && |
Christoph Hellwig | 7d21ee4 | 2018-09-06 20:30:54 -0400 | [diff] [blame] | 136 | phys_mask < DMA_BIT_MASK(32) && !(gfp & GFP_DMA)) { |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 137 | gfp = (gfp & ~GFP_DMA32) | GFP_DMA; |
| 138 | goto again; |
| 139 | } |
| 140 | } |
| 141 | |
Christoph Hellwig | b18814e7 | 2018-11-04 17:27:56 +0100 | [diff] [blame] | 142 | return page; |
| 143 | } |
| 144 | |
| 145 | void *dma_direct_alloc_pages(struct device *dev, size_t size, |
| 146 | dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) |
| 147 | { |
| 148 | struct page *page; |
| 149 | void *ret; |
| 150 | |
| 151 | page = __dma_direct_alloc_pages(dev, size, dma_handle, gfp, attrs); |
Christoph Hellwig | 080321d | 2017-12-22 11:51:44 +0100 | [diff] [blame] | 152 | if (!page) |
| 153 | return NULL; |
Christoph Hellwig | b18814e7 | 2018-11-04 17:27:56 +0100 | [diff] [blame] | 154 | |
Christoph Hellwig | 704f2c2 | 2018-09-22 20:47:26 +0200 | [diff] [blame] | 155 | if (PageHighMem(page)) { |
| 156 | /* |
| 157 | * Depending on the cma= arguments and per-arch setup |
| 158 | * dma_alloc_from_contiguous could return highmem pages. |
| 159 | * Without remapping there is no way to return them here, |
| 160 | * so log an error and fail. |
| 161 | */ |
| 162 | dev_info(dev, "Rejecting highmem page from CMA.\n"); |
| 163 | __dma_direct_free_pages(dev, size, page); |
| 164 | return NULL; |
| 165 | } |
| 166 | |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 167 | ret = page_address(page); |
| 168 | if (force_dma_unencrypted()) { |
Christoph Hellwig | b18814e7 | 2018-11-04 17:27:56 +0100 | [diff] [blame] | 169 | set_memory_decrypted((unsigned long)ret, 1 << get_order(size)); |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 170 | *dma_handle = __phys_to_dma(dev, page_to_phys(page)); |
| 171 | } else { |
| 172 | *dma_handle = phys_to_dma(dev, page_to_phys(page)); |
| 173 | } |
| 174 | memset(ret, 0, size); |
| 175 | return ret; |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Christoph Hellwig | b18814e7 | 2018-11-04 17:27:56 +0100 | [diff] [blame] | 178 | void __dma_direct_free_pages(struct device *dev, size_t size, struct page *page) |
| 179 | { |
| 180 | unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 181 | |
| 182 | if (!dma_release_from_contiguous(dev, page, count)) |
| 183 | __free_pages(page, get_order(size)); |
| 184 | } |
| 185 | |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 186 | void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr, |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 187 | dma_addr_t dma_addr, unsigned long attrs) |
| 188 | { |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 189 | unsigned int page_order = get_order(size); |
Christoph Hellwig | 080321d | 2017-12-22 11:51:44 +0100 | [diff] [blame] | 190 | |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 191 | if (force_dma_unencrypted()) |
| 192 | set_memory_encrypted((unsigned long)cpu_addr, 1 << page_order); |
Christoph Hellwig | b18814e7 | 2018-11-04 17:27:56 +0100 | [diff] [blame] | 193 | __dma_direct_free_pages(dev, size, virt_to_page(cpu_addr)); |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 196 | void *dma_direct_alloc(struct device *dev, size_t size, |
| 197 | dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) |
| 198 | { |
| 199 | if (!dev_is_dma_coherent(dev)) |
| 200 | return arch_dma_alloc(dev, size, dma_handle, gfp, attrs); |
| 201 | return dma_direct_alloc_pages(dev, size, dma_handle, gfp, attrs); |
| 202 | } |
| 203 | |
| 204 | void dma_direct_free(struct device *dev, size_t size, |
| 205 | void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs) |
| 206 | { |
| 207 | if (!dev_is_dma_coherent(dev)) |
| 208 | arch_dma_free(dev, size, cpu_addr, dma_addr, attrs); |
| 209 | else |
| 210 | dma_direct_free_pages(dev, size, cpu_addr, dma_addr, attrs); |
| 211 | } |
| 212 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 213 | #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ |
| 214 | defined(CONFIG_SWIOTLB) |
| 215 | void dma_direct_sync_single_for_device(struct device *dev, |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 216 | dma_addr_t addr, size_t size, enum dma_data_direction dir) |
| 217 | { |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 218 | phys_addr_t paddr = dma_to_phys(dev, addr); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 219 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 220 | if (unlikely(is_swiotlb_buffer(paddr))) |
| 221 | swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_DEVICE); |
| 222 | |
| 223 | if (!dev_is_dma_coherent(dev)) |
| 224 | arch_sync_dma_for_device(dev, paddr, size, dir); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 225 | } |
Christoph Hellwig | 356da6d | 2018-12-06 13:39:32 -0800 | [diff] [blame] | 226 | EXPORT_SYMBOL(dma_direct_sync_single_for_device); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 227 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 228 | void dma_direct_sync_sg_for_device(struct device *dev, |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 229 | struct scatterlist *sgl, int nents, enum dma_data_direction dir) |
| 230 | { |
| 231 | struct scatterlist *sg; |
| 232 | int i; |
| 233 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 234 | for_each_sg(sgl, sg, nents, i) { |
| 235 | if (unlikely(is_swiotlb_buffer(sg_phys(sg)))) |
| 236 | swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length, |
| 237 | dir, SYNC_FOR_DEVICE); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 238 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 239 | if (!dev_is_dma_coherent(dev)) |
| 240 | arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, |
| 241 | dir); |
| 242 | } |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 243 | } |
Christoph Hellwig | 356da6d | 2018-12-06 13:39:32 -0800 | [diff] [blame] | 244 | EXPORT_SYMBOL(dma_direct_sync_sg_for_device); |
Christoph Hellwig | 17ac524 | 2018-12-03 11:14:09 +0100 | [diff] [blame] | 245 | #endif |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 246 | |
| 247 | #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 248 | defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) || \ |
| 249 | defined(CONFIG_SWIOTLB) |
| 250 | void dma_direct_sync_single_for_cpu(struct device *dev, |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 251 | dma_addr_t addr, size_t size, enum dma_data_direction dir) |
| 252 | { |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 253 | phys_addr_t paddr = dma_to_phys(dev, addr); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 254 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 255 | if (!dev_is_dma_coherent(dev)) { |
| 256 | arch_sync_dma_for_cpu(dev, paddr, size, dir); |
| 257 | arch_sync_dma_for_cpu_all(dev); |
| 258 | } |
| 259 | |
| 260 | if (unlikely(is_swiotlb_buffer(paddr))) |
| 261 | swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_CPU); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 262 | } |
Christoph Hellwig | 356da6d | 2018-12-06 13:39:32 -0800 | [diff] [blame] | 263 | EXPORT_SYMBOL(dma_direct_sync_single_for_cpu); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 264 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 265 | void dma_direct_sync_sg_for_cpu(struct device *dev, |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 266 | struct scatterlist *sgl, int nents, enum dma_data_direction dir) |
| 267 | { |
| 268 | struct scatterlist *sg; |
| 269 | int i; |
| 270 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 271 | for_each_sg(sgl, sg, nents, i) { |
| 272 | if (!dev_is_dma_coherent(dev)) |
| 273 | arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir); |
| 274 | |
| 275 | if (unlikely(is_swiotlb_buffer(sg_phys(sg)))) |
| 276 | swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length, dir, |
| 277 | SYNC_FOR_CPU); |
| 278 | } |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 279 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 280 | if (!dev_is_dma_coherent(dev)) |
| 281 | arch_sync_dma_for_cpu_all(dev); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 282 | } |
Christoph Hellwig | 356da6d | 2018-12-06 13:39:32 -0800 | [diff] [blame] | 283 | EXPORT_SYMBOL(dma_direct_sync_sg_for_cpu); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 284 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 285 | void dma_direct_unmap_page(struct device *dev, dma_addr_t addr, |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 286 | size_t size, enum dma_data_direction dir, unsigned long attrs) |
| 287 | { |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 288 | phys_addr_t phys = dma_to_phys(dev, addr); |
| 289 | |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 290 | if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC)) |
| 291 | dma_direct_sync_single_for_cpu(dev, addr, size, dir); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 292 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 293 | if (unlikely(is_swiotlb_buffer(phys))) |
| 294 | swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 295 | } |
Christoph Hellwig | 356da6d | 2018-12-06 13:39:32 -0800 | [diff] [blame] | 296 | EXPORT_SYMBOL(dma_direct_unmap_page); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 297 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 298 | void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 299 | int nents, enum dma_data_direction dir, unsigned long attrs) |
| 300 | { |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 301 | struct scatterlist *sg; |
| 302 | int i; |
| 303 | |
| 304 | for_each_sg(sgl, sg, nents, i) |
| 305 | dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir, |
| 306 | attrs); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 307 | } |
Christoph Hellwig | 356da6d | 2018-12-06 13:39:32 -0800 | [diff] [blame] | 308 | EXPORT_SYMBOL(dma_direct_unmap_sg); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 309 | #endif |
| 310 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 311 | static inline bool dma_direct_possible(struct device *dev, dma_addr_t dma_addr, |
| 312 | size_t size) |
| 313 | { |
| 314 | return swiotlb_force != SWIOTLB_FORCE && |
| 315 | (!dev || dma_capable(dev, dma_addr, size)); |
| 316 | } |
| 317 | |
Christoph Hellwig | 782e676 | 2018-04-16 15:24:51 +0200 | [diff] [blame] | 318 | dma_addr_t dma_direct_map_page(struct device *dev, struct page *page, |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 319 | unsigned long offset, size_t size, enum dma_data_direction dir, |
| 320 | unsigned long attrs) |
| 321 | { |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 322 | phys_addr_t phys = page_to_phys(page) + offset; |
| 323 | dma_addr_t dma_addr = phys_to_dma(dev, phys); |
Christoph Hellwig | 2797596 | 2018-01-09 16:30:47 +0100 | [diff] [blame] | 324 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 325 | if (unlikely(!dma_direct_possible(dev, dma_addr, size)) && |
| 326 | !swiotlb_map(dev, &phys, &dma_addr, size, dir, attrs)) { |
Christoph Hellwig | 58dfd4a | 2018-12-03 07:43:05 +0100 | [diff] [blame] | 327 | report_addr(dev, dma_addr, size); |
Christoph Hellwig | b0cbeae | 2018-11-21 18:52:35 +0100 | [diff] [blame] | 328 | return DMA_MAPPING_ERROR; |
Christoph Hellwig | 58dfd4a | 2018-12-03 07:43:05 +0100 | [diff] [blame] | 329 | } |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 330 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 331 | if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) |
| 332 | arch_sync_dma_for_device(dev, phys, size, dir); |
Christoph Hellwig | 2797596 | 2018-01-09 16:30:47 +0100 | [diff] [blame] | 333 | return dma_addr; |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 334 | } |
Christoph Hellwig | 356da6d | 2018-12-06 13:39:32 -0800 | [diff] [blame] | 335 | EXPORT_SYMBOL(dma_direct_map_page); |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 336 | |
Christoph Hellwig | 782e676 | 2018-04-16 15:24:51 +0200 | [diff] [blame] | 337 | int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, |
| 338 | enum dma_data_direction dir, unsigned long attrs) |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 339 | { |
| 340 | int i; |
| 341 | struct scatterlist *sg; |
| 342 | |
| 343 | for_each_sg(sgl, sg, nents, i) { |
Christoph Hellwig | 17ac524 | 2018-12-03 11:14:09 +0100 | [diff] [blame] | 344 | sg->dma_address = dma_direct_map_page(dev, sg_page(sg), |
| 345 | sg->offset, sg->length, dir, attrs); |
| 346 | if (sg->dma_address == DMA_MAPPING_ERROR) |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 347 | goto out_unmap; |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 348 | sg_dma_len(sg) = sg->length; |
| 349 | } |
| 350 | |
| 351 | return nents; |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 352 | |
| 353 | out_unmap: |
| 354 | dma_direct_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC); |
| 355 | return 0; |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 356 | } |
Christoph Hellwig | 356da6d | 2018-12-06 13:39:32 -0800 | [diff] [blame] | 357 | EXPORT_SYMBOL(dma_direct_map_sg); |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 358 | |
Christoph Hellwig | 9d7a224 | 2018-09-07 09:31:58 +0200 | [diff] [blame] | 359 | /* |
| 360 | * Because 32-bit DMA masks are so common we expect every architecture to be |
| 361 | * able to satisfy them - either by not supporting more physical memory, or by |
| 362 | * providing a ZONE_DMA32. If neither is the case, the architecture needs to |
| 363 | * use an IOMMU instead of the direct mapping. |
| 364 | */ |
Christoph Hellwig | 1a9777a | 2017-12-24 15:04:32 +0100 | [diff] [blame] | 365 | int dma_direct_supported(struct device *dev, u64 mask) |
| 366 | { |
Christoph Hellwig | 9d7a224 | 2018-09-07 09:31:58 +0200 | [diff] [blame] | 367 | u64 min_mask; |
| 368 | |
| 369 | if (IS_ENABLED(CONFIG_ZONE_DMA)) |
| 370 | min_mask = DMA_BIT_MASK(ARCH_ZONE_DMA_BITS); |
| 371 | else |
| 372 | min_mask = DMA_BIT_MASK(32); |
| 373 | |
| 374 | min_mask = min_t(u64, min_mask, (max_pfn - 1) << PAGE_SHIFT); |
| 375 | |
Lendacky, Thomas | c92a54c | 2018-12-17 14:39:16 +0000 | [diff] [blame] | 376 | /* |
| 377 | * This check needs to be against the actual bit mask value, so |
| 378 | * use __phys_to_dma() here so that the SME encryption mask isn't |
| 379 | * part of the check. |
| 380 | */ |
| 381 | return mask >= __phys_to_dma(dev, min_mask); |
Christoph Hellwig | 1a9777a | 2017-12-24 15:04:32 +0100 | [diff] [blame] | 382 | } |