Skip to content

Commit d35834c

Browse files
author
Christoph Hellwig
committed
dma-mapping: add a dma_ops_bypass flag to struct device
Several IOMMU drivers have a bypass mode where they can use a direct mapping if the devices DMA mask is large enough. Add generic support to the core dma-mapping code to do that to switch those drivers to a common solution. Signed-off-by: Christoph Hellwig <[email protected]> Tested-by: Alexey Kardashevskiy <[email protected]> Reviewed-by: Alexey Kardashevskiy <[email protected]>
1 parent 2f9237d commit d35834c

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

include/linux/device.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,11 @@ struct dev_links_info {
523523
* sync_state() callback.
524524
* @dma_coherent: this particular device is dma coherent, even if the
525525
* architecture supports non-coherent devices.
526+
* @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the
527+
* streaming DMA operations (->map_* / ->unmap_* / ->sync_*),
528+
* and optionall (if the coherent mask is large enough) also
529+
* for dma allocations. This flag is managed by the dma ops
530+
* instance from ->dma_supported.
526531
*
527532
* At the lowest level, every device in a Linux system is represented by an
528533
* instance of struct device. The device structure contains the information
@@ -623,6 +628,9 @@ struct device {
623628
defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
624629
bool dma_coherent:1;
625630
#endif
631+
#ifdef CONFIG_DMA_OPS_BYPASS
632+
bool dma_ops_bypass : 1;
633+
#endif
626634
};
627635

628636
static inline struct device *kobj_to_dev(struct kobject *kobj)

kernel/dma/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ config HAS_DMA
88
config DMA_OPS
99
bool
1010

11+
#
12+
# IOMMU drivers that can bypass the IOMMU code and optionally use the direct
13+
# mapping fast path should select this option and set the dma_ops_bypass
14+
# flag in struct device where applicable
15+
#
16+
config DMA_OPS_BYPASS
17+
bool
18+
1119
config NEED_SG_DMA_LENGTH
1220
bool
1321

kernel/dma/mapping.c

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,35 @@ void *dmam_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
105105
}
106106
EXPORT_SYMBOL(dmam_alloc_attrs);
107107

108-
static inline bool dma_is_direct(const struct dma_map_ops *ops)
108+
static bool dma_go_direct(struct device *dev, dma_addr_t mask,
109+
const struct dma_map_ops *ops)
109110
{
110-
return likely(!ops);
111+
if (likely(!ops))
112+
return true;
113+
#ifdef CONFIG_DMA_OPS_BYPASS
114+
if (dev->dma_ops_bypass)
115+
return min_not_zero(mask, dev->bus_dma_limit) >=
116+
dma_direct_get_required_mask(dev);
117+
#endif
118+
return false;
119+
}
120+
121+
122+
/*
123+
* Check if the devices uses a direct mapping for streaming DMA operations.
124+
* This allows IOMMU drivers to set a bypass mode if the DMA mask is large
125+
* enough.
126+
*/
127+
static inline bool dma_alloc_direct(struct device *dev,
128+
const struct dma_map_ops *ops)
129+
{
130+
return dma_go_direct(dev, dev->coherent_dma_mask, ops);
131+
}
132+
133+
static inline bool dma_map_direct(struct device *dev,
134+
const struct dma_map_ops *ops)
135+
{
136+
return dma_go_direct(dev, *dev->dma_mask, ops);
111137
}
112138

113139
dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
@@ -118,7 +144,7 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
118144
dma_addr_t addr;
119145

120146
BUG_ON(!valid_dma_direction(dir));
121-
if (dma_is_direct(ops))
147+
if (dma_map_direct(dev, ops))
122148
addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
123149
else
124150
addr = ops->map_page(dev, page, offset, size, dir, attrs);
@@ -134,7 +160,7 @@ void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size,
134160
const struct dma_map_ops *ops = get_dma_ops(dev);
135161

136162
BUG_ON(!valid_dma_direction(dir));
137-
if (dma_is_direct(ops))
163+
if (dma_map_direct(dev, ops))
138164
dma_direct_unmap_page(dev, addr, size, dir, attrs);
139165
else if (ops->unmap_page)
140166
ops->unmap_page(dev, addr, size, dir, attrs);
@@ -153,7 +179,7 @@ int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
153179
int ents;
154180

155181
BUG_ON(!valid_dma_direction(dir));
156-
if (dma_is_direct(ops))
182+
if (dma_map_direct(dev, ops))
157183
ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
158184
else
159185
ents = ops->map_sg(dev, sg, nents, dir, attrs);
@@ -172,7 +198,7 @@ void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
172198

173199
BUG_ON(!valid_dma_direction(dir));
174200
debug_dma_unmap_sg(dev, sg, nents, dir);
175-
if (dma_is_direct(ops))
201+
if (dma_map_direct(dev, ops))
176202
dma_direct_unmap_sg(dev, sg, nents, dir, attrs);
177203
else if (ops->unmap_sg)
178204
ops->unmap_sg(dev, sg, nents, dir, attrs);
@@ -191,7 +217,7 @@ dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,
191217
if (WARN_ON_ONCE(pfn_valid(PHYS_PFN(phys_addr))))
192218
return DMA_MAPPING_ERROR;
193219

194-
if (dma_is_direct(ops))
220+
if (dma_map_direct(dev, ops))
195221
addr = dma_direct_map_resource(dev, phys_addr, size, dir, attrs);
196222
else if (ops->map_resource)
197223
addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
@@ -207,7 +233,7 @@ void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
207233
const struct dma_map_ops *ops = get_dma_ops(dev);
208234

209235
BUG_ON(!valid_dma_direction(dir));
210-
if (!dma_is_direct(ops) && ops->unmap_resource)
236+
if (!dma_map_direct(dev, ops) && ops->unmap_resource)
211237
ops->unmap_resource(dev, addr, size, dir, attrs);
212238
debug_dma_unmap_resource(dev, addr, size, dir);
213239
}
@@ -219,7 +245,7 @@ void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
219245
const struct dma_map_ops *ops = get_dma_ops(dev);
220246

221247
BUG_ON(!valid_dma_direction(dir));
222-
if (dma_is_direct(ops))
248+
if (dma_map_direct(dev, ops))
223249
dma_direct_sync_single_for_cpu(dev, addr, size, dir);
224250
else if (ops->sync_single_for_cpu)
225251
ops->sync_single_for_cpu(dev, addr, size, dir);
@@ -233,7 +259,7 @@ void dma_sync_single_for_device(struct device *dev, dma_addr_t addr,
233259
const struct dma_map_ops *ops = get_dma_ops(dev);
234260

235261
BUG_ON(!valid_dma_direction(dir));
236-
if (dma_is_direct(ops))
262+
if (dma_map_direct(dev, ops))
237263
dma_direct_sync_single_for_device(dev, addr, size, dir);
238264
else if (ops->sync_single_for_device)
239265
ops->sync_single_for_device(dev, addr, size, dir);
@@ -247,7 +273,7 @@ void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
247273
const struct dma_map_ops *ops = get_dma_ops(dev);
248274

249275
BUG_ON(!valid_dma_direction(dir));
250-
if (dma_is_direct(ops))
276+
if (dma_map_direct(dev, ops))
251277
dma_direct_sync_sg_for_cpu(dev, sg, nelems, dir);
252278
else if (ops->sync_sg_for_cpu)
253279
ops->sync_sg_for_cpu(dev, sg, nelems, dir);
@@ -261,7 +287,7 @@ void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
261287
const struct dma_map_ops *ops = get_dma_ops(dev);
262288

263289
BUG_ON(!valid_dma_direction(dir));
264-
if (dma_is_direct(ops))
290+
if (dma_map_direct(dev, ops))
265291
dma_direct_sync_sg_for_device(dev, sg, nelems, dir);
266292
else if (ops->sync_sg_for_device)
267293
ops->sync_sg_for_device(dev, sg, nelems, dir);
@@ -302,7 +328,7 @@ int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
302328
{
303329
const struct dma_map_ops *ops = get_dma_ops(dev);
304330

305-
if (dma_is_direct(ops))
331+
if (dma_alloc_direct(dev, ops))
306332
return dma_direct_get_sgtable(dev, sgt, cpu_addr, dma_addr,
307333
size, attrs);
308334
if (!ops->get_sgtable)
@@ -372,7 +398,7 @@ bool dma_can_mmap(struct device *dev)
372398
{
373399
const struct dma_map_ops *ops = get_dma_ops(dev);
374400

375-
if (dma_is_direct(ops))
401+
if (dma_alloc_direct(dev, ops))
376402
return dma_direct_can_mmap(dev);
377403
return ops->mmap != NULL;
378404
}
@@ -397,7 +423,7 @@ int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
397423
{
398424
const struct dma_map_ops *ops = get_dma_ops(dev);
399425

400-
if (dma_is_direct(ops))
426+
if (dma_alloc_direct(dev, ops))
401427
return dma_direct_mmap(dev, vma, cpu_addr, dma_addr, size,
402428
attrs);
403429
if (!ops->mmap)
@@ -410,7 +436,7 @@ u64 dma_get_required_mask(struct device *dev)
410436
{
411437
const struct dma_map_ops *ops = get_dma_ops(dev);
412438

413-
if (dma_is_direct(ops))
439+
if (dma_alloc_direct(dev, ops))
414440
return dma_direct_get_required_mask(dev);
415441
if (ops->get_required_mask)
416442
return ops->get_required_mask(dev);
@@ -441,7 +467,7 @@ void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
441467
/* let the implementation decide on the zone to allocate from: */
442468
flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
443469

444-
if (dma_is_direct(ops))
470+
if (dma_alloc_direct(dev, ops))
445471
cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs);
446472
else if (ops->alloc)
447473
cpu_addr = ops->alloc(dev, size, dma_handle, flag, attrs);
@@ -473,7 +499,7 @@ void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
473499
return;
474500

475501
debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
476-
if (dma_is_direct(ops))
502+
if (dma_alloc_direct(dev, ops))
477503
dma_direct_free(dev, size, cpu_addr, dma_handle, attrs);
478504
else if (ops->free)
479505
ops->free(dev, size, cpu_addr, dma_handle, attrs);
@@ -484,7 +510,11 @@ int dma_supported(struct device *dev, u64 mask)
484510
{
485511
const struct dma_map_ops *ops = get_dma_ops(dev);
486512

487-
if (dma_is_direct(ops))
513+
/*
514+
* ->dma_supported sets the bypass flag, so we must always call
515+
* into the method here unless the device is truly direct mapped.
516+
*/
517+
if (!ops)
488518
return dma_direct_supported(dev, mask);
489519
if (!ops->dma_supported)
490520
return 1;
@@ -540,7 +570,7 @@ void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
540570

541571
BUG_ON(!valid_dma_direction(dir));
542572

543-
if (dma_is_direct(ops))
573+
if (dma_alloc_direct(dev, ops))
544574
arch_dma_cache_sync(dev, vaddr, size, dir);
545575
else if (ops->cache_sync)
546576
ops->cache_sync(dev, vaddr, size, dir);
@@ -552,7 +582,7 @@ size_t dma_max_mapping_size(struct device *dev)
552582
const struct dma_map_ops *ops = get_dma_ops(dev);
553583
size_t size = SIZE_MAX;
554584

555-
if (dma_is_direct(ops))
585+
if (dma_map_direct(dev, ops))
556586
size = dma_direct_max_mapping_size(dev);
557587
else if (ops && ops->max_mapping_size)
558588
size = ops->max_mapping_size(dev);
@@ -565,7 +595,7 @@ bool dma_need_sync(struct device *dev, dma_addr_t dma_addr)
565595
{
566596
const struct dma_map_ops *ops = get_dma_ops(dev);
567597

568-
if (dma_is_direct(ops))
598+
if (dma_map_direct(dev, ops))
569599
return dma_direct_need_sync(dev, dma_addr);
570600
return ops->sync_single_for_cpu || ops->sync_single_for_device;
571601
}

0 commit comments

Comments
 (0)