Skip to content

Commit

Permalink
iommu/dma: Merge the CMA and alloc_pages allocation paths
Browse files Browse the repository at this point in the history
Instead of having a separate code path for the non-blocking alloc_pages
and CMA allocations paths merge them into one.  There is a slight
behavior change here in that we try the page allocator if CMA fails.
This matches what dma-direct and other iommu drivers do and will be
needed to use the dma-iommu code on architectures without DMA remapping
later on.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
  • Loading branch information
Christoph Hellwig authored and Joerg Roedel committed May 27, 2019
1 parent 8680aa5 commit 9a4ab94
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions drivers/iommu/dma-iommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ static void *iommu_dma_alloc(struct device *dev, size_t size,
bool coherent = dev_is_dma_coherent(dev);
int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
size_t iosize = size;
struct page *page;
struct page *page = NULL;
void *addr;

size = PAGE_ALIGN(size);
Expand All @@ -984,35 +984,26 @@ static void *iommu_dma_alloc(struct device *dev, size_t size,
!(attrs & DMA_ATTR_FORCE_CONTIGUOUS))
return iommu_dma_alloc_remap(dev, iosize, handle, gfp, attrs);

if (!gfpflags_allow_blocking(gfp)) {
/*
* In atomic context we can't remap anything, so we'll only
* get the virtually contiguous buffer we need by way of a
* physically contiguous allocation.
*/
if (coherent) {
page = alloc_pages(gfp, get_order(size));
addr = page ? page_address(page) : NULL;
} else {
addr = dma_alloc_from_pool(size, &page, gfp);
}
if (!gfpflags_allow_blocking(gfp) && !coherent) {
addr = dma_alloc_from_pool(size, &page, gfp);
if (!addr)
return NULL;

*handle = __iommu_dma_map(dev, page_to_phys(page), iosize,
ioprot);
if (*handle == DMA_MAPPING_ERROR) {
if (coherent)
__free_pages(page, get_order(size));
else
dma_free_from_pool(addr, size);
dma_free_from_pool(addr, size);
return NULL;
}
return addr;
}

page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
get_order(size), gfp & __GFP_NOWARN);
if (gfpflags_allow_blocking(gfp))
page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
get_order(size),
gfp & __GFP_NOWARN);
if (!page)
page = alloc_pages(gfp, get_order(size));
if (!page)
return NULL;

Expand All @@ -1038,7 +1029,8 @@ static void *iommu_dma_alloc(struct device *dev, size_t size,
out_unmap:
__iommu_dma_unmap(dev, *handle, iosize);
out_free_pages:
dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT))
__free_pages(page, get_order(size));
return NULL;
}

Expand Down

0 comments on commit 9a4ab94

Please sign in to comment.