Skip to content

Commit 18c9cbb

Browse files
rleonmszyprow
authored andcommitted
dma-mapping: implement DMA_ATTR_MMIO for dma_(un)map_page_attrs()
Make dma_map_page_attrs() and dma_map_page_attrs() respect DMA_ATTR_MMIO. DMA_ATR_MMIO makes the functions behave the same as dma_(un)map_resource(): - No swiotlb is possible - Legacy dma_ops arches use ops->map_resource() - No kmsan - No arch_dma_map_phys_direct() The prior patches have made the internal functions called here support DMA_ATTR_MMIO. This is also preparation for turning dma_map_resource() into an inline calling dma_map_phys(DMA_ATTR_MMIO) to consolidate the flows. Reviewed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Leon Romanovsky <[email protected]> Signed-off-by: Marek Szyprowski <[email protected]> Link: https://lore.kernel.org/r/3660e2c78ea409d6c483a215858fb3af52cd0ed3.1757423202.git.leonro@nvidia.com
1 parent 6eb1e76 commit 18c9cbb

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

kernel/dma/mapping.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
158158
{
159159
const struct dma_map_ops *ops = get_dma_ops(dev);
160160
phys_addr_t phys = page_to_phys(page) + offset;
161+
bool is_mmio = attrs & DMA_ATTR_MMIO;
161162
dma_addr_t addr;
162163

163164
BUG_ON(!valid_dma_direction(dir));
@@ -166,14 +167,25 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
166167
return DMA_MAPPING_ERROR;
167168

168169
if (dma_map_direct(dev, ops) ||
169-
arch_dma_map_phys_direct(dev, phys + size))
170+
(!is_mmio && arch_dma_map_phys_direct(dev, phys + size)))
170171
addr = dma_direct_map_phys(dev, phys, size, dir, attrs);
171172
else if (use_dma_iommu(dev))
172173
addr = iommu_dma_map_phys(dev, phys, size, dir, attrs);
173-
else
174+
else if (is_mmio) {
175+
if (!ops->map_resource)
176+
return DMA_MAPPING_ERROR;
177+
178+
addr = ops->map_resource(dev, phys, size, dir, attrs);
179+
} else {
180+
/*
181+
* The dma_ops API contract for ops->map_page() requires
182+
* kmappable memory, while ops->map_resource() does not.
183+
*/
174184
addr = ops->map_page(dev, page, offset, size, dir, attrs);
185+
}
175186

176-
kmsan_handle_dma(phys, size, dir);
187+
if (!is_mmio)
188+
kmsan_handle_dma(phys, size, dir);
177189
trace_dma_map_phys(dev, phys, addr, size, dir, attrs);
178190
debug_dma_map_phys(dev, phys, size, dir, addr, attrs);
179191

@@ -185,14 +197,18 @@ void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size,
185197
enum dma_data_direction dir, unsigned long attrs)
186198
{
187199
const struct dma_map_ops *ops = get_dma_ops(dev);
200+
bool is_mmio = attrs & DMA_ATTR_MMIO;
188201

189202
BUG_ON(!valid_dma_direction(dir));
190203
if (dma_map_direct(dev, ops) ||
191-
arch_dma_unmap_phys_direct(dev, addr + size))
204+
(!is_mmio && arch_dma_unmap_phys_direct(dev, addr + size)))
192205
dma_direct_unmap_phys(dev, addr, size, dir, attrs);
193206
else if (use_dma_iommu(dev))
194207
iommu_dma_unmap_phys(dev, addr, size, dir, attrs);
195-
else
208+
else if (is_mmio) {
209+
if (ops->unmap_resource)
210+
ops->unmap_resource(dev, addr, size, dir, attrs);
211+
} else
196212
ops->unmap_page(dev, addr, size, dir, attrs);
197213
trace_dma_unmap_phys(dev, addr, size, dir, attrs);
198214
debug_dma_unmap_phys(dev, addr, size, dir);

0 commit comments

Comments
 (0)