blob: f737e334705945c938159e84e8b12fde2face7c1 [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 Hellwig26749b32020-06-15 08:52:31 +020078static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
Christoph Hellwig3773dfe2020-08-17 17:14:28 +020079 gfp_t gfp)
Christoph Hellwig002e6742018-01-09 16:30:23 +010080{
Christoph Hellwig90ae4092019-08-20 11:45:49 +090081 int node = dev_to_node(dev);
Christoph Hellwig080321d2017-12-22 11:51:44 +010082 struct page *page = NULL;
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +010083 u64 phys_limit;
Christoph Hellwig002e6742018-01-09 16:30:23 +010084
David Rientjes633d5fc2020-06-11 12:20:28 -070085 WARN_ON_ONCE(!PAGE_ALIGNED(size));
86
David Rientjesc84dc6e2020-04-14 17:04:55 -070087 gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
88 &phys_limit);
David Rientjes633d5fc2020-06-11 12:20:28 -070089 page = dma_alloc_contiguous(dev, size, gfp);
Christoph Hellwig90ae4092019-08-20 11:45:49 +090090 if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
David Rientjes633d5fc2020-06-11 12:20:28 -070091 dma_free_contiguous(dev, page, size);
Christoph Hellwig90ae4092019-08-20 11:45:49 +090092 page = NULL;
93 }
Christoph Hellwig95f18392018-01-09 23:40:57 +010094again:
Christoph Hellwig90ae4092019-08-20 11:45:49 +090095 if (!page)
David Rientjes633d5fc2020-06-11 12:20:28 -070096 page = alloc_pages_node(node, gfp, get_order(size));
Christoph Hellwig95f18392018-01-09 23:40:57 +010097 if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
Nicolin Chenb1d2dc02019-05-23 21:06:32 -070098 dma_free_contiguous(dev, page, size);
Christoph Hellwig95f18392018-01-09 23:40:57 +010099 page = NULL;
100
Takashi Iwaide7eab32018-04-16 17:18:19 +0200101 if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +0100102 phys_limit < DMA_BIT_MASK(64) &&
Takashi Iwaide7eab32018-04-16 17:18:19 +0200103 !(gfp & (GFP_DMA32 | GFP_DMA))) {
104 gfp |= GFP_DMA32;
105 goto again;
106 }
107
Christoph Hellwigfbce2512019-02-13 08:01:03 +0100108 if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) {
Christoph Hellwig95f18392018-01-09 23:40:57 +0100109 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
110 goto again;
111 }
112 }
113
Christoph Hellwigb18814e72018-11-04 17:27:56 +0100114 return page;
115}
116
Christoph Hellwig5b138c52020-10-07 11:06:09 +0200117static void *dma_direct_alloc_from_pool(struct device *dev, size_t size,
118 dma_addr_t *dma_handle, gfp_t gfp)
119{
120 struct page *page;
121 u64 phys_mask;
122 void *ret;
123
124 gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
125 &phys_mask);
126 page = dma_alloc_from_pool(dev, size, &ret, gfp, dma_coherent_ok);
127 if (!page)
128 return NULL;
129 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
130 return ret;
131}
132
Christoph Hellwig2f5388a22020-08-17 17:06:40 +0200133void *dma_direct_alloc(struct device *dev, size_t size,
Christoph Hellwigb18814e72018-11-04 17:27:56 +0100134 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
135{
136 struct page *page;
137 void *ret;
David Rientjes56fccf22020-06-11 12:20:30 -0700138 int err;
Christoph Hellwigb18814e72018-11-04 17:27:56 +0100139
David Rientjes633d5fc2020-06-11 12:20:28 -0700140 size = PAGE_ALIGN(size);
Christoph Hellwig3773dfe2020-08-17 17:14:28 +0200141 if (attrs & DMA_ATTR_NO_WARN)
142 gfp |= __GFP_NOWARN;
David Rientjes633d5fc2020-06-11 12:20:28 -0700143
Christoph Hellwig849face2020-10-07 11:04:08 +0200144 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
145 !force_dma_unencrypted(dev)) {
146 page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO);
147 if (!page)
148 return NULL;
149 /* remove any dirty cache lines on the kernel alias */
150 if (!PageHighMem(page))
151 arch_dma_prep_coherent(page, size);
152 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
153 /* return the page pointer as the opaque cookie */
154 return page;
155 }
156
157 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) &&
158 !IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
159 !dev_is_dma_coherent(dev))
160 return arch_dma_alloc(dev, size, dma_handle, gfp, attrs);
161
162 /*
163 * Remapping or decrypting memory may block. If either is required and
164 * we can't block, allocate the memory from the atomic pools.
165 */
166 if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
167 !gfpflags_allow_blocking(gfp) &&
168 (force_dma_unencrypted(dev) ||
169 (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && !dev_is_dma_coherent(dev))))
Christoph Hellwig5b138c52020-10-07 11:06:09 +0200170 return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp);
Christoph Hellwig3acac062019-10-29 11:06:32 +0100171
Christoph Hellwig3773dfe2020-08-17 17:14:28 +0200172 /* we always manually zero the memory once we are done */
173 page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO);
Christoph Hellwig080321d2017-12-22 11:51:44 +0100174 if (!page)
175 return NULL;
Christoph Hellwigb18814e72018-11-04 17:27:56 +0100176
Christoph Hellwig3acac062019-10-29 11:06:32 +0100177 if ((IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
Christoph Hellwig849face2020-10-07 11:04:08 +0200178 !dev_is_dma_coherent(dev)) ||
Christoph Hellwig3acac062019-10-29 11:06:32 +0100179 (IS_ENABLED(CONFIG_DMA_REMAP) && PageHighMem(page))) {
180 /* remove any dirty cache lines on the kernel alias */
David Rientjes633d5fc2020-06-11 12:20:28 -0700181 arch_dma_prep_coherent(page, size);
Christoph Hellwig3acac062019-10-29 11:06:32 +0100182
183 /* create a coherent mapping */
David Rientjes633d5fc2020-06-11 12:20:28 -0700184 ret = dma_common_contiguous_remap(page, size,
Christoph Hellwig3acac062019-10-29 11:06:32 +0100185 dma_pgprot(dev, PAGE_KERNEL, attrs),
186 __builtin_return_address(0));
Christoph Hellwig3d0fc342020-02-21 12:26:00 -0800187 if (!ret)
188 goto out_free_pages;
David Rientjes1a2b3352020-06-11 12:20:32 -0700189 if (force_dma_unencrypted(dev)) {
190 err = set_memory_decrypted((unsigned long)ret,
191 1 << get_order(size));
192 if (err)
193 goto out_free_pages;
194 }
Christoph Hellwig3acac062019-10-29 11:06:32 +0100195 memset(ret, 0, size);
196 goto done;
Christoph Hellwigd98849a2019-06-14 16:17:27 +0200197 }
198
Christoph Hellwig704f2c22018-09-22 20:47:26 +0200199 if (PageHighMem(page)) {
200 /*
201 * Depending on the cma= arguments and per-arch setup
Nicolin Chenb1d2dc02019-05-23 21:06:32 -0700202 * dma_alloc_contiguous could return highmem pages.
Christoph Hellwig704f2c22018-09-22 20:47:26 +0200203 * Without remapping there is no way to return them here,
204 * so log an error and fail.
205 */
206 dev_info(dev, "Rejecting highmem page from CMA.\n");
Christoph Hellwig3d0fc342020-02-21 12:26:00 -0800207 goto out_free_pages;
Christoph Hellwig704f2c22018-09-22 20:47:26 +0200208 }
209
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100210 ret = page_address(page);
David Rientjes56fccf22020-06-11 12:20:30 -0700211 if (force_dma_unencrypted(dev)) {
212 err = set_memory_decrypted((unsigned long)ret,
213 1 << get_order(size));
214 if (err)
215 goto out_free_pages;
216 }
Christoph Hellwig3acac062019-10-29 11:06:32 +0100217
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100218 memset(ret, 0, size);
Christoph Hellwigc30700d2019-06-03 08:43:51 +0200219
Christoph Hellwigfa7e2242020-02-21 15:55:43 -0800220 if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) &&
Christoph Hellwig849face2020-10-07 11:04:08 +0200221 !dev_is_dma_coherent(dev)) {
Christoph Hellwigc30700d2019-06-03 08:43:51 +0200222 arch_dma_prep_coherent(page, size);
Christoph Hellwigfa7e2242020-02-21 15:55:43 -0800223 ret = arch_dma_set_uncached(ret, size);
224 if (IS_ERR(ret))
David Rientjes96a539f2020-06-11 12:20:29 -0700225 goto out_encrypt_pages;
Christoph Hellwigc30700d2019-06-03 08:43:51 +0200226 }
Christoph Hellwig3acac062019-10-29 11:06:32 +0100227done:
Christoph Hellwig96eb89c2020-08-17 17:20:52 +0200228 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100229 return ret;
David Rientjes96a539f2020-06-11 12:20:29 -0700230
231out_encrypt_pages:
David Rientjes56fccf22020-06-11 12:20:30 -0700232 if (force_dma_unencrypted(dev)) {
233 err = set_memory_encrypted((unsigned long)page_address(page),
234 1 << get_order(size));
235 /* If memory cannot be re-encrypted, it must be leaked */
236 if (err)
237 return NULL;
238 }
Christoph Hellwig3d0fc342020-02-21 12:26:00 -0800239out_free_pages:
240 dma_free_contiguous(dev, page, size);
241 return NULL;
Christoph Hellwig002e6742018-01-09 16:30:23 +0100242}
243
Christoph Hellwig2f5388a22020-08-17 17:06:40 +0200244void dma_direct_free(struct device *dev, size_t size,
245 void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs)
Christoph Hellwig002e6742018-01-09 16:30:23 +0100246{
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100247 unsigned int page_order = get_order(size);
Christoph Hellwig080321d2017-12-22 11:51:44 +0100248
Christoph Hellwigcf14be02019-08-06 14:33:23 +0300249 if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
250 !force_dma_unencrypted(dev)) {
Christoph Hellwigd98849a2019-06-14 16:17:27 +0200251 /* cpu_addr is a struct page cookie, not a kernel address */
Christoph Hellwigacaade12019-10-29 09:57:09 +0100252 dma_free_contiguous(dev, cpu_addr, size);
Christoph Hellwigd98849a2019-06-14 16:17:27 +0200253 return;
254 }
255
Christoph Hellwig849face2020-10-07 11:04:08 +0200256 if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) &&
257 !IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
258 !dev_is_dma_coherent(dev)) {
259 arch_dma_free(dev, size, cpu_addr, dma_addr, attrs);
260 return;
261 }
262
263 /* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */
264 if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
265 dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size)))
266 return;
267
Tom Lendacky9087c372019-07-10 19:01:19 +0000268 if (force_dma_unencrypted(dev))
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100269 set_memory_encrypted((unsigned long)cpu_addr, 1 << page_order);
Christoph Hellwigc30700d2019-06-03 08:43:51 +0200270
Christoph Hellwig3acac062019-10-29 11:06:32 +0100271 if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr))
272 vunmap(cpu_addr);
Christoph Hellwig999a5d12020-02-21 12:35:05 -0800273 else if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED))
274 arch_dma_clear_uncached(cpu_addr, size);
Christoph Hellwig3acac062019-10-29 11:06:32 +0100275
276 dma_free_contiguous(dev, dma_direct_to_page(dev, dma_addr), size);
Christoph Hellwig002e6742018-01-09 16:30:23 +0100277}
278
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200279struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
280 dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
281{
282 struct page *page;
283 void *ret;
284
Christoph Hellwig849face2020-10-07 11:04:08 +0200285 if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
286 force_dma_unencrypted(dev) && !gfpflags_allow_blocking(gfp))
Christoph Hellwig5b138c52020-10-07 11:06:09 +0200287 return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp);
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200288
289 page = __dma_direct_alloc_pages(dev, size, gfp);
290 if (!page)
291 return NULL;
Christoph Hellwig08a89c22020-09-26 16:39:36 +0200292 if (PageHighMem(page)) {
293 /*
294 * Depending on the cma= arguments and per-arch setup
295 * dma_alloc_contiguous could return highmem pages.
296 * Without remapping there is no way to return them here,
297 * so log an error and fail.
298 */
299 dev_info(dev, "Rejecting highmem page from CMA.\n");
300 goto out_free_pages;
301 }
302
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200303 ret = page_address(page);
304 if (force_dma_unencrypted(dev)) {
305 if (set_memory_decrypted((unsigned long)ret,
306 1 << get_order(size)))
307 goto out_free_pages;
308 }
309 memset(ret, 0, size);
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200310 *dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
311 return page;
312out_free_pages:
313 dma_free_contiguous(dev, page, size);
314 return NULL;
315}
316
317void dma_direct_free_pages(struct device *dev, size_t size,
318 struct page *page, dma_addr_t dma_addr,
319 enum dma_data_direction dir)
320{
321 unsigned int page_order = get_order(size);
322 void *vaddr = page_address(page);
323
324 /* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */
Christoph Hellwig849face2020-10-07 11:04:08 +0200325 if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
Christoph Hellwigefa70f22020-09-01 13:34:33 +0200326 dma_free_from_pool(dev, vaddr, size))
327 return;
328
329 if (force_dma_unencrypted(dev))
330 set_memory_encrypted((unsigned long)vaddr, 1 << page_order);
331
332 dma_free_contiguous(dev, page, size);
333}
334
Christoph Hellwig55897af2018-12-03 11:43:54 +0100335#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
336 defined(CONFIG_SWIOTLB)
Christoph Hellwig55897af2018-12-03 11:43:54 +0100337void dma_direct_sync_sg_for_device(struct device *dev,
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200338 struct scatterlist *sgl, int nents, enum dma_data_direction dir)
339{
340 struct scatterlist *sg;
341 int i;
342
Christoph Hellwig55897af2018-12-03 11:43:54 +0100343 for_each_sg(sgl, sg, nents, i) {
Fugang Duan449fa542019-07-19 17:26:48 +0800344 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
345
346 if (unlikely(is_swiotlb_buffer(paddr)))
Christoph Hellwig80808d22021-03-01 08:44:26 +0100347 swiotlb_sync_single_for_device(dev, paddr, sg->length,
348 dir);
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200349
Christoph Hellwig55897af2018-12-03 11:43:54 +0100350 if (!dev_is_dma_coherent(dev))
Christoph Hellwig56e35f92019-11-07 18:03:11 +0100351 arch_sync_dma_for_device(paddr, sg->length,
Christoph Hellwig55897af2018-12-03 11:43:54 +0100352 dir);
353 }
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200354}
Christoph Hellwig17ac5242018-12-03 11:14:09 +0100355#endif
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200356
357#if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
Christoph Hellwig55897af2018-12-03 11:43:54 +0100358 defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) || \
359 defined(CONFIG_SWIOTLB)
Christoph Hellwig55897af2018-12-03 11:43:54 +0100360void dma_direct_sync_sg_for_cpu(struct device *dev,
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200361 struct scatterlist *sgl, int nents, enum dma_data_direction dir)
362{
363 struct scatterlist *sg;
364 int i;
365
Christoph Hellwig55897af2018-12-03 11:43:54 +0100366 for_each_sg(sgl, sg, nents, i) {
Fugang Duan449fa542019-07-19 17:26:48 +0800367 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
368
Christoph Hellwig55897af2018-12-03 11:43:54 +0100369 if (!dev_is_dma_coherent(dev))
Christoph Hellwig56e35f92019-11-07 18:03:11 +0100370 arch_sync_dma_for_cpu(paddr, sg->length, dir);
Fugang Duan449fa542019-07-19 17:26:48 +0800371
372 if (unlikely(is_swiotlb_buffer(paddr)))
Christoph Hellwig80808d22021-03-01 08:44:26 +0100373 swiotlb_sync_single_for_cpu(dev, paddr, sg->length,
374 dir);
Christoph Hellwigabdaf112020-08-17 16:41:50 +0200375
376 if (dir == DMA_FROM_DEVICE)
377 arch_dma_mark_clean(paddr, sg->length);
Christoph Hellwig55897af2018-12-03 11:43:54 +0100378 }
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200379
Christoph Hellwig55897af2018-12-03 11:43:54 +0100380 if (!dev_is_dma_coherent(dev))
Christoph Hellwig56e35f92019-11-07 18:03:11 +0100381 arch_sync_dma_for_cpu_all();
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200382}
383
Christoph Hellwig55897af2018-12-03 11:43:54 +0100384void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200385 int nents, enum dma_data_direction dir, unsigned long attrs)
386{
Christoph Hellwig55897af2018-12-03 11:43:54 +0100387 struct scatterlist *sg;
388 int i;
389
390 for_each_sg(sgl, sg, nents, i)
391 dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir,
392 attrs);
Christoph Hellwigbc3ec752018-09-08 11:22:43 +0200393}
394#endif
395
Christoph Hellwig782e6762018-04-16 15:24:51 +0200396int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
397 enum dma_data_direction dir, unsigned long attrs)
Christoph Hellwig002e6742018-01-09 16:30:23 +0100398{
399 int i;
400 struct scatterlist *sg;
401
402 for_each_sg(sgl, sg, nents, i) {
Christoph Hellwig17ac5242018-12-03 11:14:09 +0100403 sg->dma_address = dma_direct_map_page(dev, sg_page(sg),
404 sg->offset, sg->length, dir, attrs);
405 if (sg->dma_address == DMA_MAPPING_ERROR)
Christoph Hellwig55897af2018-12-03 11:43:54 +0100406 goto out_unmap;
Christoph Hellwig002e6742018-01-09 16:30:23 +0100407 sg_dma_len(sg) = sg->length;
408 }
409
410 return nents;
Christoph Hellwig55897af2018-12-03 11:43:54 +0100411
412out_unmap:
413 dma_direct_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
414 return 0;
Christoph Hellwig002e6742018-01-09 16:30:23 +0100415}
416
Christoph Hellwigcfced782019-01-04 18:20:05 +0100417dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr,
418 size_t size, enum dma_data_direction dir, unsigned long attrs)
419{
420 dma_addr_t dma_addr = paddr;
421
Christoph Hellwig68a33b12019-11-19 17:38:58 +0100422 if (unlikely(!dma_capable(dev, dma_addr, size, false))) {
Christoph Hellwig75467ee2020-02-03 14:54:50 +0100423 dev_err_once(dev,
424 "DMA addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
425 &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
426 WARN_ON_ONCE(1);
Christoph Hellwigcfced782019-01-04 18:20:05 +0100427 return DMA_MAPPING_ERROR;
428 }
429
430 return dma_addr;
431}
Christoph Hellwigcfced782019-01-04 18:20:05 +0100432
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +0100433int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
434 void *cpu_addr, dma_addr_t dma_addr, size_t size,
435 unsigned long attrs)
436{
437 struct page *page = dma_direct_to_page(dev, dma_addr);
438 int ret;
439
440 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
441 if (!ret)
442 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
443 return ret;
444}
445
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +0100446bool dma_direct_can_mmap(struct device *dev)
447{
448 return dev_is_dma_coherent(dev) ||
449 IS_ENABLED(CONFIG_DMA_NONCOHERENT_MMAP);
450}
451
452int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
453 void *cpu_addr, dma_addr_t dma_addr, size_t size,
454 unsigned long attrs)
455{
456 unsigned long user_count = vma_pages(vma);
457 unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
458 unsigned long pfn = PHYS_PFN(dma_to_phys(dev, dma_addr));
459 int ret = -ENXIO;
460
461 vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs);
462
463 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
464 return ret;
465
466 if (vma->vm_pgoff >= count || user_count > count - vma->vm_pgoff)
467 return -ENXIO;
468 return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
469 user_count << PAGE_SHIFT, vma->vm_page_prot);
470}
Christoph Hellwig34dc0ea2019-10-29 11:01:37 +0100471
Christoph Hellwig1a9777a2017-12-24 15:04:32 +0100472int dma_direct_supported(struct device *dev, u64 mask)
473{
Christoph Hellwig91ef26f2020-02-03 18:11:10 +0100474 u64 min_mask = (max_pfn - 1) << PAGE_SHIFT;
Christoph Hellwig9d7a2242018-09-07 09:31:58 +0200475
Christoph Hellwig91ef26f2020-02-03 18:11:10 +0100476 /*
477 * Because 32-bit DMA masks are so common we expect every architecture
478 * to be able to satisfy them - either by not supporting more physical
479 * memory, or by providing a ZONE_DMA32. If neither is the case, the
480 * architecture needs to use an IOMMU instead of the direct mapping.
481 */
482 if (mask >= DMA_BIT_MASK(32))
483 return 1;
Christoph Hellwig9d7a2242018-09-07 09:31:58 +0200484
Lendacky, Thomasc92a54c2018-12-17 14:39:16 +0000485 /*
Christoph Hellwig5ceda742020-08-17 17:34:03 +0200486 * This check needs to be against the actual bit mask value, so use
487 * phys_to_dma_unencrypted() here so that the SME encryption mask isn't
Lendacky, Thomasc92a54c2018-12-17 14:39:16 +0000488 * part of the check.
489 */
Christoph Hellwig91ef26f2020-02-03 18:11:10 +0100490 if (IS_ENABLED(CONFIG_ZONE_DMA))
491 min_mask = min_t(u64, min_mask, DMA_BIT_MASK(zone_dma_bits));
Christoph Hellwig5ceda742020-08-17 17:34:03 +0200492 return mask >= phys_to_dma_unencrypted(dev, min_mask);
Christoph Hellwig1a9777a2017-12-24 15:04:32 +0100493}
Joerg Roedel133d6242019-02-07 12:59:15 +0100494
495size_t dma_direct_max_mapping_size(struct device *dev)
496{
Joerg Roedel133d6242019-02-07 12:59:15 +0100497 /* If SWIOTLB is active, use its maximum mapping size */
Christoph Hellwiga5008b52019-07-16 22:00:54 +0200498 if (is_swiotlb_active() &&
499 (dma_addressing_limited(dev) || swiotlb_force == SWIOTLB_FORCE))
500 return swiotlb_max_mapping_size(dev);
501 return SIZE_MAX;
Joerg Roedel133d6242019-02-07 12:59:15 +0100502}
Christoph Hellwig3aa9162502020-06-29 15:03:56 +0200503
504bool dma_direct_need_sync(struct device *dev, dma_addr_t dma_addr)
505{
506 return !dev_is_dma_coherent(dev) ||
507 is_swiotlb_buffer(dma_to_phys(dev, dma_addr));
508}
Jim Quinlane0d07272020-09-17 18:43:40 +0200509
510/**
511 * dma_direct_set_offset - Assign scalar offset for a single DMA range.
512 * @dev: device pointer; needed to "own" the alloced memory.
513 * @cpu_start: beginning of memory region covered by this offset.
514 * @dma_start: beginning of DMA/PCI region covered by this offset.
515 * @size: size of the region.
516 *
517 * This is for the simple case of a uniform offset which cannot
518 * be discovered by "dma-ranges".
519 *
520 * It returns -ENOMEM if out of memory, -EINVAL if a map
521 * already exists, 0 otherwise.
522 *
523 * Note: any call to this from a driver is a bug. The mapping needs
524 * to be described by the device tree or other firmware interfaces.
525 */
526int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start,
527 dma_addr_t dma_start, u64 size)
528{
529 struct bus_dma_region *map;
530 u64 offset = (u64)cpu_start - (u64)dma_start;
531
532 if (dev->dma_range_map) {
533 dev_err(dev, "attempt to add DMA range to existing map\n");
534 return -EINVAL;
535 }
536
537 if (!offset)
538 return 0;
539
540 map = kcalloc(2, sizeof(*map), GFP_KERNEL);
541 if (!map)
542 return -ENOMEM;
543 map[0].cpu_start = cpu_start;
544 map[0].dma_start = dma_start;
545 map[0].offset = offset;
546 map[0].size = size;
547 dev->dma_range_map = map;
548 return 0;
549}