Skip to content

Commit 0176adb

Browse files
author
Christoph Hellwig
committed
swiotlb: refactor coherent buffer allocation
Factor out a new swiotlb_alloc_buffer helper that allocates DMA coherent memory from the swiotlb bounce buffer. This allows to simplify the swiotlb_alloc implemenation that uses dma_direct_alloc to try to allocate a reachable buffer first. Signed-off-by: Christoph Hellwig <[email protected]> Acked-by: Christian König <[email protected]>
1 parent a25381a commit 0176adb

File tree

1 file changed

+65
-57
lines changed

1 file changed

+65
-57
lines changed

lib/swiotlb.c

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -709,75 +709,79 @@ void swiotlb_tbl_sync_single(struct device *hwdev, phys_addr_t tlb_addr,
709709
}
710710
EXPORT_SYMBOL_GPL(swiotlb_tbl_sync_single);
711711

712-
void *
713-
swiotlb_alloc_coherent(struct device *hwdev, size_t size,
714-
dma_addr_t *dma_handle, gfp_t flags)
712+
static inline bool dma_coherent_ok(struct device *dev, dma_addr_t addr,
713+
size_t size)
715714
{
716-
bool warn = !(flags & __GFP_NOWARN);
717-
dma_addr_t dev_addr;
718-
void *ret;
719-
int order = get_order(size);
720-
u64 dma_mask = DMA_BIT_MASK(32);
715+
u64 mask = DMA_BIT_MASK(32);
721716

722-
if (hwdev && hwdev->coherent_dma_mask)
723-
dma_mask = hwdev->coherent_dma_mask;
717+
if (dev && dev->coherent_dma_mask)
718+
mask = dev->coherent_dma_mask;
719+
return addr + size - 1 <= mask;
720+
}
724721

725-
ret = (void *)__get_free_pages(flags, order);
726-
if (ret) {
727-
dev_addr = swiotlb_virt_to_bus(hwdev, ret);
728-
if (dev_addr + size - 1 > dma_mask) {
729-
/*
730-
* The allocated memory isn't reachable by the device.
731-
*/
732-
free_pages((unsigned long) ret, order);
733-
ret = NULL;
734-
}
735-
}
736-
if (!ret) {
737-
/*
738-
* We are either out of memory or the device can't DMA to
739-
* GFP_DMA memory; fall back on map_single(), which
740-
* will grab memory from the lowest available address range.
741-
*/
742-
phys_addr_t paddr = map_single(hwdev, 0, size, DMA_FROM_DEVICE,
743-
warn ? 0 : DMA_ATTR_NO_WARN);
744-
if (paddr == SWIOTLB_MAP_ERROR)
745-
goto err_warn;
722+
static void *
723+
swiotlb_alloc_buffer(struct device *dev, size_t size, dma_addr_t *dma_handle,
724+
unsigned long attrs)
725+
{
726+
phys_addr_t phys_addr;
746727

747-
ret = phys_to_virt(paddr);
748-
dev_addr = swiotlb_phys_to_dma(hwdev, paddr);
728+
if (swiotlb_force == SWIOTLB_NO_FORCE)
729+
goto out_warn;
749730

750-
/* Confirm address can be DMA'd by device */
751-
if (dev_addr + size - 1 > dma_mask) {
752-
printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
753-
(unsigned long long)dma_mask,
754-
(unsigned long long)dev_addr);
731+
phys_addr = swiotlb_tbl_map_single(dev,
732+
swiotlb_phys_to_dma(dev, io_tlb_start),
733+
0, size, DMA_FROM_DEVICE, 0);
734+
if (phys_addr == SWIOTLB_MAP_ERROR)
735+
goto out_warn;
755736

756-
/*
757-
* DMA_TO_DEVICE to avoid memcpy in unmap_single.
758-
* The DMA_ATTR_SKIP_CPU_SYNC is optional.
759-
*/
760-
swiotlb_tbl_unmap_single(hwdev, paddr,
761-
size, DMA_TO_DEVICE,
762-
DMA_ATTR_SKIP_CPU_SYNC);
763-
goto err_warn;
764-
}
765-
}
737+
*dma_handle = swiotlb_phys_to_dma(dev, phys_addr);
738+
if (dma_coherent_ok(dev, *dma_handle, size))
739+
goto out_unmap;
766740

767-
*dma_handle = dev_addr;
768-
memset(ret, 0, size);
741+
memset(phys_to_virt(phys_addr), 0, size);
742+
return phys_to_virt(phys_addr);
769743

770-
return ret;
744+
out_unmap:
745+
dev_warn(dev, "hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
746+
(unsigned long long)(dev ? dev->coherent_dma_mask : 0),
747+
(unsigned long long)*dma_handle);
771748

772-
err_warn:
773-
if (warn && printk_ratelimit()) {
774-
pr_warn("swiotlb: coherent allocation failed for device %s size=%zu\n",
775-
dev_name(hwdev), size);
749+
/*
750+
* DMA_TO_DEVICE to avoid memcpy in unmap_single.
751+
* DMA_ATTR_SKIP_CPU_SYNC is optional.
752+
*/
753+
swiotlb_tbl_unmap_single(dev, phys_addr, size, DMA_TO_DEVICE,
754+
DMA_ATTR_SKIP_CPU_SYNC);
755+
out_warn:
756+
if ((attrs & DMA_ATTR_NO_WARN) && printk_ratelimit()) {
757+
dev_warn(dev,
758+
"swiotlb: coherent allocation failed, size=%zu\n",
759+
size);
776760
dump_stack();
777761
}
778-
779762
return NULL;
780763
}
764+
765+
void *
766+
swiotlb_alloc_coherent(struct device *hwdev, size_t size,
767+
dma_addr_t *dma_handle, gfp_t flags)
768+
{
769+
int order = get_order(size);
770+
unsigned long attrs = (flags & __GFP_NOWARN) ? DMA_ATTR_NO_WARN : 0;
771+
void *ret;
772+
773+
ret = (void *)__get_free_pages(flags, order);
774+
if (ret) {
775+
*dma_handle = swiotlb_virt_to_bus(hwdev, ret);
776+
if (dma_coherent_ok(hwdev, *dma_handle, size)) {
777+
memset(ret, 0, size);
778+
return ret;
779+
}
780+
free_pages((unsigned long)ret, order);
781+
}
782+
783+
return swiotlb_alloc_buffer(hwdev, size, dma_handle, attrs);
784+
}
781785
EXPORT_SYMBOL(swiotlb_alloc_coherent);
782786

783787
static bool swiotlb_free_buffer(struct device *dev, size_t size,
@@ -1103,6 +1107,10 @@ void *swiotlb_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
11031107
{
11041108
void *vaddr;
11051109

1110+
/* temporary workaround: */
1111+
if (gfp & __GFP_NOWARN)
1112+
attrs |= DMA_ATTR_NO_WARN;
1113+
11061114
/*
11071115
* Don't print a warning when the first allocation attempt fails.
11081116
* swiotlb_alloc_coherent() will print a warning when the DMA memory
@@ -1112,7 +1120,7 @@ void *swiotlb_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
11121120

11131121
vaddr = dma_direct_alloc(dev, size, dma_handle, gfp, attrs);
11141122
if (!vaddr)
1115-
vaddr = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);
1123+
vaddr = swiotlb_alloc_buffer(dev, size, dma_handle, attrs);
11161124
return vaddr;
11171125
}
11181126

0 commit comments

Comments
 (0)