Skip to content

Commit 122fac0

Browse files
rmurphy-armjoergroedel
authored andcommitted
iommu/dma: Implement PCI allocation optimisation
Whilst PCI devices may have 64-bit DMA masks, they still benefit from using 32-bit addresses wherever possible in order to avoid DAC (PCI) or longer address packets (PCIe), which may incur a performance overhead. Implement the same optimisation as other allocators by trying to get a 32-bit address first, only falling back to the full mask if that fails. Signed-off-by: Robin Murphy <robin.murphy@arm.com> Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent f51d7bb commit 122fac0

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

drivers/iommu/dma-iommu.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,19 +287,28 @@ int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
287287
}
288288

289289
static struct iova *__alloc_iova(struct iommu_domain *domain, size_t size,
290-
dma_addr_t dma_limit)
290+
dma_addr_t dma_limit, struct device *dev)
291291
{
292292
struct iova_domain *iovad = cookie_iovad(domain);
293293
unsigned long shift = iova_shift(iovad);
294294
unsigned long length = iova_align(iovad, size) >> shift;
295+
struct iova *iova = NULL;
295296

296297
if (domain->geometry.force_aperture)
297298
dma_limit = min(dma_limit, domain->geometry.aperture_end);
299+
300+
/* Try to get PCI devices a SAC address */
301+
if (dma_limit > DMA_BIT_MASK(32) && dev_is_pci(dev))
302+
iova = alloc_iova(iovad, length, DMA_BIT_MASK(32) >> shift,
303+
true);
298304
/*
299305
* Enforce size-alignment to be safe - there could perhaps be an
300306
* attribute to control this per-device, or at least per-domain...
301307
*/
302-
return alloc_iova(iovad, length, dma_limit >> shift, true);
308+
if (!iova)
309+
iova = alloc_iova(iovad, length, dma_limit >> shift, true);
310+
311+
return iova;
303312
}
304313

305314
/* The IOVA allocator knows what we mapped, so just unmap whatever that was */
@@ -452,7 +461,7 @@ struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
452461
if (!pages)
453462
return NULL;
454463

455-
iova = __alloc_iova(domain, size, dev->coherent_dma_mask);
464+
iova = __alloc_iova(domain, size, dev->coherent_dma_mask, dev);
456465
if (!iova)
457466
goto out_free_pages;
458467

@@ -523,7 +532,7 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
523532
struct iova_domain *iovad = cookie_iovad(domain);
524533
size_t iova_off = iova_offset(iovad, phys);
525534
size_t len = iova_align(iovad, size + iova_off);
526-
struct iova *iova = __alloc_iova(domain, len, dma_get_mask(dev));
535+
struct iova *iova = __alloc_iova(domain, len, dma_get_mask(dev), dev);
527536

528537
if (!iova)
529538
return DMA_ERROR_CODE;
@@ -681,7 +690,7 @@ int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
681690
prev = s;
682691
}
683692

684-
iova = __alloc_iova(domain, iova_len, dma_get_mask(dev));
693+
iova = __alloc_iova(domain, iova_len, dma_get_mask(dev), dev);
685694
if (!iova)
686695
goto out_restore_sg;
687696

@@ -761,7 +770,7 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
761770

762771
msi_page->phys = msi_addr;
763772
if (iovad) {
764-
iova = __alloc_iova(domain, size, dma_get_mask(dev));
773+
iova = __alloc_iova(domain, size, dma_get_mask(dev), dev);
765774
if (!iova)
766775
goto out_free_page;
767776
msi_page->iova = iova_dma_addr(iovad, iova);

0 commit comments

Comments
 (0)