Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
Christoph Hellwig | efa70f2 | 2020-09-01 13:34:33 +0200 | [diff] [blame] | 3 | * Copyright (C) 2018-2020 Christoph Hellwig. |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 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 | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 10 | #include <linux/dma-map-ops.h> |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 11 | #include <linux/scatterlist.h> |
| 12 | #include <linux/pfn.h> |
Christoph Hellwig | 3acac06 | 2019-10-29 11:06:32 +0100 | [diff] [blame] | 13 | #include <linux/vmalloc.h> |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 14 | #include <linux/set_memory.h> |
Jim Quinlan | e0d0727 | 2020-09-17 18:43:40 +0200 | [diff] [blame] | 15 | #include <linux/slab.h> |
Christoph Hellwig | 19c65c3 | 2020-09-22 15:34:22 +0200 | [diff] [blame] | 16 | #include "direct.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 | /* |
Randy Dunlap | 7b7b8a2 | 2020-10-15 20:10:28 -0700 | [diff] [blame] | 19 | * Most architectures use ZONE_DMA for the first 16 Megabytes, but some use |
Nicolas Saenz Julienne | 8b5369e | 2019-10-14 20:31:03 +0200 | [diff] [blame] | 20 | * it for entirely different regions. In that case the arch code needs to |
| 21 | * override the variable below for dma-direct to work properly. |
Christoph Hellwig | c61e963 | 2018-01-09 23:39:03 +0100 | [diff] [blame] | 22 | */ |
Nicolas Saenz Julienne | 8b5369e | 2019-10-14 20:31:03 +0200 | [diff] [blame] | 23 | unsigned int zone_dma_bits __ro_after_init = 24; |
Christoph Hellwig | c61e963 | 2018-01-09 23:39:03 +0100 | [diff] [blame] | 24 | |
Christoph Hellwig | a20bb05 | 2018-09-20 13:26:13 +0200 | [diff] [blame] | 25 | static inline dma_addr_t phys_to_dma_direct(struct device *dev, |
| 26 | phys_addr_t phys) |
| 27 | { |
Tom Lendacky | 9087c37 | 2019-07-10 19:01:19 +0000 | [diff] [blame] | 28 | if (force_dma_unencrypted(dev)) |
Christoph Hellwig | 5ceda74 | 2020-08-17 17:34:03 +0200 | [diff] [blame] | 29 | return phys_to_dma_unencrypted(dev, phys); |
Christoph Hellwig | a20bb05 | 2018-09-20 13:26:13 +0200 | [diff] [blame] | 30 | return phys_to_dma(dev, phys); |
| 31 | } |
| 32 | |
Christoph Hellwig | 34dc0ea | 2019-10-29 11:01:37 +0100 | [diff] [blame] | 33 | static inline struct page *dma_direct_to_page(struct device *dev, |
| 34 | dma_addr_t dma_addr) |
| 35 | { |
| 36 | return pfn_to_page(PHYS_PFN(dma_to_phys(dev, dma_addr))); |
| 37 | } |
| 38 | |
Christoph Hellwig | a20bb05 | 2018-09-20 13:26:13 +0200 | [diff] [blame] | 39 | u64 dma_direct_get_required_mask(struct device *dev) |
| 40 | { |
Kishon Vijay Abraham I | cdcda0d | 2020-04-06 10:58:36 +0530 | [diff] [blame] | 41 | phys_addr_t phys = (phys_addr_t)(max_pfn - 1) << PAGE_SHIFT; |
| 42 | u64 max_dma = phys_to_dma_direct(dev, phys); |
Christoph Hellwig | a20bb05 | 2018-09-20 13:26:13 +0200 | [diff] [blame] | 43 | |
| 44 | return (1ULL << (fls64(max_dma) - 1)) * 2 - 1; |
| 45 | } |
| 46 | |
Christoph Hellwig | 9420139 | 2020-08-14 12:26:24 +0200 | [diff] [blame] | 47 | static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask, |
David Rientjes | c84dc6e | 2020-04-14 17:04:55 -0700 | [diff] [blame] | 48 | u64 *phys_limit) |
Christoph Hellwig | 7d21ee4 | 2018-09-06 20:30:54 -0400 | [diff] [blame] | 49 | { |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 50 | u64 dma_limit = min_not_zero(dma_mask, dev->bus_dma_limit); |
Christoph Hellwig | b4ebe60 | 2018-09-20 14:04:08 +0200 | [diff] [blame] | 51 | |
Christoph Hellwig | 79ac32a | 2018-10-01 07:40:53 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Optimistically try the zone that the physical address mask falls |
| 54 | * into first. If that returns memory that isn't actually addressable |
| 55 | * we will fallback to the next lower zone and try again. |
| 56 | * |
| 57 | * Note that GFP_DMA32 and GFP_DMA are no ops without the corresponding |
| 58 | * zones. |
| 59 | */ |
Christoph Hellwig | 7bc5c42 | 2020-09-08 17:56:22 +0200 | [diff] [blame] | 60 | *phys_limit = dma_to_phys(dev, dma_limit); |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 61 | if (*phys_limit <= DMA_BIT_MASK(zone_dma_bits)) |
Christoph Hellwig | 7d21ee4 | 2018-09-06 20:30:54 -0400 | [diff] [blame] | 62 | return GFP_DMA; |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 63 | if (*phys_limit <= DMA_BIT_MASK(32)) |
Christoph Hellwig | 7d21ee4 | 2018-09-06 20:30:54 -0400 | [diff] [blame] | 64 | return GFP_DMA32; |
| 65 | return 0; |
| 66 | } |
| 67 | |
Christoph Hellwig | 9420139 | 2020-08-14 12:26:24 +0200 | [diff] [blame] | 68 | static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size) |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 69 | { |
Jim Quinlan | e0d0727 | 2020-09-17 18:43:40 +0200 | [diff] [blame] | 70 | dma_addr_t dma_addr = phys_to_dma_direct(dev, phys); |
| 71 | |
| 72 | if (dma_addr == DMA_MAPPING_ERROR) |
| 73 | return false; |
| 74 | return dma_addr + size - 1 <= |
| 75 | min_not_zero(dev->coherent_dma_mask, dev->bus_dma_limit); |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Christoph Hellwig | 4d05647 | 2021-10-18 13:18:34 +0200 | [diff] [blame] | 78 | static int dma_set_decrypted(struct device *dev, void *vaddr, size_t size) |
| 79 | { |
| 80 | if (!force_dma_unencrypted(dev)) |
| 81 | return 0; |
| 82 | return set_memory_decrypted((unsigned long)vaddr, 1 << get_order(size)); |
| 83 | } |
| 84 | |
| 85 | static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size) |
| 86 | { |
Christoph Hellwig | a90cf30 | 2021-11-09 15:41:01 +0100 | [diff] [blame^] | 87 | int ret; |
| 88 | |
Christoph Hellwig | 4d05647 | 2021-10-18 13:18:34 +0200 | [diff] [blame] | 89 | if (!force_dma_unencrypted(dev)) |
| 90 | return 0; |
Christoph Hellwig | a90cf30 | 2021-11-09 15:41:01 +0100 | [diff] [blame^] | 91 | ret = set_memory_encrypted((unsigned long)vaddr, 1 << get_order(size)); |
| 92 | if (ret) |
| 93 | pr_warn_ratelimited("leaking DMA memory that can't be re-encrypted\n"); |
| 94 | return ret; |
Christoph Hellwig | 4d05647 | 2021-10-18 13:18:34 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 97 | static void __dma_direct_free_pages(struct device *dev, struct page *page, |
| 98 | size_t size) |
| 99 | { |
| 100 | if (IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL) && |
| 101 | swiotlb_free(dev, page, size)) |
| 102 | return; |
| 103 | dma_free_contiguous(dev, page, size); |
| 104 | } |
| 105 | |
Christoph Hellwig | 26749b3 | 2020-06-15 08:52:31 +0200 | [diff] [blame] | 106 | static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size, |
Christoph Hellwig | 3773dfe | 2020-08-17 17:14:28 +0200 | [diff] [blame] | 107 | gfp_t gfp) |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 108 | { |
Christoph Hellwig | 90ae409 | 2019-08-20 11:45:49 +0900 | [diff] [blame] | 109 | int node = dev_to_node(dev); |
Christoph Hellwig | 080321d | 2017-12-22 11:51:44 +0100 | [diff] [blame] | 110 | struct page *page = NULL; |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 111 | u64 phys_limit; |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 112 | |
David Rientjes | 633d5fc | 2020-06-11 12:20:28 -0700 | [diff] [blame] | 113 | WARN_ON_ONCE(!PAGE_ALIGNED(size)); |
| 114 | |
David Rientjes | c84dc6e | 2020-04-14 17:04:55 -0700 | [diff] [blame] | 115 | gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask, |
| 116 | &phys_limit); |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 117 | if (IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL) && |
| 118 | is_swiotlb_for_alloc(dev)) { |
| 119 | page = swiotlb_alloc(dev, size); |
| 120 | if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) { |
| 121 | __dma_direct_free_pages(dev, page, size); |
| 122 | return NULL; |
| 123 | } |
| 124 | return page; |
| 125 | } |
| 126 | |
David Rientjes | 633d5fc | 2020-06-11 12:20:28 -0700 | [diff] [blame] | 127 | page = dma_alloc_contiguous(dev, size, gfp); |
Christoph Hellwig | 90ae409 | 2019-08-20 11:45:49 +0900 | [diff] [blame] | 128 | if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) { |
David Rientjes | 633d5fc | 2020-06-11 12:20:28 -0700 | [diff] [blame] | 129 | dma_free_contiguous(dev, page, size); |
Christoph Hellwig | 90ae409 | 2019-08-20 11:45:49 +0900 | [diff] [blame] | 130 | page = NULL; |
| 131 | } |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 132 | again: |
Christoph Hellwig | 90ae409 | 2019-08-20 11:45:49 +0900 | [diff] [blame] | 133 | if (!page) |
David Rientjes | 633d5fc | 2020-06-11 12:20:28 -0700 | [diff] [blame] | 134 | page = alloc_pages_node(node, gfp, get_order(size)); |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 135 | if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) { |
Nicolin Chen | b1d2dc0 | 2019-05-23 21:06:32 -0700 | [diff] [blame] | 136 | dma_free_contiguous(dev, page, size); |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 137 | page = NULL; |
| 138 | |
Takashi Iwai | de7eab3 | 2018-04-16 17:18:19 +0200 | [diff] [blame] | 139 | if (IS_ENABLED(CONFIG_ZONE_DMA32) && |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 140 | phys_limit < DMA_BIT_MASK(64) && |
Takashi Iwai | de7eab3 | 2018-04-16 17:18:19 +0200 | [diff] [blame] | 141 | !(gfp & (GFP_DMA32 | GFP_DMA))) { |
| 142 | gfp |= GFP_DMA32; |
| 143 | goto again; |
| 144 | } |
| 145 | |
Christoph Hellwig | fbce251 | 2019-02-13 08:01:03 +0100 | [diff] [blame] | 146 | if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) { |
Christoph Hellwig | 95f1839 | 2018-01-09 23:40:57 +0100 | [diff] [blame] | 147 | gfp = (gfp & ~GFP_DMA32) | GFP_DMA; |
| 148 | goto again; |
| 149 | } |
| 150 | } |
| 151 | |
Christoph Hellwig | b18814e7 | 2018-11-04 17:27:56 +0100 | [diff] [blame] | 152 | return page; |
| 153 | } |
| 154 | |
Christoph Hellwig | 5b138c5 | 2020-10-07 11:06:09 +0200 | [diff] [blame] | 155 | static void *dma_direct_alloc_from_pool(struct device *dev, size_t size, |
| 156 | dma_addr_t *dma_handle, gfp_t gfp) |
| 157 | { |
| 158 | struct page *page; |
| 159 | u64 phys_mask; |
| 160 | void *ret; |
| 161 | |
| 162 | gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask, |
| 163 | &phys_mask); |
| 164 | page = dma_alloc_from_pool(dev, size, &ret, gfp, dma_coherent_ok); |
| 165 | if (!page) |
| 166 | return NULL; |
| 167 | *dma_handle = phys_to_dma_direct(dev, page_to_phys(page)); |
| 168 | return ret; |
| 169 | } |
| 170 | |
Christoph Hellwig | 2f5388a2 | 2020-08-17 17:06:40 +0200 | [diff] [blame] | 171 | void *dma_direct_alloc(struct device *dev, size_t size, |
Christoph Hellwig | b18814e7 | 2018-11-04 17:27:56 +0100 | [diff] [blame] | 172 | dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) |
| 173 | { |
| 174 | struct page *page; |
| 175 | void *ret; |
| 176 | |
David Rientjes | 633d5fc | 2020-06-11 12:20:28 -0700 | [diff] [blame] | 177 | size = PAGE_ALIGN(size); |
Christoph Hellwig | 3773dfe | 2020-08-17 17:14:28 +0200 | [diff] [blame] | 178 | if (attrs & DMA_ATTR_NO_WARN) |
| 179 | gfp |= __GFP_NOWARN; |
David Rientjes | 633d5fc | 2020-06-11 12:20:28 -0700 | [diff] [blame] | 180 | |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 181 | if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) && |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 182 | !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) { |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 183 | page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO); |
| 184 | if (!page) |
| 185 | return NULL; |
| 186 | /* remove any dirty cache lines on the kernel alias */ |
| 187 | if (!PageHighMem(page)) |
| 188 | arch_dma_prep_coherent(page, size); |
| 189 | *dma_handle = phys_to_dma_direct(dev, page_to_phys(page)); |
| 190 | /* return the page pointer as the opaque cookie */ |
| 191 | return page; |
| 192 | } |
| 193 | |
| 194 | if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) && |
| 195 | !IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && |
Christoph Hellwig | faf4ef8 | 2021-06-23 14:21:16 +0200 | [diff] [blame] | 196 | !IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) && |
Linus Torvalds | 3de18c8 | 2021-09-03 10:34:44 -0700 | [diff] [blame] | 197 | !dev_is_dma_coherent(dev) && |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 198 | !is_swiotlb_for_alloc(dev)) |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 199 | return arch_dma_alloc(dev, size, dma_handle, gfp, attrs); |
| 200 | |
Christoph Hellwig | faf4ef8 | 2021-06-23 14:21:16 +0200 | [diff] [blame] | 201 | if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) && |
| 202 | !dev_is_dma_coherent(dev)) |
| 203 | return dma_alloc_from_global_coherent(dev, size, dma_handle); |
| 204 | |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 205 | /* |
| 206 | * Remapping or decrypting memory may block. If either is required and |
| 207 | * we can't block, allocate the memory from the atomic pools. |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 208 | * If restricted DMA (i.e., is_swiotlb_for_alloc) is required, one must |
| 209 | * set up another device coherent pool by shared-dma-pool and use |
| 210 | * dma_alloc_from_dev_coherent instead. |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 211 | */ |
| 212 | if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) && |
| 213 | !gfpflags_allow_blocking(gfp) && |
| 214 | (force_dma_unencrypted(dev) || |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 215 | (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && |
| 216 | !dev_is_dma_coherent(dev))) && |
| 217 | !is_swiotlb_for_alloc(dev)) |
Christoph Hellwig | 5b138c5 | 2020-10-07 11:06:09 +0200 | [diff] [blame] | 218 | return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp); |
Christoph Hellwig | 3acac06 | 2019-10-29 11:06:32 +0100 | [diff] [blame] | 219 | |
Christoph Hellwig | 3773dfe | 2020-08-17 17:14:28 +0200 | [diff] [blame] | 220 | /* we always manually zero the memory once we are done */ |
| 221 | page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO); |
Christoph Hellwig | 080321d | 2017-12-22 11:51:44 +0100 | [diff] [blame] | 222 | if (!page) |
| 223 | return NULL; |
Christoph Hellwig | b18814e7 | 2018-11-04 17:27:56 +0100 | [diff] [blame] | 224 | |
Christoph Hellwig | 3acac06 | 2019-10-29 11:06:32 +0100 | [diff] [blame] | 225 | if ((IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 226 | !dev_is_dma_coherent(dev)) || |
Christoph Hellwig | 3acac06 | 2019-10-29 11:06:32 +0100 | [diff] [blame] | 227 | (IS_ENABLED(CONFIG_DMA_REMAP) && PageHighMem(page))) { |
| 228 | /* remove any dirty cache lines on the kernel alias */ |
David Rientjes | 633d5fc | 2020-06-11 12:20:28 -0700 | [diff] [blame] | 229 | arch_dma_prep_coherent(page, size); |
Christoph Hellwig | 3acac06 | 2019-10-29 11:06:32 +0100 | [diff] [blame] | 230 | |
| 231 | /* create a coherent mapping */ |
David Rientjes | 633d5fc | 2020-06-11 12:20:28 -0700 | [diff] [blame] | 232 | ret = dma_common_contiguous_remap(page, size, |
Christoph Hellwig | 3acac06 | 2019-10-29 11:06:32 +0100 | [diff] [blame] | 233 | dma_pgprot(dev, PAGE_KERNEL, attrs), |
| 234 | __builtin_return_address(0)); |
Christoph Hellwig | 3d0fc34 | 2020-02-21 12:26:00 -0800 | [diff] [blame] | 235 | if (!ret) |
| 236 | goto out_free_pages; |
Christoph Hellwig | 3acac06 | 2019-10-29 11:06:32 +0100 | [diff] [blame] | 237 | memset(ret, 0, size); |
| 238 | goto done; |
Christoph Hellwig | d98849a | 2019-06-14 16:17:27 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Christoph Hellwig | 704f2c2 | 2018-09-22 20:47:26 +0200 | [diff] [blame] | 241 | if (PageHighMem(page)) { |
| 242 | /* |
| 243 | * Depending on the cma= arguments and per-arch setup |
Nicolin Chen | b1d2dc0 | 2019-05-23 21:06:32 -0700 | [diff] [blame] | 244 | * dma_alloc_contiguous could return highmem pages. |
Christoph Hellwig | 704f2c2 | 2018-09-22 20:47:26 +0200 | [diff] [blame] | 245 | * Without remapping there is no way to return them here, |
| 246 | * so log an error and fail. |
| 247 | */ |
| 248 | dev_info(dev, "Rejecting highmem page from CMA.\n"); |
Christoph Hellwig | 3d0fc34 | 2020-02-21 12:26:00 -0800 | [diff] [blame] | 249 | goto out_free_pages; |
Christoph Hellwig | 704f2c2 | 2018-09-22 20:47:26 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 252 | ret = page_address(page); |
Christoph Hellwig | 4d05647 | 2021-10-18 13:18:34 +0200 | [diff] [blame] | 253 | if (dma_set_decrypted(dev, ret, size)) |
| 254 | goto out_free_pages; |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 255 | memset(ret, 0, size); |
Christoph Hellwig | c30700d | 2019-06-03 08:43:51 +0200 | [diff] [blame] | 256 | |
Christoph Hellwig | fa7e224 | 2020-02-21 15:55:43 -0800 | [diff] [blame] | 257 | if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) && |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 258 | !dev_is_dma_coherent(dev)) { |
Christoph Hellwig | c30700d | 2019-06-03 08:43:51 +0200 | [diff] [blame] | 259 | arch_dma_prep_coherent(page, size); |
Christoph Hellwig | fa7e224 | 2020-02-21 15:55:43 -0800 | [diff] [blame] | 260 | ret = arch_dma_set_uncached(ret, size); |
| 261 | if (IS_ERR(ret)) |
David Rientjes | 96a539f | 2020-06-11 12:20:29 -0700 | [diff] [blame] | 262 | goto out_encrypt_pages; |
Christoph Hellwig | c30700d | 2019-06-03 08:43:51 +0200 | [diff] [blame] | 263 | } |
Christoph Hellwig | 3acac06 | 2019-10-29 11:06:32 +0100 | [diff] [blame] | 264 | done: |
Christoph Hellwig | 96eb89c | 2020-08-17 17:20:52 +0200 | [diff] [blame] | 265 | *dma_handle = phys_to_dma_direct(dev, page_to_phys(page)); |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 266 | return ret; |
David Rientjes | 96a539f | 2020-06-11 12:20:29 -0700 | [diff] [blame] | 267 | |
| 268 | out_encrypt_pages: |
Christoph Hellwig | 4d05647 | 2021-10-18 13:18:34 +0200 | [diff] [blame] | 269 | if (dma_set_encrypted(dev, page_address(page), size)) |
| 270 | return NULL; |
Christoph Hellwig | 3d0fc34 | 2020-02-21 12:26:00 -0800 | [diff] [blame] | 271 | out_free_pages: |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 272 | __dma_direct_free_pages(dev, page, size); |
Christoph Hellwig | 3d0fc34 | 2020-02-21 12:26:00 -0800 | [diff] [blame] | 273 | return NULL; |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Christoph Hellwig | 2f5388a2 | 2020-08-17 17:06:40 +0200 | [diff] [blame] | 276 | void dma_direct_free(struct device *dev, size_t size, |
| 277 | void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs) |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 278 | { |
Christoph Hellwig | c10f07a | 2018-03-19 11:38:25 +0100 | [diff] [blame] | 279 | unsigned int page_order = get_order(size); |
Christoph Hellwig | 080321d | 2017-12-22 11:51:44 +0100 | [diff] [blame] | 280 | |
Christoph Hellwig | cf14be0 | 2019-08-06 14:33:23 +0300 | [diff] [blame] | 281 | if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) && |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 282 | !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) { |
Christoph Hellwig | d98849a | 2019-06-14 16:17:27 +0200 | [diff] [blame] | 283 | /* cpu_addr is a struct page cookie, not a kernel address */ |
Christoph Hellwig | acaade1 | 2019-10-29 09:57:09 +0100 | [diff] [blame] | 284 | dma_free_contiguous(dev, cpu_addr, size); |
Christoph Hellwig | d98849a | 2019-06-14 16:17:27 +0200 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 288 | if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) && |
| 289 | !IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && |
Christoph Hellwig | faf4ef8 | 2021-06-23 14:21:16 +0200 | [diff] [blame] | 290 | !IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) && |
Linus Torvalds | 3de18c8 | 2021-09-03 10:34:44 -0700 | [diff] [blame] | 291 | !dev_is_dma_coherent(dev) && |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 292 | !is_swiotlb_for_alloc(dev)) { |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 293 | arch_dma_free(dev, size, cpu_addr, dma_addr, attrs); |
| 294 | return; |
| 295 | } |
| 296 | |
Christoph Hellwig | faf4ef8 | 2021-06-23 14:21:16 +0200 | [diff] [blame] | 297 | if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) && |
| 298 | !dev_is_dma_coherent(dev)) { |
| 299 | if (!dma_release_from_global_coherent(page_order, cpu_addr)) |
| 300 | WARN_ON_ONCE(1); |
| 301 | return; |
| 302 | } |
| 303 | |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 304 | /* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */ |
| 305 | if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) && |
| 306 | dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size))) |
| 307 | return; |
| 308 | |
Christoph Hellwig | 5570449 | 2021-10-21 09:20:39 +0200 | [diff] [blame] | 309 | if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) { |
Christoph Hellwig | 3acac06 | 2019-10-29 11:06:32 +0100 | [diff] [blame] | 310 | vunmap(cpu_addr); |
Christoph Hellwig | 5570449 | 2021-10-21 09:20:39 +0200 | [diff] [blame] | 311 | } else { |
| 312 | if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED)) |
| 313 | arch_dma_clear_uncached(cpu_addr, size); |
Christoph Hellwig | a90cf30 | 2021-11-09 15:41:01 +0100 | [diff] [blame^] | 314 | if (dma_set_encrypted(dev, cpu_addr, 1 << page_order)) |
| 315 | return; |
Christoph Hellwig | 5570449 | 2021-10-21 09:20:39 +0200 | [diff] [blame] | 316 | } |
Christoph Hellwig | 3acac06 | 2019-10-29 11:06:32 +0100 | [diff] [blame] | 317 | |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 318 | __dma_direct_free_pages(dev, dma_direct_to_page(dev, dma_addr), size); |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Christoph Hellwig | efa70f2 | 2020-09-01 13:34:33 +0200 | [diff] [blame] | 321 | struct page *dma_direct_alloc_pages(struct device *dev, size_t size, |
| 322 | dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp) |
| 323 | { |
| 324 | struct page *page; |
| 325 | void *ret; |
| 326 | |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 327 | if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) && |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 328 | force_dma_unencrypted(dev) && !gfpflags_allow_blocking(gfp) && |
| 329 | !is_swiotlb_for_alloc(dev)) |
Christoph Hellwig | 5b138c5 | 2020-10-07 11:06:09 +0200 | [diff] [blame] | 330 | return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp); |
Christoph Hellwig | efa70f2 | 2020-09-01 13:34:33 +0200 | [diff] [blame] | 331 | |
| 332 | page = __dma_direct_alloc_pages(dev, size, gfp); |
| 333 | if (!page) |
| 334 | return NULL; |
Christoph Hellwig | 08a89c2 | 2020-09-26 16:39:36 +0200 | [diff] [blame] | 335 | if (PageHighMem(page)) { |
| 336 | /* |
| 337 | * Depending on the cma= arguments and per-arch setup |
| 338 | * dma_alloc_contiguous could return highmem pages. |
| 339 | * Without remapping there is no way to return them here, |
| 340 | * so log an error and fail. |
| 341 | */ |
| 342 | dev_info(dev, "Rejecting highmem page from CMA.\n"); |
| 343 | goto out_free_pages; |
| 344 | } |
| 345 | |
Christoph Hellwig | efa70f2 | 2020-09-01 13:34:33 +0200 | [diff] [blame] | 346 | ret = page_address(page); |
Christoph Hellwig | 4d05647 | 2021-10-18 13:18:34 +0200 | [diff] [blame] | 347 | if (dma_set_decrypted(dev, ret, size)) |
| 348 | goto out_free_pages; |
Christoph Hellwig | efa70f2 | 2020-09-01 13:34:33 +0200 | [diff] [blame] | 349 | memset(ret, 0, size); |
Christoph Hellwig | efa70f2 | 2020-09-01 13:34:33 +0200 | [diff] [blame] | 350 | *dma_handle = phys_to_dma_direct(dev, page_to_phys(page)); |
| 351 | return page; |
| 352 | out_free_pages: |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 353 | __dma_direct_free_pages(dev, page, size); |
Christoph Hellwig | efa70f2 | 2020-09-01 13:34:33 +0200 | [diff] [blame] | 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | void dma_direct_free_pages(struct device *dev, size_t size, |
| 358 | struct page *page, dma_addr_t dma_addr, |
| 359 | enum dma_data_direction dir) |
| 360 | { |
| 361 | unsigned int page_order = get_order(size); |
| 362 | void *vaddr = page_address(page); |
| 363 | |
| 364 | /* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */ |
Christoph Hellwig | 849face | 2020-10-07 11:04:08 +0200 | [diff] [blame] | 365 | if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) && |
Christoph Hellwig | efa70f2 | 2020-09-01 13:34:33 +0200 | [diff] [blame] | 366 | dma_free_from_pool(dev, vaddr, size)) |
| 367 | return; |
| 368 | |
Christoph Hellwig | a90cf30 | 2021-11-09 15:41:01 +0100 | [diff] [blame^] | 369 | if (dma_set_encrypted(dev, vaddr, 1 << page_order)) |
| 370 | return; |
Claire Chang | f4111e3 | 2021-06-19 11:40:40 +0800 | [diff] [blame] | 371 | __dma_direct_free_pages(dev, page, size); |
Christoph Hellwig | efa70f2 | 2020-09-01 13:34:33 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 374 | #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \ |
| 375 | defined(CONFIG_SWIOTLB) |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 376 | void dma_direct_sync_sg_for_device(struct device *dev, |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 377 | struct scatterlist *sgl, int nents, enum dma_data_direction dir) |
| 378 | { |
| 379 | struct scatterlist *sg; |
| 380 | int i; |
| 381 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 382 | for_each_sg(sgl, sg, nents, i) { |
Fugang Duan | 449fa54 | 2019-07-19 17:26:48 +0800 | [diff] [blame] | 383 | phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg)); |
| 384 | |
Claire Chang | 7fd856a | 2021-06-19 11:40:35 +0800 | [diff] [blame] | 385 | if (unlikely(is_swiotlb_buffer(dev, paddr))) |
Christoph Hellwig | 80808d2 | 2021-03-01 08:44:26 +0100 | [diff] [blame] | 386 | swiotlb_sync_single_for_device(dev, paddr, sg->length, |
| 387 | dir); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 388 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 389 | if (!dev_is_dma_coherent(dev)) |
Christoph Hellwig | 56e35f9 | 2019-11-07 18:03:11 +0100 | [diff] [blame] | 390 | arch_sync_dma_for_device(paddr, sg->length, |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 391 | dir); |
| 392 | } |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 393 | } |
Christoph Hellwig | 17ac524 | 2018-12-03 11:14:09 +0100 | [diff] [blame] | 394 | #endif |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 395 | |
| 396 | #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \ |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 397 | defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) || \ |
| 398 | defined(CONFIG_SWIOTLB) |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 399 | void dma_direct_sync_sg_for_cpu(struct device *dev, |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 400 | struct scatterlist *sgl, int nents, enum dma_data_direction dir) |
| 401 | { |
| 402 | struct scatterlist *sg; |
| 403 | int i; |
| 404 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 405 | for_each_sg(sgl, sg, nents, i) { |
Fugang Duan | 449fa54 | 2019-07-19 17:26:48 +0800 | [diff] [blame] | 406 | phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg)); |
| 407 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 408 | if (!dev_is_dma_coherent(dev)) |
Christoph Hellwig | 56e35f9 | 2019-11-07 18:03:11 +0100 | [diff] [blame] | 409 | arch_sync_dma_for_cpu(paddr, sg->length, dir); |
Fugang Duan | 449fa54 | 2019-07-19 17:26:48 +0800 | [diff] [blame] | 410 | |
Claire Chang | 7fd856a | 2021-06-19 11:40:35 +0800 | [diff] [blame] | 411 | if (unlikely(is_swiotlb_buffer(dev, paddr))) |
Christoph Hellwig | 80808d2 | 2021-03-01 08:44:26 +0100 | [diff] [blame] | 412 | swiotlb_sync_single_for_cpu(dev, paddr, sg->length, |
| 413 | dir); |
Christoph Hellwig | abdaf11 | 2020-08-17 16:41:50 +0200 | [diff] [blame] | 414 | |
| 415 | if (dir == DMA_FROM_DEVICE) |
| 416 | arch_dma_mark_clean(paddr, sg->length); |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 417 | } |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 418 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 419 | if (!dev_is_dma_coherent(dev)) |
Christoph Hellwig | 56e35f9 | 2019-11-07 18:03:11 +0100 | [diff] [blame] | 420 | arch_sync_dma_for_cpu_all(); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 423 | void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 424 | int nents, enum dma_data_direction dir, unsigned long attrs) |
| 425 | { |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 426 | struct scatterlist *sg; |
| 427 | int i; |
| 428 | |
| 429 | for_each_sg(sgl, sg, nents, i) |
| 430 | dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir, |
| 431 | attrs); |
Christoph Hellwig | bc3ec75 | 2018-09-08 11:22:43 +0200 | [diff] [blame] | 432 | } |
| 433 | #endif |
| 434 | |
Christoph Hellwig | 782e676 | 2018-04-16 15:24:51 +0200 | [diff] [blame] | 435 | int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, |
| 436 | enum dma_data_direction dir, unsigned long attrs) |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 437 | { |
| 438 | int i; |
| 439 | struct scatterlist *sg; |
| 440 | |
| 441 | for_each_sg(sgl, sg, nents, i) { |
Christoph Hellwig | 17ac524 | 2018-12-03 11:14:09 +0100 | [diff] [blame] | 442 | sg->dma_address = dma_direct_map_page(dev, sg_page(sg), |
| 443 | sg->offset, sg->length, dir, attrs); |
| 444 | if (sg->dma_address == DMA_MAPPING_ERROR) |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 445 | goto out_unmap; |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 446 | sg_dma_len(sg) = sg->length; |
| 447 | } |
| 448 | |
| 449 | return nents; |
Christoph Hellwig | 55897af | 2018-12-03 11:43:54 +0100 | [diff] [blame] | 450 | |
| 451 | out_unmap: |
| 452 | dma_direct_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC); |
Logan Gunthorpe | c81be74 | 2021-07-29 14:15:20 -0600 | [diff] [blame] | 453 | return -EIO; |
Christoph Hellwig | 002e674 | 2018-01-09 16:30:23 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Christoph Hellwig | cfced78 | 2019-01-04 18:20:05 +0100 | [diff] [blame] | 456 | dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr, |
| 457 | size_t size, enum dma_data_direction dir, unsigned long attrs) |
| 458 | { |
| 459 | dma_addr_t dma_addr = paddr; |
| 460 | |
Christoph Hellwig | 68a33b1 | 2019-11-19 17:38:58 +0100 | [diff] [blame] | 461 | if (unlikely(!dma_capable(dev, dma_addr, size, false))) { |
Christoph Hellwig | 75467ee | 2020-02-03 14:54:50 +0100 | [diff] [blame] | 462 | dev_err_once(dev, |
| 463 | "DMA addr %pad+%zu overflow (mask %llx, bus limit %llx).\n", |
| 464 | &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit); |
| 465 | WARN_ON_ONCE(1); |
Christoph Hellwig | cfced78 | 2019-01-04 18:20:05 +0100 | [diff] [blame] | 466 | return DMA_MAPPING_ERROR; |
| 467 | } |
| 468 | |
| 469 | return dma_addr; |
| 470 | } |
Christoph Hellwig | cfced78 | 2019-01-04 18:20:05 +0100 | [diff] [blame] | 471 | |
Christoph Hellwig | 34dc0ea | 2019-10-29 11:01:37 +0100 | [diff] [blame] | 472 | int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt, |
| 473 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 474 | unsigned long attrs) |
| 475 | { |
| 476 | struct page *page = dma_direct_to_page(dev, dma_addr); |
| 477 | int ret; |
| 478 | |
| 479 | ret = sg_alloc_table(sgt, 1, GFP_KERNEL); |
| 480 | if (!ret) |
| 481 | sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); |
| 482 | return ret; |
| 483 | } |
| 484 | |
Christoph Hellwig | 34dc0ea | 2019-10-29 11:01:37 +0100 | [diff] [blame] | 485 | bool dma_direct_can_mmap(struct device *dev) |
| 486 | { |
| 487 | return dev_is_dma_coherent(dev) || |
| 488 | IS_ENABLED(CONFIG_DMA_NONCOHERENT_MMAP); |
| 489 | } |
| 490 | |
| 491 | int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma, |
| 492 | void *cpu_addr, dma_addr_t dma_addr, size_t size, |
| 493 | unsigned long attrs) |
| 494 | { |
| 495 | unsigned long user_count = vma_pages(vma); |
| 496 | unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 497 | unsigned long pfn = PHYS_PFN(dma_to_phys(dev, dma_addr)); |
| 498 | int ret = -ENXIO; |
| 499 | |
| 500 | vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs); |
| 501 | |
| 502 | if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret)) |
| 503 | return ret; |
Christoph Hellwig | faf4ef8 | 2021-06-23 14:21:16 +0200 | [diff] [blame] | 504 | if (dma_mmap_from_global_coherent(vma, cpu_addr, size, &ret)) |
| 505 | return ret; |
Christoph Hellwig | 34dc0ea | 2019-10-29 11:01:37 +0100 | [diff] [blame] | 506 | |
| 507 | if (vma->vm_pgoff >= count || user_count > count - vma->vm_pgoff) |
| 508 | return -ENXIO; |
| 509 | return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, |
| 510 | user_count << PAGE_SHIFT, vma->vm_page_prot); |
| 511 | } |
Christoph Hellwig | 34dc0ea | 2019-10-29 11:01:37 +0100 | [diff] [blame] | 512 | |
Christoph Hellwig | 1a9777a | 2017-12-24 15:04:32 +0100 | [diff] [blame] | 513 | int dma_direct_supported(struct device *dev, u64 mask) |
| 514 | { |
Christoph Hellwig | 91ef26f | 2020-02-03 18:11:10 +0100 | [diff] [blame] | 515 | u64 min_mask = (max_pfn - 1) << PAGE_SHIFT; |
Christoph Hellwig | 9d7a224 | 2018-09-07 09:31:58 +0200 | [diff] [blame] | 516 | |
Christoph Hellwig | 91ef26f | 2020-02-03 18:11:10 +0100 | [diff] [blame] | 517 | /* |
| 518 | * Because 32-bit DMA masks are so common we expect every architecture |
| 519 | * to be able to satisfy them - either by not supporting more physical |
| 520 | * memory, or by providing a ZONE_DMA32. If neither is the case, the |
| 521 | * architecture needs to use an IOMMU instead of the direct mapping. |
| 522 | */ |
| 523 | if (mask >= DMA_BIT_MASK(32)) |
| 524 | return 1; |
Christoph Hellwig | 9d7a224 | 2018-09-07 09:31:58 +0200 | [diff] [blame] | 525 | |
Lendacky, Thomas | c92a54c | 2018-12-17 14:39:16 +0000 | [diff] [blame] | 526 | /* |
Christoph Hellwig | 5ceda74 | 2020-08-17 17:34:03 +0200 | [diff] [blame] | 527 | * This check needs to be against the actual bit mask value, so use |
| 528 | * phys_to_dma_unencrypted() here so that the SME encryption mask isn't |
Lendacky, Thomas | c92a54c | 2018-12-17 14:39:16 +0000 | [diff] [blame] | 529 | * part of the check. |
| 530 | */ |
Christoph Hellwig | 91ef26f | 2020-02-03 18:11:10 +0100 | [diff] [blame] | 531 | if (IS_ENABLED(CONFIG_ZONE_DMA)) |
| 532 | min_mask = min_t(u64, min_mask, DMA_BIT_MASK(zone_dma_bits)); |
Christoph Hellwig | 5ceda74 | 2020-08-17 17:34:03 +0200 | [diff] [blame] | 533 | return mask >= phys_to_dma_unencrypted(dev, min_mask); |
Christoph Hellwig | 1a9777a | 2017-12-24 15:04:32 +0100 | [diff] [blame] | 534 | } |
Joerg Roedel | 133d624 | 2019-02-07 12:59:15 +0100 | [diff] [blame] | 535 | |
| 536 | size_t dma_direct_max_mapping_size(struct device *dev) |
| 537 | { |
Joerg Roedel | 133d624 | 2019-02-07 12:59:15 +0100 | [diff] [blame] | 538 | /* If SWIOTLB is active, use its maximum mapping size */ |
Claire Chang | 6f2beb2 | 2021-06-19 11:40:36 +0800 | [diff] [blame] | 539 | if (is_swiotlb_active(dev) && |
Claire Chang | 903cd0f | 2021-06-24 23:55:20 +0800 | [diff] [blame] | 540 | (dma_addressing_limited(dev) || is_swiotlb_force_bounce(dev))) |
Christoph Hellwig | a5008b5 | 2019-07-16 22:00:54 +0200 | [diff] [blame] | 541 | return swiotlb_max_mapping_size(dev); |
| 542 | return SIZE_MAX; |
Joerg Roedel | 133d624 | 2019-02-07 12:59:15 +0100 | [diff] [blame] | 543 | } |
Christoph Hellwig | 3aa916250 | 2020-06-29 15:03:56 +0200 | [diff] [blame] | 544 | |
| 545 | bool dma_direct_need_sync(struct device *dev, dma_addr_t dma_addr) |
| 546 | { |
| 547 | return !dev_is_dma_coherent(dev) || |
Claire Chang | 7fd856a | 2021-06-19 11:40:35 +0800 | [diff] [blame] | 548 | is_swiotlb_buffer(dev, dma_to_phys(dev, dma_addr)); |
Christoph Hellwig | 3aa916250 | 2020-06-29 15:03:56 +0200 | [diff] [blame] | 549 | } |
Jim Quinlan | e0d0727 | 2020-09-17 18:43:40 +0200 | [diff] [blame] | 550 | |
| 551 | /** |
| 552 | * dma_direct_set_offset - Assign scalar offset for a single DMA range. |
| 553 | * @dev: device pointer; needed to "own" the alloced memory. |
| 554 | * @cpu_start: beginning of memory region covered by this offset. |
| 555 | * @dma_start: beginning of DMA/PCI region covered by this offset. |
| 556 | * @size: size of the region. |
| 557 | * |
| 558 | * This is for the simple case of a uniform offset which cannot |
| 559 | * be discovered by "dma-ranges". |
| 560 | * |
| 561 | * It returns -ENOMEM if out of memory, -EINVAL if a map |
| 562 | * already exists, 0 otherwise. |
| 563 | * |
| 564 | * Note: any call to this from a driver is a bug. The mapping needs |
| 565 | * to be described by the device tree or other firmware interfaces. |
| 566 | */ |
| 567 | int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start, |
| 568 | dma_addr_t dma_start, u64 size) |
| 569 | { |
| 570 | struct bus_dma_region *map; |
| 571 | u64 offset = (u64)cpu_start - (u64)dma_start; |
| 572 | |
| 573 | if (dev->dma_range_map) { |
| 574 | dev_err(dev, "attempt to add DMA range to existing map\n"); |
| 575 | return -EINVAL; |
| 576 | } |
| 577 | |
| 578 | if (!offset) |
| 579 | return 0; |
| 580 | |
| 581 | map = kcalloc(2, sizeof(*map), GFP_KERNEL); |
| 582 | if (!map) |
| 583 | return -ENOMEM; |
| 584 | map[0].cpu_start = cpu_start; |
| 585 | map[0].dma_start = dma_start; |
| 586 | map[0].offset = offset; |
| 587 | map[0].size = size; |
| 588 | dev->dma_range_map = map; |
| 589 | return 0; |
| 590 | } |