clean up the bypass code in intel-iommu a little
Hi Dave and Joerg,
below are three relatively simple patches to clean up the bypass case and make it share more code with the dma-direct implementation.
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/3 intel-iommu: clean up iommu_no_mapping
Invert the return value to avoid double negatives, use a bool instead of int as the return value, and reduce some indentation after early returns.
--- drivers/iommu/intel-iommu.c | 47 +++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 26 deletions(-)
drivers/iommu/intel-iommu.c +21 -26
@@ -3606,45 +3606,40 @@ struct dmar_domain *get_valid_domain_for_dev(struct device *dev) }/* Check if the dev needs to go through non-identity map and unmap process.*/ -static int iommu_no_mapping (struct device *dev) +static bool iommu_need_mapping (struct device *dev) {int found;if (iommu_dummy (dev)) -return 1 ; +return false;if (!iommu_identity_mapping) -return 0 ; +return true; found =identity_mapping (dev);if (found) {if (iommu_should_identity_map (dev,0 )) -return 1 ; -else { -/* -* 32 bit DMA is removed from si_domain and fall back -* to non-identity mapping. -*/ -dmar_remove_one_dev_info (dev); -dev_info (dev,"32bit DMA uses non-identity mapping\n" ); -return 0 ; - } +return false; + +/* +* 32 bit DMA is removed from si_domain and fall back to +* non-identity mapping. +*/ +dmar_remove_one_dev_info (dev); +dev_info (dev,"32bit DMA uses non-identity mapping\n" ); }else {/* * In case of a detached 64 bit DMA device from vm, the device * is put into si_domain for identity mapping. */ -if (iommu_should_identity_map (dev,0 )) { -int ret; - ret =domain_add_dev_info (si_domain, dev); -if (!ret) { -dev_info (dev,"64bit DMA uses identity mapping\n" ); -return 1 ; - } +if (iommu_should_identity_map (dev,0 ) && + !domain_add_dev_info (si_domain, dev)) { +dev_info (dev,"64bit DMA uses identity mapping\n" ); +return false; } } -return 0 ; +return true; } static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,
@@ -3660,7 +3655,7 @@ static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,BUG_ON (dir ==DMA_NONE ); -if (iommu_no_mapping (dev)) +if (!iommu_need_mapping (dev))return paddr; domain =get_valid_domain_for_dev (dev);
@@ -3731,7 +3726,7 @@ static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size)struct intel_iommu *iommu;struct page *freelist; -if (iommu_no_mapping (dev)) +if (!iommu_need_mapping (dev))return ; domain =find_domain (dev);
@@ -3782,7 +3777,7 @@ static void *intel_alloc_coherent(struct device *dev, size_t size, size =PAGE_ALIGN (size); order =get_order (size); -if (!iommu_no_mapping (dev)) +if (iommu_need_mapping (dev)) flags &= ~(GFP_DMA |GFP_DMA32 );else if (dev->coherent_dma_mask <dma_get_required_mask (dev)) {if (dev->coherent_dma_mask <DMA_BIT_MASK (32 ))
@@ -3796,7 +3791,7 @@ static void *intel_alloc_coherent(struct device *dev, size_t size, page =dma_alloc_from_contiguous (dev, count, order, flags & __GFP_NOWARN); -if (page &&iommu_no_mapping (dev) && +if (page && !iommu_need_mapping (dev) &&page_to_phys (page) + size > dev->coherent_dma_mask) {dma_release_from_contiguous (dev, page, count); page =NULL ;
@@ -3878,7 +3873,7 @@ static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelestruct intel_iommu *iommu;BUG_ON (dir ==DMA_NONE ); -if (iommu_no_mapping (dev)) +if (!iommu_need_mapping (dev))return intel_nontranslate_map_sg (dev, sglist, nelems, dir); domain =get_valid_domain_for_dev (dev);
Signature · 1 line
2.20.1
PATCH 2/3 intel-iommu: use dma_direct for bypass devices
In reply to a message not in the archiveThe intel-iommu driver currently has a partial reimplementation of the direct mapping code for devices that use pass through mode. Replace that code with calls to the relevant dma_direct routines at the highest level. This means we have exactly the same behvior as the dma direct code itself, and can prepare for eventually only attaching the intel_iommu ops to devices that actually need dynamic iommu mappings.
--- drivers/iommu/intel-iommu.c | 71 ++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 40 deletions(-)
drivers/iommu/intel-iommu.c +31 -40
@@ -3655,9 +3655,6 @@ static dma_addr_t __intel_map_single(struct device *dev, phys_addr_t paddr,BUG_ON (dir ==DMA_NONE ); -if (!iommu_need_mapping (dev)) -return paddr; - domain =get_valid_domain_for_dev (dev);if (!domain)return DMA_MAPPING_ERROR ;
@@ -3706,15 +3703,20 @@ static dma_addr_t intel_map_page(struct device *dev, struct page *page, enum dma_data_direction dir, unsigned long attrs) { -return __intel_map_single (dev,page_to_phys (page) + offset, size, - dir, *dev->dma_mask); + if (iommu_need_mapping(dev)) + return __intel_map_single(dev, page_to_phys(page) + offset, + size, dir, *dev->dma_mask); +return dma_direct_map_page (dev, page, offset, size, dir, attrs); }static dma_addr_t intel_map_resource (struct device *dev,phys_addr_t phys_addr,size_t size,enum dma_data_direction dir,unsigned long attrs) { -return __intel_map_single (dev, phys_addr, size, dir, *dev->dma_mask); +if (iommu_need_mapping (dev)) +return __intel_map_single (dev, phys_addr, size, dir, + *dev->dma_mask); +return dma_direct_map_resource (dev, phys_addr, size, dir, attrs); }static void intel_unmap (struct device *dev,dma_addr_t dev_addr,size_t size)
@@ -3726,9 +3728,6 @@ static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size)struct intel_iommu *iommu;struct page *freelist; -if (!iommu_need_mapping (dev)) -return ; - domain =find_domain (dev);BUG_ON (!domain);
@@ -3764,7 +3763,17 @@ static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr, size_t size, enum dma_data_direction dir, unsigned long attrs) { -intel_unmap (dev ,dev_addr ,size ); + if (iommu_need_mapping(dev)) + intel_unmap(dev, dev_addr, size); + else +dma_direct_unmap_page (dev ,dev_addr ,size ,dir ,attrs ); +} + +static void intel_unmap_resource (struct device *dev,dma_addr_t dev_addr, +size_t size,enum dma_data_direction dir,unsigned long attrs) +{ +if (iommu_need_mapping (dev)) +intel_unmap (dev, dev_addr, size); } static void *intel_alloc_coherent(struct device *dev, size_t size,
@@ -3774,28 +3783,18 @@ static void *intel_alloc_coherent(struct device *dev, size_t size,struct page *page =NULL ;int order; +if (!iommu_need_mapping (dev)) +return dma_direct_alloc (dev, size, dma_handle, flags, attrs); + size =PAGE_ALIGN (size); order =get_order (size); - -if (iommu_need_mapping (dev)) - flags &= ~(GFP_DMA |GFP_DMA32 ); -else if (dev->coherent_dma_mask <dma_get_required_mask (dev)) { -if (dev->coherent_dma_mask <DMA_BIT_MASK (32 )) - flags |=GFP_DMA ; -else - flags |=GFP_DMA32 ; - } + flags &= ~(GFP_DMA |GFP_DMA32 );if (gfpflags_allow_blocking (flags)) {unsigned int count = size >>PAGE_SHIFT ; page =dma_alloc_from_contiguous (dev, count, order, flags & __GFP_NOWARN); -if (page && !iommu_need_mapping (dev) && -page_to_phys (page) + size > dev->coherent_dma_mask) { -dma_release_from_contiguous (dev, page, count); - page =NULL ; - } }if (!page)
@@ -3821,6 +3820,9 @@ static void intel_free_coherent(struct device *dev, size_t size, void *vaddr,int order;struct page *page =virt_to_page (vaddr); +if (!iommu_need_mapping (dev)) +return dma_direct_free (dev, size, vaddr, dma_handle, attrs); + size =PAGE_ALIGN (size); order =get_order (size);
@@ -3838,6 +3840,9 @@ static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,struct scatterlist *sg;int i; +if (!iommu_need_mapping (dev)) +return dma_direct_unmap_sg (dev, sglist, nelems, dir, attrs); +for_each_sg (sglist, sg, nelems, i) { nrpages +=aligned_nrpages (sg_dma_address (sg),sg_dma_len (sg)); }
@@ -3845,20 +3850,6 @@ static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist,intel_unmap (dev, startaddr, nrpages <<VTD_PAGE_SHIFT ); } -static int intel_nontranslate_map_sg (struct device *hddev, -struct scatterlist *sglist,int nelems,int dir) -{ -int i; -struct scatterlist *sg; - -for_each_sg (sglist, sg, nelems, i) { -BUG_ON (!sg_page (sg)); - sg->dma_address =sg_phys (sg); - sg->dma_length = sg->length; - } -return nelems; -} -static int intel_map_sg (struct device *dev,struct scatterlist *sglist,int nelems,enum dma_data_direction dir,unsigned long attrs) {
@@ -3874,7 +3865,7 @@ static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int neleBUG_ON (dir ==DMA_NONE );if (!iommu_need_mapping (dev)) -return intel_nontranslate_map_sg (dev, sglist, nelems, dir); +return dma_direct_map_sg (dev, sglist, nelems, dir, attrs); domain =get_valid_domain_for_dev (dev);if (!domain)
@@ -3924,7 +3915,7 @@ static const struct dma_map_ops intel_dma_ops = { .map_page = intel_map_page, .unmap_page = intel_unmap_page, .map_resource = intel_map_resource, - .unmap_resource = intel_unmap_page, + .unmap_resource = intel_unmap_resource, .dma_supported = dma_direct_supported, };
Signature · 1 line
2.20.1
PATCH 3/3 intel-iommu: don't clear GFP_DMA and GFP_DMA32 flags
In reply to a message not in the archiveWe already do this in the caller.
--- drivers/iommu/intel-iommu.c | 1 - 1 file changed, 1 deletion(-)
drivers/iommu/intel-iommu.c +0 -1
@@ -3788,7 +3788,6 @@ static void *intel_alloc_coherent(struct device *dev, size_t size, size =PAGE_ALIGN (size); order =get_order (size); - flags &= ~(GFP_DMA |GFP_DMA32 );if (gfpflags_allow_blocking (flags)) {unsigned int count = size >>PAGE_SHIFT ;
Signature · 1 line
2.20.1
Quoted 3 lines · Christoph Hellwig
below are three relatively simple patches to clean up the bypass case and make it share more code with the dma-direct implementation.
These look simple and straightforward to me, applied.
Can you please also Cc LKML on iommu-patches in the future? It makes the patches easier to dig out of the email archive.
Also please Cc the Intel people I added here for future intel-iommu patches (we should probably more formalize their role on the driver).
Thanks,
Joerg