Commit 3fb3378b authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Joerg Roedel

iommu/dma: Refactor iommu_dma_get_sgtable

Inline __iommu_dma_get_sgtable_page into the main function, and use the
fact that __iommu_dma_get_pages return NULL for remapped contigous
allocations to simplify the code flow a bit.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarRobin Murphy <robin.murphy@arm.com>
Signed-off-by: default avatarJoerg Roedel <jroedel@suse.de>
parent ee1ef05d
...@@ -1097,42 +1097,31 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, ...@@ -1097,42 +1097,31 @@ static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
return __iommu_dma_mmap(pages, size, vma); return __iommu_dma_mmap(pages, size, vma);
} }
static int __iommu_dma_get_sgtable_page(struct sg_table *sgt, struct page *page,
size_t size)
{
int ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
if (!ret)
sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
return ret;
}
static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt, static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
void *cpu_addr, dma_addr_t dma_addr, size_t size, void *cpu_addr, dma_addr_t dma_addr, size_t size,
unsigned long attrs) unsigned long attrs)
{ {
unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; struct page *page;
struct page **pages; int ret;
if (!is_vmalloc_addr(cpu_addr)) { if (is_vmalloc_addr(cpu_addr)) {
struct page *page = virt_to_page(cpu_addr); struct page **pages = __iommu_dma_get_pages(cpu_addr);
return __iommu_dma_get_sgtable_page(sgt, page, size);
}
if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) { if (pages) {
/* return sg_alloc_table_from_pages(sgt, pages,
* DMA_ATTR_FORCE_CONTIGUOUS allocations are always remapped, PAGE_ALIGN(size) >> PAGE_SHIFT,
* hence in the vmalloc space. 0, size, GFP_KERNEL);
*/ }
struct page *page = vmalloc_to_page(cpu_addr);
return __iommu_dma_get_sgtable_page(sgt, page, size); page = vmalloc_to_page(cpu_addr);
} else {
page = virt_to_page(cpu_addr);
} }
pages = __iommu_dma_get_pages(cpu_addr); ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
if (!pages) if (!ret)
return -ENXIO; sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
return sg_alloc_table_from_pages(sgt, pages, count, 0, size, return ret;
GFP_KERNEL);
} }
static const struct dma_map_ops iommu_dma_ops = { static const struct dma_map_ops iommu_dma_ops = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment