blob: c0bba30fef0ac1f8bda32af158b5596fcc14ce09 [file] [log] [blame]
Christoph Hellwig002e6742018-01-09 16:30:23 +01001// SPDX-License-Identifier: GPL-2.0
2/*
Christoph Hellwig2e86a042017-12-22 11:29:51 +01003 * DMA operations that map physical memory directly without using an IOMMU or
4 * flushing caches.
Christoph Hellwig002e6742018-01-09 16:30:23 +01005 */
6#include <linux/export.h>
7#include <linux/mm.h>
Christoph Hellwig2e86a042017-12-22 11:29:51 +01008#include <linux/dma-direct.h>
Christoph Hellwig002e6742018-01-09 16:30:23 +01009#include <linux/scatterlist.h>
Christoph Hellwig080321d2017-12-22 11:51:44 +010010#include <linux/dma-contiguous.h>
Christoph Hellwig002e6742018-01-09 16:30:23 +010011#include <linux/pfn.h>
Christoph Hellwigc10f07a2018-03-19 11:38:25 +010012#include <linux/set_memory.h>
Christoph Hellwig002e6742018-01-09 16:30:23 +010013
Christoph Hellwig27975962018-01-09 16:30:47 +010014#define DIRECT_MAPPING_ERROR 0
15
Christoph Hellwigc61e9632018-01-09 23:39:03 +010016/*
17 * Most architectures use ZONE_DMA for the first 16 Megabytes, but
18 * some use it for entirely different regions:
19 */
20#ifndef ARCH_ZONE_DMA_BITS
21#define ARCH_ZONE_DMA_BITS 24
22#endif
23
Christoph Hellwigc10f07a2018-03-19 11:38:25 +010024/*
25 * For AMD SEV all DMA must be to unencrypted addresses.
26 */
27static inline bool force_dma_unencrypted(void)
28{
29 return sev_active();
30}
31
Christoph Hellwig27975962018-01-09 16:30:47 +010032static bool
33check_addr(struct device *dev, dma_addr_t dma_addr, size_t size,
34 const char *caller)
35{
36 if (unlikely(dev && !dma_capable(dev, dma_addr, size))) {
37 if (*dev->dma_mask >= DMA_BIT_MASK(32)) {
38 dev_err(dev,
39 "%s: overflow %pad+%zu of device mask %llx\n",
40 caller, &dma_addr, size, *dev->dma_mask);
41 }
42 return false;
43 }
44 return true;
45}
46
Christoph Hellwig95f18392018-01-09 23:40:57 +010047static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
48{
Christoph Hellwigc10f07a2018-03-19 11:38:25 +010049 dma_addr_t addr = force_dma_unencrypted() ?
50 __phys_to_dma(dev, phys) : phys_to_dma(dev, phys);
51 return addr + size - 1 <= dev->coherent_dma_mask;
Christoph Hellwig95f18392018-01-09 23:40:57 +010052}
53
Christoph Hellwig19dca8c2017-12-23 13:46:06 +010054void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
55 gfp_t gfp, unsigned long attrs)
Christoph Hellwig002e6742018-01-09 16:30:23 +010056{
Christoph Hellwig080321d2017-12-22 11:51:44 +010057 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
58 int page_order = get_order(size);
59 struct page *page = NULL;
Christoph Hellwigc10f07a2018-03-19 11:38:25 +010060 void *ret;
Christoph Hellwig002e6742018-01-09 16:30:23 +010061
Christoph Hellwige89f5b32018-03-28 15:35:35 +020062 /* we always manually zero the memory once we are done: */
63 gfp &= ~__GFP_ZERO;
64
Christoph Hellwigc61e9632018-01-09 23:39:03 +010065 /* GFP_DMA32 and GFP_DMA are no ops without the corresponding zones: */
66 if (dev->coherent_dma_mask <= DMA_BIT_MASK(ARCH_ZONE_DMA_BITS))
67 gfp |= GFP_DMA;
68 if (dev->coherent_dma_mask <= DMA_BIT_MASK(32) && !(gfp & GFP_DMA))
69 gfp |= GFP_DMA32;
70
Christoph Hellwig95f18392018-01-09 23:40:57 +010071again:
Christoph Hellwig080321d2017-12-22 11:51:44 +010072 /* CMA can be used only in the context which permits sleeping */
Christoph Hellwig95f18392018-01-09 23:40:57 +010073 if (gfpflags_allow_blocking(gfp)) {
Christoph Hellwig080321d2017-12-22 11:51:44 +010074 page = dma_alloc_from_contiguous(dev, count, page_order, gfp);
Christoph Hellwig95f18392018-01-09 23:40:57 +010075 if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
76 dma_release_from_contiguous(dev, page, count);
77 page = NULL;
78 }
79 }
Christoph Hellwig080321d2017-12-22 11:51:44 +010080 if (!page)
Christoph Hellwig21f237e2017-12-22 11:55:23 +010081 page = alloc_pages_node(dev_to_node(dev), gfp, page_order);
Christoph Hellwig95f18392018-01-09 23:40:57 +010082
83 if (page && !dma_coherent_ok(dev, page_to_phys(page), size)) {
84 __free_pages(page, page_order);
85 page = NULL;
86
87 if (dev->coherent_dma_mask < DMA_BIT_MASK(32) &&
88 !(gfp & GFP_DMA)) {
89 gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
90 goto again;
91 }
92 }
93
Christoph Hellwig080321d2017-12-22 11:51:44 +010094 if (!page)
95 return NULL;
Christoph Hellwigc10f07a2018-03-19 11:38:25 +010096 ret = page_address(page);
97 if (force_dma_unencrypted()) {
98 set_memory_decrypted((unsigned long)ret, 1 << page_order);
99 *dma_handle = __phys_to_dma(dev, page_to_phys(page));
100 } else {
101 *dma_handle = phys_to_dma(dev, page_to_phys(page));
102 }
103 memset(ret, 0, size);
104 return ret;
Christoph Hellwig002e6742018-01-09 16:30:23 +0100105}
106
Christoph Hellwig42ed6452018-02-02 09:51:14 +0100107/*
108 * NOTE: this function must never look at the dma_addr argument, because we want
109 * to be able to use it as a helper for iommu implementations as well.
110 */
Christoph Hellwig19dca8c2017-12-23 13:46:06 +0100111void dma_direct_free(struct device *dev, size_t size, void *cpu_addr,
Christoph Hellwig002e6742018-01-09 16:30:23 +0100112 dma_addr_t dma_addr, unsigned long attrs)
113{
Christoph Hellwig080321d2017-12-22 11:51:44 +0100114 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100115 unsigned int page_order = get_order(size);
Christoph Hellwig080321d2017-12-22 11:51:44 +0100116
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100117 if (force_dma_unencrypted())
118 set_memory_encrypted((unsigned long)cpu_addr, 1 << page_order);
Christoph Hellwig080321d2017-12-22 11:51:44 +0100119 if (!dma_release_from_contiguous(dev, virt_to_page(cpu_addr), count))
Christoph Hellwigc10f07a2018-03-19 11:38:25 +0100120 free_pages((unsigned long)cpu_addr, page_order);
Christoph Hellwig002e6742018-01-09 16:30:23 +0100121}
122
123static dma_addr_t dma_direct_map_page(struct device *dev, struct page *page,
124 unsigned long offset, size_t size, enum dma_data_direction dir,
125 unsigned long attrs)
126{
Christoph Hellwig27975962018-01-09 16:30:47 +0100127 dma_addr_t dma_addr = phys_to_dma(dev, page_to_phys(page)) + offset;
128
129 if (!check_addr(dev, dma_addr, size, __func__))
130 return DIRECT_MAPPING_ERROR;
131 return dma_addr;
Christoph Hellwig002e6742018-01-09 16:30:23 +0100132}
133
134static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
135 int nents, enum dma_data_direction dir, unsigned long attrs)
136{
137 int i;
138 struct scatterlist *sg;
139
140 for_each_sg(sgl, sg, nents, i) {
Christoph Hellwig002e6742018-01-09 16:30:23 +0100141 BUG_ON(!sg_page(sg));
Christoph Hellwig2e86a042017-12-22 11:29:51 +0100142
143 sg_dma_address(sg) = phys_to_dma(dev, sg_phys(sg));
Christoph Hellwig27975962018-01-09 16:30:47 +0100144 if (!check_addr(dev, sg_dma_address(sg), sg->length, __func__))
145 return 0;
Christoph Hellwig002e6742018-01-09 16:30:23 +0100146 sg_dma_len(sg) = sg->length;
147 }
148
149 return nents;
150}
151
Christoph Hellwig1a9777a2017-12-24 15:04:32 +0100152int dma_direct_supported(struct device *dev, u64 mask)
153{
154#ifdef CONFIG_ZONE_DMA
155 if (mask < DMA_BIT_MASK(ARCH_ZONE_DMA_BITS))
156 return 0;
157#else
158 /*
159 * Because 32-bit DMA masks are so common we expect every architecture
160 * to be able to satisfy them - either by not supporting more physical
161 * memory, or by providing a ZONE_DMA32. If neither is the case, the
162 * architecture needs to use an IOMMU instead of the direct mapping.
163 */
164 if (mask < DMA_BIT_MASK(32))
165 return 0;
166#endif
167 return 1;
168}
169
Christoph Hellwig27975962018-01-09 16:30:47 +0100170static int dma_direct_mapping_error(struct device *dev, dma_addr_t dma_addr)
171{
172 return dma_addr == DIRECT_MAPPING_ERROR;
173}
174
Christoph Hellwig002e6742018-01-09 16:30:23 +0100175const struct dma_map_ops dma_direct_ops = {
176 .alloc = dma_direct_alloc,
177 .free = dma_direct_free,
178 .map_page = dma_direct_map_page,
179 .map_sg = dma_direct_map_sg,
Christoph Hellwig1a9777a2017-12-24 15:04:32 +0100180 .dma_supported = dma_direct_supported,
Christoph Hellwig27975962018-01-09 16:30:47 +0100181 .mapping_error = dma_direct_mapping_error,
Christoph Hellwigf25e6f62018-02-02 09:21:07 +0100182 .is_phys = 1,
Christoph Hellwig002e6742018-01-09 16:30:23 +0100183};
184EXPORT_SYMBOL(dma_direct_ops);