blob: cf75bfb2f499ef7da15cb3dfe9bdc1435e852112 [file] [log] [blame]
Christoph Hellwig002e6742018-01-09 16:30:23 +01001// SPDX-License-Identifier: GPL-2.0
2/*
Christoph Hellwigefa70f22020-09-01 13:34:33 +02003 * Copyright (C) 2018-2020 Christoph Hellwig.
Christoph Hellwigbc3ec752018-09-08 11:22:43 +02004 *
5 * DMA operations that map physical memory directly without using an IOMMU.
Christoph Hellwig002e6742018-01-09 16:30:23 +01006 */
Mike Rapoport57c8a662018-10-30 15:09:49 -07007#include <linux/memblock.h> /* for max_pfn */
Christoph Hellwig002e6742018-01-09 16:30:23 +01008#include <linux/export.h>
9#include <linux/mm.h>
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +020010#include <linux/dma-map-ops.h>
Christoph Hellwig002e6742018-01-09 16:30:23 +010011#include <linux/scatterlist.h>
12#include <linux/pfn.h>
Christoph Hellwig3acac062019-10-29 11:06:32 +010013#include <linux/vmalloc.h>
Christoph Hellwigc10f07a2018-03-19 11:38:25 +010014#include <linux/set_memory.h>
Jim Quinlane0d07272020-09-17 18:43:40 +020015#include <linux/slab.h>
Christoph Hellwig19c65c32020-09-22 15:34:22 +020016#include "direct.h"
Christoph Hellwig002e6742018-01-09 16:30:23 +010017
Christoph Hellwigc61e9632018-01-09 23:39:03 +010018/*
Randy Dunlap7b7b8a22020-10-15 20:10:28 -070019 * Most architectures use ZONE_DMA for the first 16 Megabytes, but some use
Nicolas Saenz Julienne8b5369e2019-10-14 20:31:03 +020020 * 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 Hellwigc61e9632018-01-09 23:39:03 +010022 */
Nicolas Saenz Julienne8b5369e2019-10-14 20:31:03 +020023unsigned int zone_dma_bits __ro_after_init = 24;
Christoph Hellwigc61e9632018-01-09 23:39:03 +010024
Christoph Hellwiga20bb052018-09-20 13:26:13 +020025static inline dma_addr_t phys_to_dma_direct(struct device *dev,
26 phys_addr_t phys)
27{
Tom Lendacky9087c372019-07-10 19:01:19 +000028 if (force_dma_unencrypted(dev))
Christoph Hellwig5ceda742020-08-17 17:34:03 +020029 return phys_to_dma_unencrypted(dev, phys);
Christoph Hellwiga20bb052018-09-20 13:26:13 +020030 return phys_to_dma(dev, phys);
31}
32
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +010033static 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 Hellwiga20bb052018-09-20 13:26:13 +020039u64 dma_direct_get_required_mask(struct device *dev)
40{
Kishon Vijay Abraham Icdcda0d2020-04-06 10:58:36 +053041 phys_addr_t phys = (phys_addr_t)(max_pfn - 1) << PAGE_SHIFT;
42 u64 max_dma = phys_to_dma_direct(dev, phys);
Christoph Hellwiga20bb052018-09-20 13:26:13 +020043
44 return (1ULL << (fls64(max_dma) - 1)) * 2 - 1;
45}
46
Christoph Hellwig94201392020-08-14 12:26:24 +020047static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
David Rientjesc84dc6e2020-04-14 17:04:55 -070048 u64 *phys_limit)
Christoph Hellwig7d21ee42018-09-06 20:30:54 -040049{
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +010050 u64 dma_limit = min_not_zero(dma_mask, dev->bus_dma_limit);
Christoph Hellwigb4ebe602018-09-20 14:04:08 +020051
Christoph Hellwig79ac32a2018-10-01 07:40:53 -070052 /*
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 Hellwig7bc5c422020-09-08 17:56:22 +020060 *phys_limit = dma_to_phys(dev, dma_limit);
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +010061 if (*phys_limit <= DMA_BIT_MASK(zone_dma_bits))
Christoph Hellwig7d21ee42018-09-06 20:30:54 -040062 return GFP_DMA;
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +010063 if (*phys_limit <= DMA_BIT_MASK(32))
Christoph Hellwig7d21ee42018-09-06 20:30:54 -040064 return GFP_DMA32;
65 return 0;
66}
67
Christoph Hellwig94201392020-08-14 12:26:24 +020068static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
Christoph Hellwig95f18392018-01-09 23:40:57 +010069{
Jim Quinlane0d07272020-09-17 18:43:40 +020070 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 Hellwig95f18392018-01-09 23:40:57 +010076}
77
Christoph Hellwig4d056472021-10-18 13:18:34 +020078static 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
85static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size)
86{
Christoph Hellwiga90cf302021-11-09 15:41:01 +010087 int ret;
88
Christoph Hellwig4d056472021-10-18 13:18:34 +020089 if (!force_dma_unencrypted(dev))
90 return 0;
Christoph Hellwiga90cf302021-11-09 15:41:01 +010091 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 Hellwig4d056472021-10-18 13:18:34 +020095}
96
Claire Changf4111e32021-06-19 11:40:40 +080097static void __dma_direct_free_pages(struct device *dev, struct page *page,
98 size_t size)
99{
Christoph Hellwigf5d39392021-10-21 09:34:59 +0200100 if (swiotlb_free(dev, page, size))
Claire Changf4111e32021-06-19 11:40:40 +0800101 return;
102 dma_free_contiguous(dev, page, size);
103}
104
Christoph Hellwig26749b32020-06-15 08:52:31 +0200105static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
Christoph Hellwig3773dfe2020-08-17 17:14:28 +0200106 gfp_t gfp)
Christoph Hellwig002e6742018-01-09 16:30:23 +0100107{
Christoph Hellwig90ae4092019-08-20 11:45:49 +0900108 int node = dev_to_node(dev);
Christoph Hellwig080321d2017-12-22 11:51:44 +0100109 struct page *page = NULL;
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +0100110 u64 phys_limit;
Christoph Hellwig002e6742018-01-09 16:30:23 +0100111
David Rientjes633d5fc2020-06-11 12:20:28 -0700112 WARN_ON_ONCE(!PAGE_ALIGNED(size));
113
David Rientjesc84dc6e2020-04-14 17:04:55 -0700114 gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
115 &phys_limit);
Christoph Hellwigf5d39392021-10-21 09:34:59 +0200116 if (is_swiotlb_for_alloc(dev)) {
Claire Changf4111e32021-06-19 11:40:40 +0800117 page = swiotlb_alloc(dev, size);
118 if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
119 __dma_direct_free_pages(dev, page, size);
120 return NULL;
121 }
122 return page;
123 }
124
David Rientjes633d5fc2020-06-11 12:20:28 -0700125 page = dma_alloc_contiguous(dev, size, gfp);
Christoph Hellwig90ae4092019-08-20 11:45:49 +0900126 if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
David Rientjes633d5fc2020-06-11 12:20:28 -0700127 dma_free_contiguous(dev, page, size);
Christoph Hellwig90ae4092019-08-20 11:45:49 +0900128 page = NULL;
129 }
Christoph Hellwig95f18392018-01-09 23:40:57 +0100130again:
Christoph Hellwig90ae4092019-08-20 11:45:49 +0900131 if (!page)
David Rientjes633d5fc2020-06-11 12:20:28 -0700132 page = alloc_pages_node(node, gfp, get_order(size));
Christoph Hellwig95f18392018-01-09 23:40:57 +0100133 if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
Nicolin Chenb1d2dc02019-05-23 21:06:32 -0700134 dma_free_contiguous(dev, page, size);
Christoph Hellwig95f18392018-01-09 23:40:57 +0100135 page = NULL;
136
Takashi Iwaide7eab32018-04-16 17:18:19 +0200137 if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +0100138 phys_limit < DMA_BIT_MASK(64) &&
Takashi Iwaide7eab32018-04-16 17:18:19 +0200139 !(gfp & (GFP_DMA32 | GFP_DMA))) {
140 gfp |= GFP_DMA32;
141 goto again;
142 }
143
Christoph Hellwigfbce2512019-02-13 08:01:03 +0100144 if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) {
Christoph Hellwig95f18392018-01-09 23:40:57 +0100145 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
146 goto again;
147 }
148 }
149
Christoph Hellwigb18814e72018-11-04 17:27:56 +0100150 return page;
151}
152
Christoph Hellwig5b138c52020-10-07 11:06:09 +0200153static void *dma_direct_alloc_from_pool(struct device *dev, size_t size,
154 dma_addr_t *dma_handle, gfp_t gfp)
155{
156 struct page *page;
157 u64 phys_mask;
158 void *ret;
159
Christoph Hellwig78bc7272021-10-21 10:00:55 +0200160 if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_DMA_COHERENT_POOL)))
161 return NULL;
162
Christoph Hellwig5b138c52020-10-07 11:06:09 +0200163 gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
164 &phys_mask);
165 page = dma_alloc_from_pool(dev, size, &ret, gfp, dma_coherent_ok);
166 if (!page)
167 return NULL;
168 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
169 return ret;
170}
171
Christoph Hellwigd541ae52021-10-18 13:08:07 +0200172static void *dma_direct_alloc_no_mapping(struct device *dev, size_t size,
173 dma_addr_t *dma_handle, gfp_t gfp)
174{
175 struct page *page;
176
177 page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO);
178 if (!page)
179 return NULL;
180
181 /* remove any dirty cache lines on the kernel alias */
182 if (!PageHighMem(page))
183 arch_dma_prep_coherent(page, size);
184
185 /* return the page pointer as the opaque cookie */
186 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
187 return page;
188}
189
Christoph Hellwig2f5388a22020-08-17 17:06:40 +0200190void *dma_direct_alloc(struct device *dev, size_t size,
Christoph Hellwigb18814e72018-11-04 17:27:56 +0100191 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
192{
Christoph Hellwigf3c96222021-11-09 15:20:40 +0100193 bool remap = false, set_uncached = false;
Christoph Hellwigb18814e72018-11-04 17:27:56 +0100194 struct page *page;
195 void *ret;
196
David Rientjes633d5fc2020-06-11 12:20:28 -0700197 size = PAGE_ALIGN(size);
Christoph Hellwig3773dfe2020-08-17 17:14:28 +0200198 if (attrs & DMA_ATTR_NO_WARN)
199 gfp |= __GFP_NOWARN;
David Rientjes633d5fc2020-06-11 12:20:28 -0700200
Christoph Hellwig849face2020-10-07 11:04:08 +0200201 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
Christoph Hellwigd541ae52021-10-18 13:08:07 +0200202 !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev))
203 return dma_direct_alloc_no_mapping(dev, size, dma_handle, gfp);
Christoph Hellwig849face2020-10-07 11:04:08 +0200204
Christoph Hellwiga86d1092021-10-21 09:47:31 +0200205 if (!dev_is_dma_coherent(dev)) {
206 /*
207 * Fallback to the arch handler if it exists. This should
208 * eventually go away.
209 */
210 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) &&
211 !IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
212 !IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) &&
213 !is_swiotlb_for_alloc(dev))
214 return arch_dma_alloc(dev, size, dma_handle, gfp,
215 attrs);
Christoph Hellwig849face2020-10-07 11:04:08 +0200216
Christoph Hellwiga86d1092021-10-21 09:47:31 +0200217 /*
218 * If there is a global pool, always allocate from it for
219 * non-coherent devices.
220 */
221 if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL))
222 return dma_alloc_from_global_coherent(dev, size,
223 dma_handle);
224
225 /*
226 * Otherwise remap if the architecture is asking for it. But
227 * given that remapping memory is a blocking operation we'll
228 * instead have to dip into the atomic pools.
229 */
230 remap = IS_ENABLED(CONFIG_DMA_DIRECT_REMAP);
231 if (remap) {
232 if (!gfpflags_allow_blocking(gfp) &&
233 !is_swiotlb_for_alloc(dev))
234 return dma_direct_alloc_from_pool(dev, size,
235 dma_handle, gfp);
236 } else {
Christoph Hellwig955f58f2021-11-09 15:47:56 +0100237 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED))
238 return NULL;
239 set_uncached = true;
Christoph Hellwiga86d1092021-10-21 09:47:31 +0200240 }
241 }
Christoph Hellwigfaf4ef82021-06-23 14:21:16 +0200242
Christoph Hellwig849face2020-10-07 11:04:08 +0200243 /*
Christoph Hellwiga86d1092021-10-21 09:47:31 +0200244 * Decrypting memory may block, so allocate the memory from the atomic
245 * pools if we can't block.
Christoph Hellwig849face2020-10-07 11:04:08 +0200246 */
Christoph Hellwig78bc7272021-10-21 10:00:55 +0200247 if (force_dma_unencrypted(dev) && !gfpflags_allow_blocking(gfp) &&
Claire Changf4111e32021-06-19 11:40:40 +0800248 !is_swiotlb_for_alloc(dev))
Christoph Hellwig5b138c52020-10-07 11:06:09 +0200249 return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp);
Christoph Hellwig3acac062019-10-29 11:06:32 +0100250
Christoph Hellwig3773dfe2020-08-17 17:14:28 +0200251 /* we always manually zero the memory once we are done */
252 page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO);
Christoph Hellwig080321d2017-12-22 11:51:44 +0100253 if (!page)
254 return NULL;
Christoph Hellwiga86d1092021-10-21 09:47:31 +0200255 if (PageHighMem(page)) {
Christoph Hellwigf3c96222021-11-09 15:20:40 +0100256 /*
257 * Depending on the cma= arguments and per-arch setup,
258 * dma_alloc_contiguous could return highmem pages.
259 * Without remapping there is no way to return them here, so
260 * log an error and fail.
261 */
262 if (!IS_ENABLED(CONFIG_DMA_REMAP)) {
263 dev_info(dev, "Rejecting highmem page from CMA.\n");
264 goto out_free_pages;
265 }
266 remap = true;
Christoph Hellwiga86d1092021-10-21 09:47:31 +0200267 set_uncached = false;
268 }
Christoph Hellwigf3c96222021-11-09 15:20:40 +0100269
270 if (remap) {
Christoph Hellwig3acac062019-10-29 11:06:32 +0100271 /* remove any dirty cache lines on the kernel alias */
David Rientjes633d5fc2020-06-11 12:20:28 -0700272 arch_dma_prep_coherent(page, size);
Christoph Hellwig3acac062019-10-29 11:06:32 +0100273
274 /* create a coherent mapping */
David Rientjes633d5fc2020-06-11 12:20:28 -0700275 ret = dma_common_contiguous_remap(page, size,
Christoph Hellwig3acac062019-10-29 11:06:32 +0100276 dma_pgprot(dev, PAGE_KERNEL, attrs),
277 __builtin_return_address(0));
Christoph Hellwig3d0fc342020-02-21 12:26:00 -0800278 if (!ret)
279 goto out_free_pages;
Christoph Hellwigf3c96222021-11-09 15:20:40 +0100280 } else {
281 ret = page_address(page);
282 if (dma_set_decrypted(dev, ret, size))
283 goto out_free_pages;
Christoph Hellwigd98849a2019-06-14 16:17:27 +0200284 }
285
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100286 memset(ret, 0, size);
Christoph Hellwigc30700d2019-06-03 08:43:51 +0200287
Christoph Hellwigf3c96222021-11-09 15:20:40 +0100288 if (set_uncached) {
Christoph Hellwigc30700d2019-06-03 08:43:51 +0200289 arch_dma_prep_coherent(page, size);
Christoph Hellwigfa7e2242020-02-21 15:55:43 -0800290 ret = arch_dma_set_uncached(ret, size);
291 if (IS_ERR(ret))
David Rientjes96a539f2020-06-11 12:20:29 -0700292 goto out_encrypt_pages;
Christoph Hellwigc30700d2019-06-03 08:43:51 +0200293 }
Christoph Hellwigf3c96222021-11-09 15:20:40 +0100294
Christoph Hellwig96eb89c2020-08-17 17:20:52 +0200295 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100296 return ret;
David Rientjes96a539f2020-06-11 12:20:29 -0700297
298out_encrypt_pages:
Christoph Hellwig4d056472021-10-18 13:18:34 +0200299 if (dma_set_encrypted(dev, page_address(page), size))
300 return NULL;
Christoph Hellwig3d0fc342020-02-21 12:26:00 -0800301out_free_pages:
Claire Changf4111e32021-06-19 11:40:40 +0800302 __dma_direct_free_pages(dev, page, size);
Christoph Hellwig3d0fc342020-02-21 12:26:00 -0800303 return NULL;
Christoph Hellwig002e6742018-01-09 16:30:23 +0100304}
305
Christoph Hellwig2f5388a22020-08-17 17:06:40 +0200306void dma_direct_free(struct device *dev, size_t size,
307 void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs)
Christoph Hellwig002e6742018-01-09 16:30:23 +0100308{
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100309 unsigned int page_order = get_order(size);
Christoph Hellwig080321d2017-12-22 11:51:44 +0100310
Christoph Hellwigcf14be02019-08-06 14:33:23 +0300311 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
Claire Changf4111e32021-06-19 11:40:40 +0800312 !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) {
Christoph Hellwigd98849a2019-06-14 16:17:27 +0200313 /* cpu_addr is a struct page cookie, not a kernel address */
Christoph Hellwigacaade12019-10-29 09:57:09 +0100314 dma_free_contiguous(dev, cpu_addr, size);
Christoph Hellwigd98849a2019-06-14 16:17:27 +0200315 return;
316 }
317
Christoph Hellwig849face2020-10-07 11:04:08 +0200318 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) &&
319 !IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
Christoph Hellwigfaf4ef82021-06-23 14:21:16 +0200320 !IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) &&
Linus Torvalds3de18c82021-09-03 10:34:44 -0700321 !dev_is_dma_coherent(dev) &&
Claire Changf4111e32021-06-19 11:40:40 +0800322 !is_swiotlb_for_alloc(dev)) {
Christoph Hellwig849face2020-10-07 11:04:08 +0200323 arch_dma_free(dev, size, cpu_addr, dma_addr, attrs);
324 return;
325 }
326
Christoph Hellwigfaf4ef82021-06-23 14:21:16 +0200327 if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) &&
328 !dev_is_dma_coherent(dev)) {
329 if (!dma_release_from_global_coherent(page_order, cpu_addr))
330 WARN_ON_ONCE(1);
331 return;
332 }
333
Christoph Hellwig849face2020-10-07 11:04:08 +0200334 /* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */
335 if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
336 dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size)))
337 return;
338
Christoph Hellwig55704492021-10-21 09:20:39 +0200339 if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
Christoph Hellwig3acac062019-10-29 11:06:32 +0100340 vunmap(cpu_addr);
Christoph Hellwig55704492021-10-21 09:20:39 +0200341 } else {
342 if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED))
343 arch_dma_clear_uncached(cpu_addr, size);
Christoph Hellwiga90cf302021-11-09 15:41:01 +0100344 if (dma_set_encrypted(dev, cpu_addr, 1 << page_order))
345 return;
Christoph Hellwig55704492021-10-21 09:20:39 +0200346 }
Christoph Hellwig3acac062019-10-29 11:06:32 +0100347
Claire Changf4111e32021-06-19 11:40:40 +0800348 __dma_direct_free_pages(dev, dma_direct_to_page(dev, dma_addr), size);
Christoph Hellwig002e6742018-01-09 16:30:23 +0100349}
350
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200351struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
352 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
353{
354 struct page *page;
355 void *ret;
356
Christoph Hellwig78bc7272021-10-21 10:00:55 +0200357 if (force_dma_unencrypted(dev) && !gfpflags_allow_blocking(gfp) &&
Claire Changf4111e32021-06-19 11:40:40 +0800358 !is_swiotlb_for_alloc(dev))
Christoph Hellwig5b138c52020-10-07 11:06:09 +0200359 return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp);
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200360
361 page = __dma_direct_alloc_pages(dev, size, gfp);
362 if (!page)
363 return NULL;
Christoph Hellwig08a89c22020-09-26 16:39:36 +0200364 if (PageHighMem(page)) {
365 /*
366 * Depending on the cma= arguments and per-arch setup
367 * dma_alloc_contiguous could return highmem pages.
368 * Without remapping there is no way to return them here,
369 * so log an error and fail.
370 */
371 dev_info(dev, "Rejecting highmem page from CMA.\n");
372 goto out_free_pages;
373 }
374
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200375 ret = page_address(page);
Christoph Hellwig4d056472021-10-18 13:18:34 +0200376 if (dma_set_decrypted(dev, ret, size))
377 goto out_free_pages;
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200378 memset(ret, 0, size);
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200379 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
380 return page;
381out_free_pages:
Claire Changf4111e32021-06-19 11:40:40 +0800382 __dma_direct_free_pages(dev, page, size);
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200383 return NULL;
384}
385
386void dma_direct_free_pages(struct device *dev, size_t size,
387 struct page *page, dma_addr_t dma_addr,
388 enum dma_data_direction dir)
389{
390 unsigned int page_order = get_order(size);
391 void *vaddr = page_address(page);
392
393 /* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */
Christoph Hellwig849face2020-10-07 11:04:08 +0200394 if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200395 dma_free_from_pool(dev, vaddr, size))
396 return;
397
Christoph Hellwiga90cf302021-11-09 15:41:01 +0100398 if (dma_set_encrypted(dev, vaddr, 1 << page_order))
399 return;
Claire Changf4111e32021-06-19 11:40:40 +0800400 __dma_direct_free_pages(dev, page, size);
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200401}
402
Christoph Hellwig55897af2018-12-03 11:43:54 +0100403#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
404 defined(CONFIG_SWIOTLB)
Christoph Hellwig55897af2018-12-03 11:43:54 +0100405void dma_direct_sync_sg_for_device(struct device *dev,
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200406 struct scatterlist *sgl, int nents, enum dma_data_direction dir)
407{
408 struct scatterlist *sg;
409 int i;
410
Christoph Hellwig55897af2018-12-03 11:43:54 +0100411 for_each_sg(sgl, sg, nents, i) {
Fugang Duan449fa542019-07-19 17:26:48 +0800412 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
413
Claire Chang7fd856a2021-06-19 11:40:35 +0800414 if (unlikely(is_swiotlb_buffer(dev, paddr)))
Christoph Hellwig80808d22021-03-01 08:44:26 +0100415 swiotlb_sync_single_for_device(dev, paddr, sg->length,
416 dir);
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200417
Christoph Hellwig55897af2018-12-03 11:43:54 +0100418 if (!dev_is_dma_coherent(dev))
Christoph Hellwig56e35f92019-11-07 18:03:11 +0100419 arch_sync_dma_for_device(paddr, sg->length,
Christoph Hellwig55897af2018-12-03 11:43:54 +0100420 dir);
421 }
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200422}
Christoph Hellwig17ac5242018-12-03 11:14:09 +0100423#endif
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200424
425#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
Christoph Hellwig55897af2018-12-03 11:43:54 +0100426 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) || \
427 defined(CONFIG_SWIOTLB)
Christoph Hellwig55897af2018-12-03 11:43:54 +0100428void dma_direct_sync_sg_for_cpu(struct device *dev,
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200429 struct scatterlist *sgl, int nents, enum dma_data_direction dir)
430{
431 struct scatterlist *sg;
432 int i;
433
Christoph Hellwig55897af2018-12-03 11:43:54 +0100434 for_each_sg(sgl, sg, nents, i) {
Fugang Duan449fa542019-07-19 17:26:48 +0800435 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
436
Christoph Hellwig55897af2018-12-03 11:43:54 +0100437 if (!dev_is_dma_coherent(dev))
Christoph Hellwig56e35f92019-11-07 18:03:11 +0100438 arch_sync_dma_for_cpu(paddr, sg->length, dir);
Fugang Duan449fa542019-07-19 17:26:48 +0800439
Claire Chang7fd856a2021-06-19 11:40:35 +0800440 if (unlikely(is_swiotlb_buffer(dev, paddr)))
Christoph Hellwig80808d22021-03-01 08:44:26 +0100441 swiotlb_sync_single_for_cpu(dev, paddr, sg->length,
442 dir);
Christoph Hellwigabdaf112020-08-17 16:41:50 +0200443
444 if (dir == DMA_FROM_DEVICE)
445 arch_dma_mark_clean(paddr, sg->length);
Christoph Hellwig55897af2018-12-03 11:43:54 +0100446 }
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200447
Christoph Hellwig55897af2018-12-03 11:43:54 +0100448 if (!dev_is_dma_coherent(dev))
Christoph Hellwig56e35f92019-11-07 18:03:11 +0100449 arch_sync_dma_for_cpu_all();
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200450}
451
Christoph Hellwig55897af2018-12-03 11:43:54 +0100452void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200453 int nents, enum dma_data_direction dir, unsigned long attrs)
454{
Christoph Hellwig55897af2018-12-03 11:43:54 +0100455 struct scatterlist *sg;
456 int i;
457
458 for_each_sg(sgl, sg, nents, i)
459 dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir,
460 attrs);
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200461}
462#endif
463
Christoph Hellwig782e6762018-04-16 15:24:51 +0200464int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
465 enum dma_data_direction dir, unsigned long attrs)
Christoph Hellwig002e6742018-01-09 16:30:23 +0100466{
467 int i;
468 struct scatterlist *sg;
469
470 for_each_sg(sgl, sg, nents, i) {
Christoph Hellwig17ac5242018-12-03 11:14:09 +0100471 sg->dma_address = dma_direct_map_page(dev, sg_page(sg),
472 sg->offset, sg->length, dir, attrs);
473 if (sg->dma_address == DMA_MAPPING_ERROR)
Christoph Hellwig55897af2018-12-03 11:43:54 +0100474 goto out_unmap;
Christoph Hellwig002e6742018-01-09 16:30:23 +0100475 sg_dma_len(sg) = sg->length;
476 }
477
478 return nents;
Christoph Hellwig55897af2018-12-03 11:43:54 +0100479
480out_unmap:
481 dma_direct_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
Logan Gunthorpec81be742021-07-29 14:15:20 -0600482 return -EIO;
Christoph Hellwig002e6742018-01-09 16:30:23 +0100483}
484
Christoph Hellwigcfced782019-01-04 18:20:05 +0100485dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr,
486 size_t size, enum dma_data_direction dir, unsigned long attrs)
487{
488 dma_addr_t dma_addr = paddr;
489
Christoph Hellwig68a33b12019-11-19 17:38:58 +0100490 if (unlikely(!dma_capable(dev, dma_addr, size, false))) {
Christoph Hellwig75467ee2020-02-03 14:54:50 +0100491 dev_err_once(dev,
492 "DMA addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
493 &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
494 WARN_ON_ONCE(1);
Christoph Hellwigcfced782019-01-04 18:20:05 +0100495 return DMA_MAPPING_ERROR;
496 }
497
498 return dma_addr;
499}
Christoph Hellwigcfced782019-01-04 18:20:05 +0100500
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +0100501int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
502 void *cpu_addr, dma_addr_t dma_addr, size_t size,
503 unsigned long attrs)
504{
505 struct page *page = dma_direct_to_page(dev, dma_addr);
506 int ret;
507
508 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
509 if (!ret)
510 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
511 return ret;
512}
513
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +0100514bool dma_direct_can_mmap(struct device *dev)
515{
516 return dev_is_dma_coherent(dev) ||
517 IS_ENABLED(CONFIG_DMA_NONCOHERENT_MMAP);
518}
519
520int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
521 void *cpu_addr, dma_addr_t dma_addr, size_t size,
522 unsigned long attrs)
523{
524 unsigned long user_count = vma_pages(vma);
525 unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
526 unsigned long pfn = PHYS_PFN(dma_to_phys(dev, dma_addr));
527 int ret = -ENXIO;
528
529 vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs);
530
531 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
532 return ret;
Christoph Hellwigfaf4ef82021-06-23 14:21:16 +0200533 if (dma_mmap_from_global_coherent(vma, cpu_addr, size, &ret))
534 return ret;
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +0100535
536 if (vma->vm_pgoff >= count || user_count > count - vma->vm_pgoff)
537 return -ENXIO;
538 return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
539 user_count << PAGE_SHIFT, vma->vm_page_prot);
540}
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +0100541
Christoph Hellwig1a9777a2017-12-24 15:04:32 +0100542int dma_direct_supported(struct device *dev, u64 mask)
543{
Christoph Hellwig91ef26f2020-02-03 18:11:10 +0100544 u64 min_mask = (max_pfn - 1) << PAGE_SHIFT;
Christoph Hellwig9d7a2242018-09-07 09:31:58 +0200545
Christoph Hellwig91ef26f2020-02-03 18:11:10 +0100546 /*
547 * Because 32-bit DMA masks are so common we expect every architecture
548 * to be able to satisfy them - either by not supporting more physical
549 * memory, or by providing a ZONE_DMA32. If neither is the case, the
550 * architecture needs to use an IOMMU instead of the direct mapping.
551 */
552 if (mask >= DMA_BIT_MASK(32))
553 return 1;
Christoph Hellwig9d7a2242018-09-07 09:31:58 +0200554
Lendacky, Thomasc92a54c2018-12-17 14:39:16 +0000555 /*
Christoph Hellwig5ceda742020-08-17 17:34:03 +0200556 * This check needs to be against the actual bit mask value, so use
557 * phys_to_dma_unencrypted() here so that the SME encryption mask isn't
Lendacky, Thomasc92a54c2018-12-17 14:39:16 +0000558 * part of the check.
559 */
Christoph Hellwig91ef26f2020-02-03 18:11:10 +0100560 if (IS_ENABLED(CONFIG_ZONE_DMA))
561 min_mask = min_t(u64, min_mask, DMA_BIT_MASK(zone_dma_bits));
Christoph Hellwig5ceda742020-08-17 17:34:03 +0200562 return mask >= phys_to_dma_unencrypted(dev, min_mask);
Christoph Hellwig1a9777a2017-12-24 15:04:32 +0100563}
Joerg Roedel133d6242019-02-07 12:59:15 +0100564
565size_t dma_direct_max_mapping_size(struct device *dev)
566{
Joerg Roedel133d6242019-02-07 12:59:15 +0100567 /* If SWIOTLB is active, use its maximum mapping size */
Claire Chang6f2beb22021-06-19 11:40:36 +0800568 if (is_swiotlb_active(dev) &&
Claire Chang903cd0f2021-06-24 23:55:20 +0800569 (dma_addressing_limited(dev) || is_swiotlb_force_bounce(dev)))
Christoph Hellwiga5008b52019-07-16 22:00:54 +0200570 return swiotlb_max_mapping_size(dev);
571 return SIZE_MAX;
Joerg Roedel133d6242019-02-07 12:59:15 +0100572}
Christoph Hellwig3aa9162502020-06-29 15:03:56 +0200573
574bool dma_direct_need_sync(struct device *dev, dma_addr_t dma_addr)
575{
576 return !dev_is_dma_coherent(dev) ||
Claire Chang7fd856a2021-06-19 11:40:35 +0800577 is_swiotlb_buffer(dev, dma_to_phys(dev, dma_addr));
Christoph Hellwig3aa9162502020-06-29 15:03:56 +0200578}
Jim Quinlane0d07272020-09-17 18:43:40 +0200579
580/**
581 * dma_direct_set_offset - Assign scalar offset for a single DMA range.
582 * @dev: device pointer; needed to "own" the alloced memory.
583 * @cpu_start: beginning of memory region covered by this offset.
584 * @dma_start: beginning of DMA/PCI region covered by this offset.
585 * @size: size of the region.
586 *
587 * This is for the simple case of a uniform offset which cannot
588 * be discovered by "dma-ranges".
589 *
590 * It returns -ENOMEM if out of memory, -EINVAL if a map
591 * already exists, 0 otherwise.
592 *
593 * Note: any call to this from a driver is a bug. The mapping needs
594 * to be described by the device tree or other firmware interfaces.
595 */
596int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start,
597 dma_addr_t dma_start, u64 size)
598{
599 struct bus_dma_region *map;
600 u64 offset = (u64)cpu_start - (u64)dma_start;
601
602 if (dev->dma_range_map) {
603 dev_err(dev, "attempt to add DMA range to existing map\n");
604 return -EINVAL;
605 }
606
607 if (!offset)
608 return 0;
609
610 map = kcalloc(2, sizeof(*map), GFP_KERNEL);
611 if (!map)
612 return -ENOMEM;
613 map[0].cpu_start = cpu_start;
614 map[0].dma_start = dma_start;
615 map[0].offset = offset;
616 map[0].size = size;
617 dev->dma_range_map = map;
618 return 0;
619}