Skip to content

Commit ad4750b

Browse files
rmurphy-armjoergroedel
authored andcommitted
iommu/dma: Make limit checks self-contained
It's now easy to retrieve the device's DMA limits if we want to check them against the domain aperture, so do that ourselves instead of relying on them being passed through the callchain. Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Tested-by: Hanjun Guo <guohanjun@huawei.com> Signed-off-by: Robin Murphy <robin.murphy@arm.com> Link: https://lore.kernel.org/r/e28a114243d1e79eb3609aded034f8529521333f.1713523152.git.robin.murphy@arm.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent fece653 commit ad4750b

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

drivers/iommu/dma-iommu.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -660,19 +660,16 @@ static void iommu_dma_init_options(struct iommu_dma_options *options,
660660
/**
661661
* iommu_dma_init_domain - Initialise a DMA mapping domain
662662
* @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
663-
* @base: IOVA at which the mappable address space starts
664-
* @limit: Last address of the IOVA space
665663
* @dev: Device the domain is being initialised for
666664
*
667-
* @base and @limit + 1 should be exact multiples of IOMMU page granularity to
668-
* avoid rounding surprises. If necessary, we reserve the page at address 0
665+
* If the geometry and dma_range_map include address 0, we reserve that page
669666
* to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but
670667
* any change which could make prior IOVAs invalid will fail.
671668
*/
672-
static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
673-
dma_addr_t limit, struct device *dev)
669+
static int iommu_dma_init_domain(struct iommu_domain *domain, struct device *dev)
674670
{
675671
struct iommu_dma_cookie *cookie = domain->iova_cookie;
672+
const struct bus_dma_region *map = dev->dma_range_map;
676673
unsigned long order, base_pfn;
677674
struct iova_domain *iovad;
678675
int ret;
@@ -684,18 +681,18 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
684681

685682
/* Use the smallest supported page size for IOVA granularity */
686683
order = __ffs(domain->pgsize_bitmap);
687-
base_pfn = max_t(unsigned long, 1, base >> order);
684+
base_pfn = 1;
688685

689686
/* Check the domain allows at least some access to the device... */
690-
if (domain->geometry.force_aperture) {
687+
if (map) {
688+
dma_addr_t base = dma_range_map_min(map);
691689
if (base > domain->geometry.aperture_end ||
692-
limit < domain->geometry.aperture_start) {
690+
dma_range_map_max(map) < domain->geometry.aperture_start) {
693691
pr_warn("specified DMA range outside IOMMU capability\n");
694692
return -EFAULT;
695693
}
696694
/* ...then finally give it a kicking to make sure it fits */
697-
base_pfn = max_t(unsigned long, base_pfn,
698-
domain->geometry.aperture_start >> order);
695+
base_pfn = max(base, domain->geometry.aperture_start) >> order;
699696
}
700697

701698
/* start_pfn is always nonzero for an already-initialised domain */
@@ -1760,7 +1757,7 @@ void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit)
17601757
* underlying IOMMU driver needs to support via the dma-iommu layer.
17611758
*/
17621759
if (iommu_is_dma_domain(domain)) {
1763-
if (iommu_dma_init_domain(domain, dma_base, dma_limit, dev))
1760+
if (iommu_dma_init_domain(domain, dev))
17641761
goto out_err;
17651762
dev->dma_ops = &iommu_dma_ops;
17661763
}

0 commit comments

Comments
 (0)