~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/dma/direct.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /kernel/dma/direct.c (Version linux-6.11.5) and /kernel/dma/direct.c (Version linux-6.5.13)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php