~ [ 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-5.1.21)


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

~ [ 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