Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 4 additions & 58 deletions drivers/iommu/apple-dart-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,25 @@ static void apple_dart_test_consumer_window(struct kunit *test)
dart->pgsize = SZ_16K;
dev->of_node = &node;

/*
* J613's common scanout alias survives both DART defaults, but only
* inside each stream's own window: the external DART excludes its top
* 64 KiB, so the shared aperture must too.
*/
/* J613's common scanout aperture must survive both DART defaults. */
for (i = 0; i < 2; i++) {
dart->dma_min = i ? 0 : 0x10000000000;
dart->dma_max = i ? 0xffffeffff : 0x10fffffffff;
ret = apple_dart_parse_dma_window(dev, dart, &args,
&dma_min, &dma_max);
KUNIT_EXPECT_EQ(test, ret, 0);
KUNIT_EXPECT_EQ(test, dma_min, (dma_addr_t)0x10000000000);
KUNIT_EXPECT_EQ(test, dma_max,
(dma_addr_t)(i ? 0x10ffffeffff : 0x10fffffffff));
KUNIT_EXPECT_EQ(test, dma_max, (dma_addr_t)0x10fffffffff);
}

/* A five-cell stream window narrows the consumer aperture the same way. */
/* Domain allocation gives the consumer priority over five-cell args too. */
args.args_count = 5;
args.args[3] = 0xf;
args.args[4] = 0xffff0000;
ret = apple_dart_parse_dma_window(dev, dart, &args, &dma_min, &dma_max);
KUNIT_EXPECT_EQ(test, ret, 0);
KUNIT_EXPECT_EQ(test, dma_min, (dma_addr_t)0x10000000000);
KUNIT_EXPECT_EQ(test, dma_max, (dma_addr_t)0x10ffffeffff);
KUNIT_EXPECT_EQ(test, dma_max, (dma_addr_t)0x10fffffffff);

/* An override must not make a malformed stream specifier acceptable. */
args.args[3] = 0;
Expand All @@ -154,54 +149,6 @@ static void apple_dart_test_consumer_window(struct kunit *test)
KUNIT_EXPECT_EQ(test, dma_max, (dma_addr_t)0xffffeffff);
}

static void apple_dart_test_disjoint_consumer_window(struct kunit *test)
{
/* 2 GiB at the 1 TiB alias; its low bits start at 2 GiB. */
__be64 value[] = { cpu_to_be64(0x10080000000), cpu_to_be64(SZ_2G) };
struct property property = {
.name = "apple,dma-range",
.length = sizeof(value),
.value = value,
};
struct device_node node = { .properties = &property };
struct of_phandle_args args = { .args_count = 1 };
struct apple_dart *dart;
struct device *dev;
dma_addr_t dma_min, dma_max;

dev = kunit_device_register(test, "apple-dart-disjoint-window-test");
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
dart = kunit_kzalloc(test, sizeof(*dart), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, dart);
dart->ias = 36;
dart->pgsize = SZ_16K;
dev->of_node = &node;

/* The stream's window ends where the consumer's low bits begin. */
dart->dma_min = 0;
dart->dma_max = SZ_2G - 1;
KUNIT_EXPECT_EQ(test, apple_dart_parse_dma_window(dev, dart, &args,
&dma_min, &dma_max), -EINVAL);

/* The same holds for a stream window given by a five-cell specifier. */
dart->dma_max = DMA_BIT_MASK(dart->ias);
args.args_count = 5;
args.args[1] = 0x100;
args.args[2] = 0;
args.args[3] = 0;
args.args[4] = 0x80000000;
KUNIT_EXPECT_EQ(test, apple_dart_parse_dma_window(dev, dart, &args,
&dma_min, &dma_max), -EINVAL);

/* One page of overlap is enough, and only that page is returned. */
args.args[4] = 0x80004000;
KUNIT_EXPECT_EQ(test, apple_dart_parse_dma_window(dev, dart, &args,
&dma_min, &dma_max), 0);
KUNIT_EXPECT_EQ(test, dma_min, (dma_addr_t)0x10080000000);
KUNIT_EXPECT_EQ(test, dma_max, (dma_addr_t)0x10080003fff);
dev->of_node = NULL;
}

static void apple_dart_test_invalid_consumer_window(struct kunit *test)
{
static const struct {
Expand Down Expand Up @@ -1486,7 +1433,6 @@ static struct kunit_case apple_dart_test_cases[] = {
KUNIT_CASE(apple_dart_test_topology),
KUNIT_CASE(apple_dart_test_iommu_specifiers),
KUNIT_CASE(apple_dart_test_consumer_window),
KUNIT_CASE(apple_dart_test_disjoint_consumer_window),
KUNIT_CASE(apple_dart_test_invalid_consumer_window),
KUNIT_CASE(apple_dart_test_independent_remap_roots),
KUNIT_CASE(apple_dart_test_before_attach),
Expand Down
49 changes: 24 additions & 25 deletions drivers/iommu/apple-dart.c
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,9 @@ struct apple_dart_domain {
struct apple_dart_dcp *dcp;

bool finalized;
bool has_dma_range;
dma_addr_t dma_min;
dma_addr_t dma_max;
u64 mask;
dma_addr_t dma_offset;
struct mutex init_lock;
Expand Down Expand Up @@ -3619,9 +3622,8 @@ static int __apple_dart_finalize_domain(struct apple_dart_domain *dart_domain,
{
struct apple_dart *dart = cfg->stream_maps[0].dart;
struct io_pgtable_cfg pgtbl_cfg;
/* of_xlate already narrowed this to any consumer apple,dma-range. */
dma_addr_t dma_min = cfg->dma_min;
dma_addr_t dma_max = cfg->dma_max;
dma_addr_t dma_min = dart_domain->has_dma_range ? dart_domain->dma_min : cfg->dma_min;
dma_addr_t dma_max = dart_domain->has_dma_range ? dart_domain->dma_max : cfg->dma_max;
u64 translated_max;
u32 ias;
int ret = 0;
Expand Down Expand Up @@ -4150,8 +4152,22 @@ static struct iommu_domain *apple_dart_domain_alloc_paging(struct device *dev)

if (dev) {
struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
u64 window[2];
int ret;

if (of_property_present(dev->of_node, "apple,dma-range")) {
ret = of_property_read_u64_array(dev->of_node, "apple,dma-range",
window, 2);
if (!ret)
ret = apple_dart_dma_window(window[0], window[1],
&dart_domain->dma_max);
if (ret) {
kfree(dart_domain);
return ERR_PTR(ret);
}
dart_domain->dma_min = window[0];
dart_domain->has_dma_range = true;
}
ret = apple_dart_finalize_domain(dart_domain, cfg);
if (ret) {
kfree(dart_domain);
Expand Down Expand Up @@ -4262,19 +4278,15 @@ static int apple_dart_validate_dma_window(struct device *dev,
* Work out the DMA window of one "iommus" specifier. One cell is the stream
* ID alone and keeps the window of the DART node. Five cells add the 64-bit
* start and length of the window that the device uses. A consumer's
* apple,dma-range narrows either window: its bits above the DART's ias pick
* the alias the consumer uses, and its low bits are intersected with the
* stream's own window, masked the same way. The DART translates only the low
* bits, so an address outside the stream's window must never be handed out.
* apple,dma-range overrides either default, as it does at domain allocation.
*/
static int apple_dart_parse_dma_window(struct device *dev,
const struct apple_dart *dart,
const struct of_phandle_args *args,
dma_addr_t *dma_min,
dma_addr_t *dma_max)
{
dma_addr_t length, consumer_max, low_min, low_max;
u64 mask = DMA_BIT_MASK(dart->ias);
dma_addr_t length;
u64 window[2];
int ret;

Expand All @@ -4293,28 +4305,15 @@ static int apple_dart_parse_dma_window(struct device *dev,
return ret;
}

/* Narrow to the consumer aperture before intersecting its DART windows. */
/* Apply the consumer aperture before intersecting its DART windows. */
if (!of_property_present(dev->of_node, "apple,dma-range"))
return 0;
ret = of_property_read_u64_array(dev->of_node, "apple,dma-range",
window, ARRAY_SIZE(window));
if (ret)
return ret;
ret = apple_dart_validate_dma_window(dev, dart, window[0], window[1],
&consumer_max);
if (ret)
return ret;

low_min = max(window[0] & mask, *dma_min & mask);
low_max = min(consumer_max & mask, *dma_max & mask);
if (low_min > low_max) {
dev_err(dev, "DMA window %#llx..%#llx does not overlap stream window %pad..%pad\n",
window[0], (u64)consumer_max, dma_min, dma_max);
return -EINVAL;
}
*dma_min = (window[0] & ~mask) | low_min;
*dma_max = (window[0] & ~mask) | low_max;
return 0;
*dma_min = window[0];
return apple_dart_validate_dma_window(dev, dart, *dma_min, window[1], dma_max);
}

static int apple_dart_of_xlate(struct device *dev,
Expand Down
Loading