• 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 EXPORT_SYMBOL_GPL(dma_direct_get_required_mask);
47 
dma_direct_optimal_gfp_mask(struct device * dev,u64 dma_mask,u64 * phys_limit)48 static gfp_t dma_direct_optimal_gfp_mask(struct device *dev, u64 dma_mask,
49 				  u64 *phys_limit)
50 {
51 	u64 dma_limit = min_not_zero(dma_mask, dev->bus_dma_limit);
52 
53 	/*
54 	 * Optimistically try the zone that the physical address mask falls
55 	 * into first.  If that returns memory that isn't actually addressable
56 	 * we will fallback to the next lower zone and try again.
57 	 *
58 	 * Note that GFP_DMA32 and GFP_DMA are no ops without the corresponding
59 	 * zones.
60 	 */
61 	*phys_limit = dma_to_phys(dev, dma_limit);
62 	if (*phys_limit <= DMA_BIT_MASK(zone_dma_bits))
63 		return GFP_DMA;
64 	if (*phys_limit <= DMA_BIT_MASK(32) &&
65 		!zone_dma32_are_empty())
66 		return GFP_DMA32;
67 	return 0;
68 }
69 
dma_coherent_ok(struct device * dev,phys_addr_t phys,size_t size)70 static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
71 {
72 	dma_addr_t dma_addr = phys_to_dma_direct(dev, phys);
73 
74 	if (dma_addr == DMA_MAPPING_ERROR)
75 		return false;
76 	return dma_addr + size - 1 <=
77 		min_not_zero(dev->coherent_dma_mask, dev->bus_dma_limit);
78 }
79 
dma_set_decrypted(struct device * dev,void * vaddr,size_t size)80 static int dma_set_decrypted(struct device *dev, void *vaddr, size_t size)
81 {
82 	if (!force_dma_unencrypted(dev))
83 		return 0;
84 	return set_memory_decrypted((unsigned long)vaddr, PFN_UP(size));
85 }
86 
dma_set_encrypted(struct device * dev,void * vaddr,size_t size)87 static int dma_set_encrypted(struct device *dev, void *vaddr, size_t size)
88 {
89 	int ret;
90 
91 	if (!force_dma_unencrypted(dev))
92 		return 0;
93 	ret = set_memory_encrypted((unsigned long)vaddr, PFN_UP(size));
94 	if (ret)
95 		pr_warn_ratelimited("leaking DMA memory that can't be re-encrypted\n");
96 	return ret;
97 }
98 
__dma_direct_free_pages(struct device * dev,struct page * page,size_t size)99 static void __dma_direct_free_pages(struct device *dev, struct page *page,
100 				    size_t size)
101 {
102 	if (swiotlb_free(dev, page, size))
103 		return;
104 	dma_free_contiguous(dev, page, size);
105 }
106 
dma_direct_alloc_swiotlb(struct device * dev,size_t size)107 static struct page *dma_direct_alloc_swiotlb(struct device *dev, size_t size)
108 {
109 	struct page *page = swiotlb_alloc(dev, size);
110 
111 	if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
112 		swiotlb_free(dev, page, size);
113 		return NULL;
114 	}
115 
116 	return page;
117 }
118 
__dma_direct_alloc_pages(struct device * dev,size_t size,gfp_t gfp,bool allow_highmem)119 static struct page *__dma_direct_alloc_pages(struct device *dev, size_t size,
120 		gfp_t gfp, bool allow_highmem)
121 {
122 	int node = dev_to_node(dev);
123 	struct page *page = NULL;
124 	u64 phys_limit;
125 
126 	WARN_ON_ONCE(!PAGE_ALIGNED(size));
127 
128 	if (is_swiotlb_for_alloc(dev))
129 		return dma_direct_alloc_swiotlb(dev, size);
130 
131 	gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
132 					   &phys_limit);
133 	page = dma_alloc_contiguous(dev, size, gfp);
134 	if (page) {
135 		if (!dma_coherent_ok(dev, page_to_phys(page), size) ||
136 		    (!allow_highmem && PageHighMem(page))) {
137 			dma_free_contiguous(dev, page, size);
138 			page = NULL;
139 		}
140 	}
141 again:
142 	if (!page)
143 		page = alloc_pages_node(node, gfp, get_order(size));
144 	if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
145 		dma_free_contiguous(dev, page, size);
146 		page = NULL;
147 
148 		if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
149 		    phys_limit < DMA_BIT_MASK(64) &&
150 		    !(gfp & (GFP_DMA32 | GFP_DMA)) &&
151 		    !zone_dma32_are_empty()) {
152 			gfp |= GFP_DMA32;
153 			goto again;
154 		}
155 
156 		if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) {
157 			gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
158 			goto again;
159 		}
160 	}
161 
162 	return page;
163 }
164 
165 /*
166  * Check if a potentially blocking operations needs to dip into the atomic
167  * pools for the given device/gfp.
168  */
dma_direct_use_pool(struct device * dev,gfp_t gfp)169 static bool dma_direct_use_pool(struct device *dev, gfp_t gfp)
170 {
171 	return !gfpflags_allow_blocking(gfp) && !is_swiotlb_for_alloc(dev);
172 }
173 
dma_direct_alloc_from_pool(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp)174 static void *dma_direct_alloc_from_pool(struct device *dev, size_t size,
175 		dma_addr_t *dma_handle, gfp_t gfp)
176 {
177 	struct page *page;
178 	u64 phys_mask;
179 	void *ret;
180 
181 	if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_DMA_COHERENT_POOL)))
182 		return NULL;
183 
184 	gfp |= dma_direct_optimal_gfp_mask(dev, dev->coherent_dma_mask,
185 					   &phys_mask);
186 	page = dma_alloc_from_pool(dev, size, &ret, gfp, dma_coherent_ok);
187 	if (!page)
188 		return NULL;
189 	*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
190 	return ret;
191 }
192 
dma_direct_alloc_no_mapping(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp)193 static void *dma_direct_alloc_no_mapping(struct device *dev, size_t size,
194 		dma_addr_t *dma_handle, gfp_t gfp)
195 {
196 	struct page *page;
197 
198 	page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true);
199 	if (!page)
200 		return NULL;
201 
202 	/* remove any dirty cache lines on the kernel alias */
203 	if (!PageHighMem(page))
204 		arch_dma_prep_coherent(page, size);
205 
206 	/* return the page pointer as the opaque cookie */
207 	*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
208 	return page;
209 }
210 
dma_direct_alloc(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp,unsigned long attrs)211 void *dma_direct_alloc(struct device *dev, size_t size,
212 		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
213 {
214 	bool remap = false, set_uncached = false;
215 	struct page *page;
216 	void *ret;
217 
218 	size = PAGE_ALIGN(size);
219 	if (attrs & DMA_ATTR_NO_WARN)
220 		gfp |= __GFP_NOWARN;
221 
222 	if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
223 	    !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev))
224 		return dma_direct_alloc_no_mapping(dev, size, dma_handle, gfp);
225 
226 	if (!dev_is_dma_coherent(dev)) {
227 		/*
228 		 * Fallback to the arch handler if it exists.  This should
229 		 * eventually go away.
230 		 */
231 		if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) &&
232 		    !IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
233 		    !IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) &&
234 		    !is_swiotlb_for_alloc(dev))
235 			return arch_dma_alloc(dev, size, dma_handle, gfp,
236 					      attrs);
237 
238 		/*
239 		 * If there is a global pool, always allocate from it for
240 		 * non-coherent devices.
241 		 */
242 		if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL))
243 			return dma_alloc_from_global_coherent(dev, size,
244 					dma_handle);
245 
246 		/*
247 		 * Otherwise remap if the architecture is asking for it.  But
248 		 * given that remapping memory is a blocking operation we'll
249 		 * instead have to dip into the atomic pools.
250 		 */
251 		remap = IS_ENABLED(CONFIG_DMA_DIRECT_REMAP);
252 		if (remap) {
253 			if (dma_direct_use_pool(dev, gfp))
254 				return dma_direct_alloc_from_pool(dev, size,
255 						dma_handle, gfp);
256 		} else {
257 			if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED))
258 				return NULL;
259 			set_uncached = true;
260 		}
261 	}
262 
263 	/*
264 	 * Decrypting memory may block, so allocate the memory from the atomic
265 	 * pools if we can't block.
266 	 */
267 	if (force_dma_unencrypted(dev) && dma_direct_use_pool(dev, gfp))
268 		return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp);
269 
270 	/* we always manually zero the memory once we are done */
271 	page = __dma_direct_alloc_pages(dev, size, gfp & ~__GFP_ZERO, true);
272 	if (!page)
273 		return NULL;
274 
275 	/*
276 	 * dma_alloc_contiguous can return highmem pages depending on a
277 	 * combination the cma= arguments and per-arch setup.  These need to be
278 	 * remapped to return a kernel virtual address.
279 	 */
280 	if (PageHighMem(page)) {
281 		remap = true;
282 		set_uncached = false;
283 	}
284 
285 	if (remap) {
286 		pgprot_t prot = dma_pgprot(dev, PAGE_KERNEL, attrs);
287 
288 		if (force_dma_unencrypted(dev))
289 			prot = pgprot_decrypted(prot);
290 
291 		/* remove any dirty cache lines on the kernel alias */
292 		arch_dma_prep_coherent(page, size);
293 
294 		/* create a coherent mapping */
295 		ret = dma_common_contiguous_remap(page, size, prot,
296 				__builtin_return_address(0));
297 		if (!ret)
298 			goto out_free_pages;
299 	} else {
300 		ret = page_address(page);
301 		if (dma_set_decrypted(dev, ret, size))
302 			goto out_free_pages;
303 	}
304 
305 	memset(ret, 0, size);
306 
307 	if (set_uncached) {
308 		arch_dma_prep_coherent(page, size);
309 		ret = arch_dma_set_uncached(ret, size);
310 		if (IS_ERR(ret))
311 			goto out_encrypt_pages;
312 	}
313 
314 	*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
315 	return ret;
316 
317 out_encrypt_pages:
318 	if (dma_set_encrypted(dev, page_address(page), size))
319 		return NULL;
320 out_free_pages:
321 	__dma_direct_free_pages(dev, page, size);
322 	return NULL;
323 }
324 EXPORT_SYMBOL_GPL(dma_direct_alloc);
325 
dma_direct_free(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_addr,unsigned long attrs)326 void dma_direct_free(struct device *dev, size_t size,
327 		void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs)
328 {
329 	unsigned int page_order = get_order(size);
330 
331 	if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
332 	    !force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) {
333 		/* cpu_addr is a struct page cookie, not a kernel address */
334 		dma_free_contiguous(dev, cpu_addr, size);
335 		return;
336 	}
337 
338 	if (!IS_ENABLED(CONFIG_ARCH_HAS_DMA_SET_UNCACHED) &&
339 	    !IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
340 	    !IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) &&
341 	    !dev_is_dma_coherent(dev) &&
342 	    !is_swiotlb_for_alloc(dev)) {
343 		arch_dma_free(dev, size, cpu_addr, dma_addr, attrs);
344 		return;
345 	}
346 
347 	if (IS_ENABLED(CONFIG_DMA_GLOBAL_POOL) &&
348 	    !dev_is_dma_coherent(dev)) {
349 		if (!dma_release_from_global_coherent(page_order, cpu_addr))
350 			WARN_ON_ONCE(1);
351 		return;
352 	}
353 
354 	/* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */
355 	if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
356 	    dma_free_from_pool(dev, cpu_addr, PAGE_ALIGN(size)))
357 		return;
358 
359 	if (is_vmalloc_addr(cpu_addr)) {
360 		vunmap(cpu_addr);
361 	} else {
362 		if (IS_ENABLED(CONFIG_ARCH_HAS_DMA_CLEAR_UNCACHED))
363 			arch_dma_clear_uncached(cpu_addr, size);
364 		if (dma_set_encrypted(dev, cpu_addr, size))
365 			return;
366 	}
367 
368 	__dma_direct_free_pages(dev, dma_direct_to_page(dev, dma_addr), size);
369 }
370 EXPORT_SYMBOL_GPL(dma_direct_free);
371 
dma_direct_alloc_pages(struct device * dev,size_t size,dma_addr_t * dma_handle,enum dma_data_direction dir,gfp_t gfp)372 struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
373 		dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
374 {
375 	struct page *page;
376 	void *ret;
377 
378 	if (force_dma_unencrypted(dev) && dma_direct_use_pool(dev, gfp))
379 		return dma_direct_alloc_from_pool(dev, size, dma_handle, gfp);
380 
381 	page = __dma_direct_alloc_pages(dev, size, gfp, false);
382 	if (!page)
383 		return NULL;
384 
385 	ret = page_address(page);
386 	if (dma_set_decrypted(dev, ret, size))
387 		goto out_free_pages;
388 	memset(ret, 0, size);
389 	*dma_handle = phys_to_dma_direct(dev, page_to_phys(page));
390 	return page;
391 out_free_pages:
392 	__dma_direct_free_pages(dev, page, size);
393 	return NULL;
394 }
395 
dma_direct_free_pages(struct device * dev,size_t size,struct page * page,dma_addr_t dma_addr,enum dma_data_direction dir)396 void dma_direct_free_pages(struct device *dev, size_t size,
397 		struct page *page, dma_addr_t dma_addr,
398 		enum dma_data_direction dir)
399 {
400 	void *vaddr = page_address(page);
401 
402 	/* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */
403 	if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
404 	    dma_free_from_pool(dev, vaddr, size))
405 		return;
406 
407 	if (dma_set_encrypted(dev, vaddr, size))
408 		return;
409 	__dma_direct_free_pages(dev, page, size);
410 }
411 
412 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
413     defined(CONFIG_SWIOTLB)
dma_direct_sync_sg_for_device(struct device * dev,struct scatterlist * sgl,int nents,enum dma_data_direction dir)414 void dma_direct_sync_sg_for_device(struct device *dev,
415 		struct scatterlist *sgl, int nents, enum dma_data_direction dir)
416 {
417 	struct scatterlist *sg;
418 	int i;
419 
420 	for_each_sg(sgl, sg, nents, i) {
421 		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
422 
423 		if (unlikely(is_swiotlb_buffer(dev, paddr)))
424 			swiotlb_sync_single_for_device(dev, paddr, sg->length,
425 						       dir);
426 
427 		if (!dev_is_dma_coherent(dev))
428 			arch_sync_dma_for_device(paddr, sg->length,
429 					dir);
430 	}
431 }
432 #endif
433 
434 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
435     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) || \
436     defined(CONFIG_SWIOTLB)
dma_direct_sync_sg_for_cpu(struct device * dev,struct scatterlist * sgl,int nents,enum dma_data_direction dir)437 void dma_direct_sync_sg_for_cpu(struct device *dev,
438 		struct scatterlist *sgl, int nents, enum dma_data_direction dir)
439 {
440 	struct scatterlist *sg;
441 	int i;
442 
443 	for_each_sg(sgl, sg, nents, i) {
444 		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
445 
446 		if (!dev_is_dma_coherent(dev))
447 			arch_sync_dma_for_cpu(paddr, sg->length, dir);
448 
449 		if (unlikely(is_swiotlb_buffer(dev, paddr)))
450 			swiotlb_sync_single_for_cpu(dev, paddr, sg->length,
451 						    dir);
452 
453 		if (dir == DMA_FROM_DEVICE)
454 			arch_dma_mark_clean(paddr, sg->length);
455 	}
456 
457 	if (!dev_is_dma_coherent(dev))
458 		arch_sync_dma_for_cpu_all();
459 }
460 
461 /*
462  * Unmaps segments, except for ones marked as pci_p2pdma which do not
463  * require any further action as they contain a bus address.
464  */
dma_direct_unmap_sg(struct device * dev,struct scatterlist * sgl,int nents,enum dma_data_direction dir,unsigned long attrs)465 void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,
466 		int nents, enum dma_data_direction dir, unsigned long attrs)
467 {
468 	struct scatterlist *sg;
469 	int i;
470 
471 	for_each_sg(sgl,  sg, nents, i) {
472 		if (sg_is_dma_bus_address(sg))
473 			sg_dma_unmark_bus_address(sg);
474 		else
475 			dma_direct_unmap_page(dev, sg->dma_address,
476 					      sg_dma_len(sg), dir, attrs);
477 	}
478 }
479 #endif
480 
dma_direct_map_sg(struct device * dev,struct scatterlist * sgl,int nents,enum dma_data_direction dir,unsigned long attrs)481 int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
482 		enum dma_data_direction dir, unsigned long attrs)
483 {
484 	struct pci_p2pdma_map_state p2pdma_state = {};
485 	enum pci_p2pdma_map_type map;
486 	struct scatterlist *sg;
487 	int i, ret;
488 
489 	for_each_sg(sgl, sg, nents, i) {
490 		if (is_pci_p2pdma_page(sg_page(sg))) {
491 			map = pci_p2pdma_map_segment(&p2pdma_state, dev, sg);
492 			switch (map) {
493 			case PCI_P2PDMA_MAP_BUS_ADDR:
494 				continue;
495 			case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
496 				/*
497 				 * Any P2P mapping that traverses the PCI
498 				 * host bridge must be mapped with CPU physical
499 				 * address and not PCI bus addresses. This is
500 				 * done with dma_direct_map_page() below.
501 				 */
502 				break;
503 			default:
504 				ret = -EREMOTEIO;
505 				goto out_unmap;
506 			}
507 		}
508 
509 		sg->dma_address = dma_direct_map_page(dev, sg_page(sg),
510 				sg->offset, sg->length, dir, attrs);
511 		if (sg->dma_address == DMA_MAPPING_ERROR) {
512 			ret = -EIO;
513 			goto out_unmap;
514 		}
515 		sg_dma_len(sg) = sg->length;
516 	}
517 
518 	return nents;
519 
520 out_unmap:
521 	dma_direct_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
522 	return ret;
523 }
524 
dma_direct_map_resource(struct device * dev,phys_addr_t paddr,size_t size,enum dma_data_direction dir,unsigned long attrs)525 dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr,
526 		size_t size, enum dma_data_direction dir, unsigned long attrs)
527 {
528 	dma_addr_t dma_addr = paddr;
529 
530 	if (unlikely(!dma_capable(dev, dma_addr, size, false))) {
531 		dev_err_once(dev,
532 			     "DMA addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
533 			     &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
534 		WARN_ON_ONCE(1);
535 		return DMA_MAPPING_ERROR;
536 	}
537 
538 	return dma_addr;
539 }
540 
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)541 int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
542 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
543 		unsigned long attrs)
544 {
545 	struct page *page = dma_direct_to_page(dev, dma_addr);
546 	int ret;
547 
548 	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
549 	if (!ret)
550 		sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
551 	return ret;
552 }
553 
dma_direct_can_mmap(struct device * dev)554 bool dma_direct_can_mmap(struct device *dev)
555 {
556 	return dev_is_dma_coherent(dev) ||
557 		IS_ENABLED(CONFIG_DMA_NONCOHERENT_MMAP);
558 }
559 
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)560 int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
561 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
562 		unsigned long attrs)
563 {
564 	unsigned long user_count = vma_pages(vma);
565 	unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
566 	unsigned long pfn = PHYS_PFN(dma_to_phys(dev, dma_addr));
567 	int ret = -ENXIO;
568 
569 	vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs);
570 	if (force_dma_unencrypted(dev))
571 		vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
572 
573 	if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
574 		return ret;
575 	if (dma_mmap_from_global_coherent(vma, cpu_addr, size, &ret))
576 		return ret;
577 
578 	if (vma->vm_pgoff >= count || user_count > count - vma->vm_pgoff)
579 		return -ENXIO;
580 	return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
581 			user_count << PAGE_SHIFT, vma->vm_page_prot);
582 }
583 
dma_direct_supported(struct device * dev,u64 mask)584 int dma_direct_supported(struct device *dev, u64 mask)
585 {
586 	u64 min_mask = (max_pfn - 1) << PAGE_SHIFT;
587 
588 	/*
589 	 * Because 32-bit DMA masks are so common we expect every architecture
590 	 * to be able to satisfy them - either by not supporting more physical
591 	 * memory, or by providing a ZONE_DMA32.  If neither is the case, the
592 	 * architecture needs to use an IOMMU instead of the direct mapping.
593 	 */
594 	if (mask >= DMA_BIT_MASK(32))
595 		return 1;
596 
597 	/*
598 	 * This check needs to be against the actual bit mask value, so use
599 	 * phys_to_dma_unencrypted() here so that the SME encryption mask isn't
600 	 * part of the check.
601 	 */
602 	if (IS_ENABLED(CONFIG_ZONE_DMA))
603 		min_mask = min_t(u64, min_mask, DMA_BIT_MASK(zone_dma_bits));
604 	return mask >= phys_to_dma_unencrypted(dev, min_mask);
605 }
606 
dma_direct_max_mapping_size(struct device * dev)607 size_t dma_direct_max_mapping_size(struct device *dev)
608 {
609 	/* If SWIOTLB is active, use its maximum mapping size */
610 	if (is_swiotlb_active(dev) &&
611 	    (dma_addressing_limited(dev) || is_swiotlb_force_bounce(dev)))
612 		return swiotlb_max_mapping_size(dev);
613 	return SIZE_MAX;
614 }
615 
dma_direct_need_sync(struct device * dev,dma_addr_t dma_addr)616 bool dma_direct_need_sync(struct device *dev, dma_addr_t dma_addr)
617 {
618 	return !dev_is_dma_coherent(dev) ||
619 	       is_swiotlb_buffer(dev, dma_to_phys(dev, dma_addr));
620 }
621 
622 /**
623  * dma_direct_set_offset - Assign scalar offset for a single DMA range.
624  * @dev:	device pointer; needed to "own" the alloced memory.
625  * @cpu_start:  beginning of memory region covered by this offset.
626  * @dma_start:  beginning of DMA/PCI region covered by this offset.
627  * @size:	size of the region.
628  *
629  * This is for the simple case of a uniform offset which cannot
630  * be discovered by "dma-ranges".
631  *
632  * It returns -ENOMEM if out of memory, -EINVAL if a map
633  * already exists, 0 otherwise.
634  *
635  * Note: any call to this from a driver is a bug.  The mapping needs
636  * to be described by the device tree or other firmware interfaces.
637  */
dma_direct_set_offset(struct device * dev,phys_addr_t cpu_start,dma_addr_t dma_start,u64 size)638 int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start,
639 			 dma_addr_t dma_start, u64 size)
640 {
641 	struct bus_dma_region *map;
642 	u64 offset = (u64)cpu_start - (u64)dma_start;
643 
644 	if (dev->dma_range_map) {
645 		dev_err(dev, "attempt to add DMA range to existing map\n");
646 		return -EINVAL;
647 	}
648 
649 	if (!offset)
650 		return 0;
651 
652 	map = kcalloc(2, sizeof(*map), GFP_KERNEL);
653 	if (!map)
654 		return -ENOMEM;
655 	map[0].cpu_start = cpu_start;
656 	map[0].dma_start = dma_start;
657 	map[0].offset = offset;
658 	map[0].size = size;
659 	dev->dma_range_map = map;
660 	return 0;
661 }
662