Skip to content

Commit 28e2d64

Browse files
marcanjannau
authored andcommitted
iommu: apple-dart: Support specifying the DMA aperture in the DT
Apple DARTs are often connected directly to devices that expect only a portion of their address space to be used for DMA (for example, because other ranges are mapped directly to something else). Add an apple,dma-range property to allow specifying this range. This range *can* be outside of the DART's IAS. In that case, it is assumed that the hardware truncates addresses and the page tables will only map the lower bits of the address. However, the specified range cannot straddle an IAS boundary (you cannot cover more than IAS worth of address space nor wrap). This corresponds to the vm-base and vm-size properties on the Apple device tree side of things. Signed-off-by: Hector Martin <marcan@marcan.st>
1 parent 177bffd commit 28e2d64

1 file changed

Lines changed: 43 additions & 8 deletions

File tree

drivers/iommu/apple-dart.c

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/io-pgtable.h>
2222
#include <linux/iommu.h>
2323
#include <linux/iopoll.h>
24+
#include <linux/minmax.h>
2425
#include <linux/module.h>
2526
#include <linux/of.h>
2627
#include <linux/of_address.h>
@@ -224,6 +225,9 @@ struct apple_dart {
224225
u32 supports_bypass : 1;
225226
u32 four_level : 1;
226227

228+
dma_addr_t dma_min;
229+
dma_addr_t dma_max;
230+
227231
struct iommu_group *sid2group[DART_MAX_STREAMS];
228232
struct iommu_device iommu;
229233

@@ -268,6 +272,7 @@ struct apple_dart_domain {
268272
struct io_pgtable_ops *pgtbl_ops;
269273

270274
bool finalized;
275+
u64 mask;
271276
struct mutex init_lock;
272277
struct apple_dart_atomic_stream_map stream_maps[MAX_DARTS_PER_DEVICE];
273278

@@ -540,7 +545,7 @@ static phys_addr_t apple_dart_iova_to_phys(struct iommu_domain *domain,
540545
if (!ops)
541546
return 0;
542547

543-
return ops->iova_to_phys(ops, iova);
548+
return ops->iova_to_phys(ops, iova & dart_domain->mask);
544549
}
545550

546551
static int apple_dart_map_pages(struct iommu_domain *domain, unsigned long iova,
@@ -554,8 +559,8 @@ static int apple_dart_map_pages(struct iommu_domain *domain, unsigned long iova,
554559
if (!ops)
555560
return -ENODEV;
556561

557-
return ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, gfp,
558-
mapped);
562+
return ops->map_pages(ops, iova & dart_domain->mask, paddr, pgsize,
563+
pgcount, prot, gfp, mapped);
559564
}
560565

561566
static size_t apple_dart_unmap_pages(struct iommu_domain *domain,
@@ -566,7 +571,8 @@ static size_t apple_dart_unmap_pages(struct iommu_domain *domain,
566571
struct apple_dart_domain *dart_domain = to_dart_domain(domain);
567572
struct io_pgtable_ops *ops = dart_domain->pgtbl_ops;
568573

569-
return ops->unmap_pages(ops, iova, pgsize, pgcount, gather);
574+
return ops->unmap_pages(ops, iova & dart_domain->mask, pgsize, pgcount,
575+
gather);
570576
}
571577

572578
static void
@@ -593,6 +599,8 @@ static int apple_dart_finalize_domain(struct apple_dart_domain *dart_domain,
593599
{
594600
struct apple_dart *dart = cfg->stream_maps[0].dart;
595601
struct io_pgtable_cfg pgtbl_cfg;
602+
dma_addr_t dma_max = dart->dma_max;
603+
u32 ias = min_t(u32, dart->ias, fls64(dma_max));
596604
int ret = 0;
597605
int i, j;
598606

@@ -613,7 +621,7 @@ static int apple_dart_finalize_domain(struct apple_dart_domain *dart_domain,
613621

614622
pgtbl_cfg = (struct io_pgtable_cfg){
615623
.pgsize_bitmap = dart->pgsize,
616-
.ias = dart->ias,
624+
.ias = ias,
617625
.oas = dart->oas,
618626
.coherent_walk = 1,
619627
.iommu_dev = dart->dev,
@@ -626,10 +634,16 @@ static int apple_dart_finalize_domain(struct apple_dart_domain *dart_domain,
626634
goto done;
627635
}
628636

637+
if (pgtbl_cfg.pgsize_bitmap == SZ_4K)
638+
dart_domain->mask = DMA_BIT_MASK(min_t(u32, dart->ias, 32));
639+
else if (pgtbl_cfg.apple_dart_cfg.n_levels == 3)
640+
dart_domain->mask = DMA_BIT_MASK(min_t(u32, dart->ias, 36));
641+
else if (pgtbl_cfg.apple_dart_cfg.n_levels == 4)
642+
dart_domain->mask = DMA_BIT_MASK(min_t(u32, dart->ias, 47));
643+
629644
dart_domain->domain.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
630-
dart_domain->domain.geometry.aperture_start = 0;
631-
dart_domain->domain.geometry.aperture_end =
632-
(dma_addr_t)DMA_BIT_MASK(pgtbl_cfg.ias);
645+
dart_domain->domain.geometry.aperture_start = dart->dma_min;
646+
dart_domain->domain.geometry.aperture_end = dma_max;
633647
dart_domain->domain.geometry.force_aperture = true;
634648

635649
dart_domain->finalized = true;
@@ -1137,6 +1151,7 @@ static int apple_dart_probe(struct platform_device *pdev)
11371151
struct resource *res;
11381152
struct apple_dart *dart;
11391153
struct device *dev = &pdev->dev;
1154+
u64 dma_range[2];
11401155

11411156
dart = devm_kzalloc(dev, sizeof(*dart), GFP_KERNEL);
11421157
if (!dart)
@@ -1199,6 +1214,26 @@ static int apple_dart_probe(struct platform_device *pdev)
11991214
break;
12001215
}
12011216

1217+
dart->dma_min = 0;
1218+
dart->dma_max = DMA_BIT_MASK(dart->ias);
1219+
1220+
ret = of_property_read_u64_array(dev->of_node, "apple,dma-range", dma_range, 2);
1221+
if (ret == -EINVAL) {
1222+
ret = 0;
1223+
} else if (ret) {
1224+
goto err_clk_disable;
1225+
} else {
1226+
dart->dma_min = dma_range[0];
1227+
dart->dma_max = dma_range[0] + dma_range[1] - 1;
1228+
if ((dart->dma_min ^ dart->dma_max) & ~DMA_BIT_MASK(dart->ias)) {
1229+
dev_err(&pdev->dev, "Invalid DMA range for ias=%d\n",
1230+
dart->ias);
1231+
goto err_clk_disable;
1232+
}
1233+
dev_info(&pdev->dev, "Limiting DMA range to %pad..%pad\n",
1234+
&dart->dma_min, &dart->dma_max);
1235+
}
1236+
12021237
if (dart->num_streams > DART_MAX_STREAMS) {
12031238
dev_err(&pdev->dev, "Too many streams (%d > %d)\n",
12041239
dart->num_streams, DART_MAX_STREAMS);

0 commit comments

Comments
 (0)