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