From 390b657ea242b12fea99924a0b64de88d4375b0c Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 26 Nov 2026 15:34:08 -0500 Subject: [PATCH 1/3] iommu/apple-dart: fail closed when an unmap's TLB retirement fails apple_dart_domain_flush_tlb_range() returns the first error of a DART that could not be powered, could not publish a locked root, or whose TLB invalidate failed or timed out. apple_dart_iotlb_sync() discarded it, and dma-iommu then freed the IOVA, and its caller the pages, while that DART could still translate them. Only managed streams were quarantined on an invalidate failure. Fail closed instead, in iotlb_sync and, for a flush-queue domain, in flush_iotlb_all, which retires its queued unmaps: - A DART that failed while it could be reached (powered by the flush, its commands not gated) has the domain's streams disabled, and each TCR must read back disabled: a disabled stream reaches no memory, whatever its TLB or tables still hold. - A DART that could not be reached (runtime PM did not resume it, or it is dark or closed for its owner's power cycle with a locked root left unpublished), or whose streams did not take the disable, cannot be shown to have stopped: panic, as the T6050 display sleep's live unmap already does, rather than let the pages be handed out again. - The domain then refuses mappings with -EIO until it is freed. Gated commands are not a failure: the DART's sleep list stopped every stream and flushed the TLB before gating them, and nothing translates until a restore. Borrowed DCP domains keep their own failure handling. A flush that succeeds changes nothing. Add KUnit cases that inject each failure: a hardware TLB invalidate timeout, a failed firmware invalidate, an unpublishable locked root (live, then with the display dark), a runtime-PM resume failure, and streams that do not take the disable; that gated commands and a clean flush leave the domain alone; and that a full flush fails closed only for a flush-queue domain. With the failure discarded as before, all the failure cases fail. Signed-off-by: DjDeveloperr --- drivers/iommu/Kconfig | 3 + drivers/iommu/apple-dart-test.c | 269 ++++++++++++++++++++++++++++++++ drivers/iommu/apple-dart.c | 153 +++++++++++++++--- 3 files changed, 404 insertions(+), 21 deletions(-) diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index 081f59673ccf1..3fe8f6904633c 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -355,6 +355,9 @@ config APPLE_DART_KUNIT_TEST roots retain their mappings and reject conflicting host entries. Verify that unmapping an IOVA maintains independent remap targets. That case requires kernel pages of at least 16 KiB. + Inject each way an unmap's TLB retirement can fail and verify that + the domain's streams are stopped, or the DART reported stuck, and + that the domain then refuses mappings. config S390_IOMMU def_bool y if S390 && PCI diff --git a/drivers/iommu/apple-dart-test.c b/drivers/iommu/apple-dart-test.c index b6b0d1690bd95..847ca26f21816 100644 --- a/drivers/iommu/apple-dart-test.c +++ b/drivers/iommu/apple-dart-test.c @@ -1481,6 +1481,268 @@ static void apple_dart_test_host_power_tables(struct kunit *test) } #endif +/* + * Unmap retirement. Each source of a failed flush leaves the domain refusing + * mappings. A DART that can be reached has the domain's streams stopped; one + * that cannot, or whose streams do not stop, is stuck. + */ +struct apple_dart_test_retire { + struct apple_dart *stuck; + int err; + unsigned int calls; +}; + +static void apple_dart_test_stuck(struct apple_dart *dart, int err) +{ + struct apple_dart_test_retire *retire = kunit_get_current_test()->priv; + + retire->stuck = dart; + retire->err = err; + retire->calls++; +} + +static int apple_dart_test_tlb_timeout(struct apple_dart_stream_map *stream_map) +{ + return -ETIMEDOUT; +} + +static int apple_dart_test_fw_invalidate_failure(struct apple_dart_stream_map *stream_map, + u64 lo, u64 hi) +{ + return -EIO; +} + +/* Writes reach everything but the TCRs, as on a DART that does not take them. */ +static void apple_dart_test_keep_tcr(struct apple_dart *dart, u32 value, u32 offset) +{ + if (offset >= DART_TCR(dart, 0) && offset <= DART_TCR(dart, dart->num_streams - 1)) + return; + writel(value, dart->regs + offset); +} + +/* A T8110 DART whose streams 0 and 1 translate for a strict domain. */ +static struct apple_dart_domain * +apple_dart_test_retire_domain(struct kunit *test, struct apple_dart_test_retire *retire, + struct apple_dart **dartp) +{ + struct apple_dart_domain *domain; + struct apple_dart *dart; + struct io_pgtable *iop; + int sid; + + dart = apple_dart_test_t8110(test, "apple-dart-retire"); + dart->num_streams = 2; + dart->pgsize = SZ_16K; + dev_set_drvdata(dart->dev, dart); + for (sid = 0; sid < 2; sid++) + writel(dart->hw->tcr_enabled, dart->regs + DART_TCR(dart, sid)); + + iop = kunit_kzalloc(test, sizeof(*iop), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, iop); + iop->cfg.apple_dart_cfg.n_ttbrs = 1; + domain = kunit_kzalloc(test, sizeof(*domain), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, domain); + domain->pgtbl_ops = &iop->ops; + domain->domain.type = IOMMU_DOMAIN_DMA; + domain->stream_maps[0].dart = dart; + atomic_long_set(&domain->stream_maps[0].sidmap[0], BIT(0) | BIT(1)); + + test->priv = retire; + kunit_activate_static_stub(test, apple_dart_retire_stuck, apple_dart_test_stuck); + *dartp = dart; + return domain; +} + +/* An unmap of the domain's first 16 KiB: the pages are freed after this. */ +static void apple_dart_test_unmap_sync(struct apple_dart_domain *domain) +{ + struct iommu_iotlb_gather gather = { .start = 0, .end = SZ_16K - 1 }; + + apple_dart_iotlb_sync(&domain->domain, &gather); +} + +static int apple_dart_test_map(struct apple_dart_domain *domain) +{ + size_t mapped = 0; + + return apple_dart_map_pages_unguarded(&domain->domain, 0, 0x10000, SZ_16K, 1, + IOMMU_READ | IOMMU_WRITE, GFP_KERNEL, &mapped); +} + +/* The domain's streams were stopped, and it takes no mapping any more. */ +static void apple_dart_test_expect_fenced(struct kunit *test, struct apple_dart_domain *domain, + struct apple_dart *dart, + struct apple_dart_test_retire *retire) +{ + KUNIT_EXPECT_EQ(test, retire->calls, 0U); + KUNIT_EXPECT_TRUE(test, domain->retire_failed); + KUNIT_EXPECT_EQ(test, readl(dart->regs + DART_TCR(dart, 0)), dart->hw->tcr_disabled); + KUNIT_EXPECT_EQ(test, readl(dart->regs + DART_TCR(dart, 1)), dart->hw->tcr_disabled); + KUNIT_EXPECT_EQ(test, apple_dart_test_map(domain), -EIO); + KUNIT_EXPECT_EQ(test, atomic_read(&dart->dev->power.usage_count), 0); +} + +/* A hardware TLB invalidate that times out */ +static void apple_dart_test_retire_tlb_timeout(struct kunit *test) +{ + struct apple_dart_test_retire retire = {}; + struct apple_dart_hw hw = apple_dart_hw_t8110; + struct apple_dart_domain *domain; + struct apple_dart *dart; + + domain = apple_dart_test_retire_domain(test, &retire, &dart); + hw.invalidate_tlb = apple_dart_test_tlb_timeout; + dart->hw = &hw; + apple_dart_test_unmap_sync(domain); + apple_dart_test_expect_fenced(test, domain, dart, &retire); + pm_runtime_disable(dart->dev); +} + +/* A firmware-coordinated range invalidate that fails */ +static void apple_dart_test_retire_fw_invalidate(struct kunit *test) +{ + struct apple_dart_test_retire retire = {}; + struct apple_dart_domain *domain; + struct apple_dart *dart; + + domain = apple_dart_test_retire_domain(test, &retire, &dart); + dart->fw_mirror = true; + kunit_activate_static_stub(test, apple_dart_fw_invalidate, + apple_dart_test_fw_invalidate_failure); + apple_dart_test_unmap_sync(domain); + apple_dart_test_expect_fenced(test, domain, dart, &retire); + pm_runtime_disable(dart->dev); +} + +/* + * A locked root that cannot be published: live, the streams are stopped; dark, + * the tables its power-up walks still map the pages, and the DART is stuck. + */ +static void apple_dart_test_retire_publish(struct kunit *test) +{ + struct apple_dart_test_retire retire = {}; + struct apple_dart_domain *domain; + struct apple_dart *dart; + struct io_pgtable *iop; + u64 live[] = { 0x1001, 0, 0, 0 }; + u64 owned[ARRAY_SIZE(live)] = {}; + u64 ours[] = { 0x3001, 0, 0, 0 }; + + domain = apple_dart_test_retire_domain(test, &retire, &dart); + iop = io_pgtable_ops_to_pgtable(domain->pgtbl_ops); + iop->cfg.apple_dart_cfg.ttbr[0] = ours; + dart->locked = true; + dart->pgsize = sizeof(live); + dart->locked_ttbr[0][0] = live; + dart->locked_owned[0][0] = owned; + + apple_dart_test_unmap_sync(domain); + apple_dart_test_expect_fenced(test, domain, dart, &retire); + KUNIT_EXPECT_EQ(test, live[0], 0x1001ULL); + + domain->retire_failed = false; + dart->display_dark = true; + apple_dart_test_unmap_sync(domain); + KUNIT_EXPECT_EQ(test, retire.calls, 1U); + KUNIT_EXPECT_PTR_EQ(test, retire.stuck, dart); + KUNIT_EXPECT_EQ(test, retire.err, -EBUSY); + KUNIT_EXPECT_TRUE(test, domain->retire_failed); + pm_runtime_disable(dart->dev); +} + +/* A DART runtime PM cannot resume: its registers are not touched, and it is stuck. */ +static void apple_dart_test_retire_pm(struct kunit *test) +{ + struct apple_dart_test_retire retire = {}; + struct apple_dart_domain *domain; + struct apple_dart *dart; + + domain = apple_dart_test_retire_domain(test, &retire, &dart); + scoped_guard(spinlock_irqsave, &dart->dev->power.lock) + dart->dev->power.runtime_error = -EIO; + apple_dart_test_unmap_sync(domain); + KUNIT_EXPECT_EQ(test, retire.calls, 1U); + KUNIT_EXPECT_PTR_EQ(test, retire.stuck, dart); + KUNIT_EXPECT_LT(test, retire.err, 0); + KUNIT_EXPECT_TRUE(test, domain->retire_failed); + KUNIT_EXPECT_EQ(test, readl(dart->regs + DART_TCR(dart, 0)), dart->hw->tcr_enabled); + KUNIT_EXPECT_EQ(test, apple_dart_test_map(domain), -EIO); + KUNIT_EXPECT_EQ(test, atomic_read(&dart->dev->power.usage_count), 0); + KUNIT_EXPECT_EQ(test, pm_runtime_set_active(dart->dev), 0); + pm_runtime_disable(dart->dev); +} + +/* Streams that do not take the disable: the DART is stuck. */ +static void apple_dart_test_retire_unfenced(struct kunit *test) +{ + struct apple_dart_test_retire retire = {}; + struct apple_dart_hw hw = apple_dart_hw_t8110; + struct apple_dart_domain *domain; + struct apple_dart *dart; + + domain = apple_dart_test_retire_domain(test, &retire, &dart); + hw.invalidate_tlb = apple_dart_test_tlb_timeout; + dart->hw = &hw; + kunit_activate_static_stub(test, apple_dart_writel, apple_dart_test_keep_tcr); + apple_dart_test_unmap_sync(domain); + KUNIT_EXPECT_EQ(test, retire.calls, 1U); + KUNIT_EXPECT_PTR_EQ(test, retire.stuck, dart); + KUNIT_EXPECT_EQ(test, retire.err, -ETIMEDOUT); + KUNIT_EXPECT_TRUE(test, domain->retire_failed); + pm_runtime_disable(dart->dev); +} + +/* + * Gated commands: the sleep list stopped the streams and flushed the TLB, so + * nothing fails, and the DART is not touched. A working flush changes + * nothing either. + */ +static void apple_dart_test_retire_gated_and_clean(struct kunit *test) +{ + struct apple_dart_test_retire retire = {}; + struct apple_dart_domain *domain; + struct apple_dart *dart; + + domain = apple_dart_test_retire_domain(test, &retire, &dart); + apple_dart_quiesce_commands(dart->dev); + apple_dart_test_unmap_sync(domain); + KUNIT_EXPECT_EQ(test, retire.calls, 0U); + KUNIT_EXPECT_FALSE(test, domain->retire_failed); + KUNIT_EXPECT_EQ(test, readl(dart->regs + DART_TCR(dart, 0)), dart->hw->tcr_enabled); + + KUNIT_EXPECT_EQ(test, apple_dart_resume_commands(dart->dev), 0); + apple_dart_test_unmap_sync(domain); + KUNIT_EXPECT_EQ(test, retire.calls, 0U); + KUNIT_EXPECT_FALSE(test, domain->retire_failed); + KUNIT_EXPECT_EQ(test, readl(dart->regs + DART_TCR(dart, 1)), dart->hw->tcr_enabled); + KUNIT_EXPECT_EQ(test, atomic_read(&dart->dev->power.usage_count), 0); + pm_runtime_disable(dart->dev); +} + +/* + * A full flush retires the queued unmaps of a flush-queue domain only: there a + * failure stops the streams; in a strict domain it does not. + */ +static void apple_dart_test_retire_flush_queue(struct kunit *test) +{ + struct apple_dart_test_retire retire = {}; + struct apple_dart_hw hw = apple_dart_hw_t8110; + struct apple_dart_domain *domain; + struct apple_dart *dart; + + domain = apple_dart_test_retire_domain(test, &retire, &dart); + hw.invalidate_tlb = apple_dart_test_tlb_timeout; + dart->hw = &hw; + apple_dart_flush_iotlb_all(&domain->domain); + KUNIT_EXPECT_FALSE(test, domain->retire_failed); + KUNIT_EXPECT_EQ(test, readl(dart->regs + DART_TCR(dart, 0)), dart->hw->tcr_enabled); + + domain->domain.type = IOMMU_DOMAIN_DMA_FQ; + apple_dart_flush_iotlb_all(&domain->domain); + apple_dart_test_expect_fenced(test, domain, dart, &retire); + pm_runtime_disable(dart->dev); +} + static struct kunit_case apple_dart_test_cases[] = { KUNIT_CASE(apple_dart_test_strict_engines), KUNIT_CASE(apple_dart_test_topology), @@ -1494,6 +1756,13 @@ static struct kunit_case apple_dart_test_cases[] = { KUNIT_CASE(apple_dart_test_sync_unknown_roots), KUNIT_CASE(apple_dart_test_firmware_roots), KUNIT_CASE(apple_dart_test_unmap_flush_after_publish_failure), + KUNIT_CASE(apple_dart_test_retire_tlb_timeout), + KUNIT_CASE(apple_dart_test_retire_fw_invalidate), + KUNIT_CASE(apple_dart_test_retire_publish), + KUNIT_CASE(apple_dart_test_retire_pm), + KUNIT_CASE(apple_dart_test_retire_unfenced), + KUNIT_CASE(apple_dart_test_retire_gated_and_clean), + KUNIT_CASE(apple_dart_test_retire_flush_queue), KUNIT_CASE(apple_dart_test_gated_commands), KUNIT_CASE(apple_dart_test_resume_commands_failure), KUNIT_CASE(apple_dart_test_tunnel_restore), diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index c630e5a248b15..5665e1be41e97 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -866,6 +866,11 @@ struct apple_dart_domain { struct apple_dart_dcp *dcp; bool finalized; + /* + * An unmap's retirement failed: no mapping until the domain is freed + * (apple_dart_retire_check()). + */ + bool retire_failed; u64 mask; dma_addr_t dma_offset; struct mutex init_lock; @@ -1940,6 +1945,9 @@ static int apple_dart_fw_invalidate(struct apple_dart_stream_map *stream_map, unsigned long flags; int sid, ret = 0; +#if IS_ENABLED(CONFIG_APPLE_DART_KUNIT_TEST) + KUNIT_STATIC_STUB_REDIRECT(apple_dart_fw_invalidate, stream_map, lo, hi); +#endif if (entries > DART_MAX_ROOT_ENTRIES) return 0; @@ -2946,14 +2954,96 @@ static int __apple_dart_dcp_domain_flush(struct apple_dart_domain *domain) return ret; } +/* + * A flush that retires unmapped pages: its caller frees them, and their IOVA, + * once it returns. @err is the first DART that failed to publish or invalidate + * them; @stuck is one that could not then be shown to have stopped its + * streams' DMA. + */ +struct apple_dart_retire { + int err; + struct apple_dart *stuck; +}; + +/* + * Stops the DMA of @stream's streams, which a failed flush left able to reach + * unmapped pages: their TCRs disabled, whatever their TLBs and tables still + * hold. Whether every one reads back disabled; a locked DART may not take it. + */ +static bool apple_dart_retire_fence(struct apple_dart_stream_map *stream) +{ + struct apple_dart *dart = stream->dart; + int sid; + + apple_dart_hw_disable_dma(stream); + for_each_set_bit(sid, stream->sidmap, dart->num_streams) + if (apple_dart_readl(dart, DART_TCR(dart, sid)) != dart->hw->tcr_disabled) + return false; + return true; +} + +/* + * @stream failed to retire unmapped pages (@ret). If its DART can be reached + * now (@reachable: powered, its commands not gated), its streams are stopped; + * otherwise, or if they do not stop, it is stuck. + */ +static void apple_dart_retire_fail(struct apple_dart_retire *retire, + struct apple_dart_stream_map *stream, int ret, bool reachable) +{ + if (!retire || !ret) + return; + retire->err = retire->err ?: ret; + if (reachable && apple_dart_retire_fence(stream)) { + dev_err_ratelimited(stream->dart->dev, + "streams %*pbl stopped: unmapped pages not retired: %d\n", + (int)stream->dart->num_streams, stream->sidmap, ret); + return; + } + retire->stuck = retire->stuck ?: stream->dart; +} + +/* + * A DART that may still translate unmapped pages, which the caller frees + * next, and whose streams could not be stopped: as the display sleep's live + * unmap, stop rather than hand that memory out again. + */ +static void apple_dart_retire_stuck(struct apple_dart *dart, int err) +{ +#if IS_ENABLED(CONFIG_APPLE_DART_KUNIT_TEST) + KUNIT_STATIC_STUB_REDIRECT(apple_dart_retire_stuck, dart, err); +#endif + panic("%s: unmapped pages not retired (%d) and its streams not stopped; DMA backing retained\n", + dev_name(dart->dev), err); +} + +/* + * After a flush that retires unmapped pages: if one failed, the domain takes + * no mapping any more, and a DART that could not be stopped stops the machine. + */ +static void apple_dart_retire_check(struct apple_dart_domain *domain, + struct apple_dart_retire *retire) +{ + if (!retire->err) + return; + WRITE_ONCE(domain->retire_failed, true); + if (retire->stuck) + apple_dart_retire_stuck(retire->stuck, retire->err); +} + +/* + * @retire, for a flush whose caller frees what was unmapped: a DART that fails + * to publish or invalidate has its streams stopped, or is stuck. See + * apple_dart_retire_check(). + */ static int apple_dart_domain_flush_tlb_range(struct apple_dart_domain *domain, - bool range, u64 first, u64 last) + bool range, u64 first, u64 last, + struct apple_dart_retire *retire) { struct apple_dart_atomic_stream_map *map; struct apple_dart_stream_map stream; struct io_pgtable_cfg *cfg; const u32 op = DART_T8110_TLB_CMD_OP_FLUSH_SID; - int i, j, ret, gate, err = 0; + int i, j, ret, published, gate, err = 0; u64 start, end; if (domain->dcp) @@ -2979,6 +3069,8 @@ static int apple_dart_domain_flush_tlb_range(struct apple_dart_domain *domain, ret = stream.dart->locked ? apple_dart_hw_sync_locked(cfg, &stream, true) : 0; WRITE_ONCE(stream.dart->display_tlb_stale, true); + /* Unpublished, the tables its power-up walks still map them. */ + apple_dart_retire_fail(retire, &stream, ret, false); err = err ?: ret; continue; } @@ -2990,6 +3082,7 @@ static int apple_dart_domain_flush_tlb_range(struct apple_dart_domain *domain, ret = stream.dart->locked ? apple_dart_hw_sync_locked(cfg, &stream, true) : 0; apple_dart_sleep_writer_put(stream.dart, gate); + apple_dart_retire_fail(retire, &stream, ret, false); err = err ?: ret; continue; } @@ -2998,15 +3091,17 @@ static int apple_dart_domain_flush_tlb_range(struct apple_dart_domain *domain, apple_dart_sleep_writer_put(stream.dart, gate); dev_err_ratelimited(stream.dart->dev, "failed to power DART for TLB synchronization: %d\n", ret); + /* Unpowered by us, its TLB cannot be shown dropped. */ + apple_dart_retire_fail(retire, &stream, ret, false); err = err ?: ret; continue; } - ret = stream.dart->locked ? + published = stream.dart->locked ? apple_dart_hw_sync_locked(cfg, &stream, true) : 0; - if (ret) { + if (published) { dev_err_ratelimited(stream.dart->dev, - "failed to publish host page-table entries: %d\n", ret); - err = err ?: ret; + "failed to publish host page-table entries: %d\n", published); + err = err ?: published; } /* The range registers take the DVA the device issues. */ start = first + domain->dma_offset; @@ -3024,6 +3119,14 @@ static int apple_dart_domain_flush_tlb_range(struct apple_dart_domain *domain, ret = stream.dart->hw->invalidate_tlb(&stream); if (ret && stream.dart->managed_sids) apple_dart_managed_quarantine(stream.dart); + /* + * Gated commands mean the DART's sleep list stopped every stream and + * flushed its TLB first: nothing translates until a restore. Gated, + * its registers are not touched either. + */ + apple_dart_retire_fail(retire, &stream, + published ?: (ret == -EHOSTDOWN ? 0 : ret), + !READ_ONCE(stream.dart->commands_gated)); pm_runtime_put(stream.dart->dev); apple_dart_sleep_writer_put(stream.dart, gate); err = err ?: ret; @@ -3031,26 +3134,31 @@ static int apple_dart_domain_flush_tlb_range(struct apple_dart_domain *domain, return err; } -static void apple_dart_domain_flush_tlb(struct apple_dart_domain *domain) -{ - (void)apple_dart_domain_flush_tlb_range(domain, false, 0, 0); -} - static void apple_dart_flush_iotlb_all(struct iommu_domain *domain) { - apple_dart_v27_domain_hold(to_dart_domain(domain)); - apple_dart_domain_flush_tlb(to_dart_domain(domain)); - apple_dart_v27_domain_drop(to_dart_domain(domain)); + struct apple_dart_domain *dart_domain = to_dart_domain(domain); + struct apple_dart_retire retire = {}; + /* With a flush queue, this retires the unmaps queued until now. */ + bool retiring = domain->type == IOMMU_DOMAIN_DMA_FQ; + + apple_dart_v27_domain_hold(dart_domain); + (void)apple_dart_domain_flush_tlb_range(dart_domain, false, 0, 0, + retiring ? &retire : NULL); + apple_dart_v27_domain_drop(dart_domain); + apple_dart_retire_check(dart_domain, &retire); } static void apple_dart_iotlb_sync(struct iommu_domain *domain, struct iommu_iotlb_gather *gather) { - apple_dart_v27_domain_hold(to_dart_domain(domain)); - (void)apple_dart_domain_flush_tlb_range(to_dart_domain(domain), - gather->start <= gather->end, - gather->start, gather->end); - apple_dart_v27_domain_drop(to_dart_domain(domain)); + struct apple_dart_domain *dart_domain = to_dart_domain(domain); + struct apple_dart_retire retire = {}; + + apple_dart_v27_domain_hold(dart_domain); + (void)apple_dart_domain_flush_tlb_range(dart_domain, gather->start <= gather->end, + gather->start, gather->end, &retire); + apple_dart_v27_domain_drop(dart_domain); + apple_dart_retire_check(dart_domain, &retire); } static int apple_dart_iotlb_sync_map(struct iommu_domain *domain, @@ -3062,7 +3170,7 @@ static int apple_dart_iotlb_sync_map(struct iommu_domain *domain, return -EINVAL; apple_dart_v27_domain_hold(to_dart_domain(domain)); ret = apple_dart_domain_flush_tlb_range(to_dart_domain(domain), true, - iova, iova + size - 1); + iova, iova + size - 1, NULL); apple_dart_v27_domain_drop(to_dart_domain(domain)); return ret; } @@ -3411,6 +3519,9 @@ static int apple_dart_map_pages_unguarded(struct iommu_domain *domain, unsigned if (dart_domain->stream_maps[0].dart && READ_ONCE(dart_domain->stream_maps[0].dart->managed_failed)) return -EIO; + /* Its streams are stopped: see apple_dart_retire_check(). */ + if (READ_ONCE(dart_domain->retire_failed)) + return -EIO; dva = (iova + dart_domain->dma_offset) & dart_domain->mask; ret = apple_dart_check_fw_map(dart_domain, dva, paddr, pgsize * pgcount, @@ -3501,7 +3612,7 @@ static size_t apple_dart_unmap_pages_v27(struct iommu_domain *domain, * the flush until restore. Never report success over a failed live * invalidation: that could let the caller reuse DMA-visible memory. */ - ret = apple_dart_domain_flush_tlb_range(dd, false, 0, U64_MAX); + ret = apple_dart_domain_flush_tlb_range(dd, false, 0, U64_MAX, NULL); if (ret) panic("DCP DART live unmap invalidation failed: %d; DMA backing retained", ret); } From 4b4eba5cca547ab89fa02ee7e775f399431838f4 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 26 Nov 2026 15:34:09 -0500 Subject: [PATCH 2/3] ci: run the Apple DART KUnit suite No check ran the apple-dart KUnit suite. Add drivers/iommu/apple-dart.kunitconfig, list it in MAINTAINERS, and run it in the KUnit matrix; the workflow already starts on drivers/iommu changes. Signed-off-by: DjDeveloperr --- .github/workflows/apple-build-kunit.yml | 1 + MAINTAINERS | 1 + drivers/iommu/apple-dart.kunitconfig | 6 ++++++ 3 files changed, 8 insertions(+) create mode 100644 drivers/iommu/apple-dart.kunitconfig diff --git a/.github/workflows/apple-build-kunit.yml b/.github/workflows/apple-build-kunit.yml index 51354983d8220..abf8d04b164d6 100644 --- a/.github/workflows/apple-build-kunit.yml +++ b/.github/workflows/apple-build-kunit.yml @@ -152,6 +152,7 @@ jobs: # adds, is already on by default on arm64. - { suite: avd, kunitconfig: drivers/media/platform/apple/avd/tests/.kunitconfig } - { suite: ave, kunitconfig: drivers/media/platform/apple/ave/.kunitconfig } + - { suite: apple-dart, kunitconfig: drivers/iommu/apple-dart.kunitconfig } - { suite: isp, kunitconfig: drivers/media/platform/apple/isp/.kunitconfig } - { suite: jpeg, kunitconfig: drivers/media/platform/apple/jpeg/.kunitconfig } - { suite: prores, kunitconfig: drivers/media/platform/apple/prores/.kunitconfig } diff --git a/MAINTAINERS b/MAINTAINERS index 798e34ac61c30..66fc6df8160c5 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2807,6 +2807,7 @@ F: drivers/iommu/apple-dart-power.h F: drivers/iommu/apple-dart-sleep.c F: drivers/iommu/apple-dart-test.c F: drivers/iommu/apple-dart.c +F: drivers/iommu/apple-dart.kunitconfig F: drivers/iommu/io-pgtable-dart.c F: drivers/irqchip/irq-apple-aic.c F: drivers/mfd/macsmc*.c diff --git a/drivers/iommu/apple-dart.kunitconfig b/drivers/iommu/apple-dart.kunitconfig new file mode 100644 index 0000000000000..b40595da7aea2 --- /dev/null +++ b/drivers/iommu/apple-dart.kunitconfig @@ -0,0 +1,6 @@ +CONFIG_KUNIT=y +CONFIG_ARCH_APPLE=y +CONFIG_PM=y +CONFIG_IOMMU_SUPPORT=y +CONFIG_APPLE_DART=y +CONFIG_APPLE_DART_KUNIT_TEST=y From 4e398880677b123ad0769b35e255e6ecc9b56db9 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 26 Nov 2026 15:46:56 -0500 Subject: [PATCH 3/3] iommu/apple-dart: pass the retirement record at every flush caller apple_dart_domain_flush_tlb_range() took a fifth argument in the last change, but the two flushes of the DCP shared-buffer publication in apple-dart-dcp-shared.h, built with APPLE_DART_J713_DCP, still passed four. Both publish a new mapping on a DCP domain, which never reaches the retirement record: pass NULL. Update the host harnesses that compile the production functions: - apple-dart-range-tests and bcm4377-radio-sids pass NULL and stub apple_dart_retire_fail(), asserting that their map and attach flushes carry no record; their stand-in DART gains commands_gated. - dart-s2idle-retention's stand-in domain gains retire_failed, which apple_dart_map_pages_unguarded() reads. - The apple-dart-fw fixture defines IS_ENABLED() as off, so the KUnit-only hook in apple_dart_fw_invalidate() drops out. In the CLOSED-gate branch, record the failure before putting the writer's reference, which leaves the put, error and continue lines together as dart-sleep-writer-tests' negative control expects. Signed-off-by: DjDeveloperr --- drivers/iommu/apple-dart-dcp-shared.h | 4 +-- drivers/iommu/apple-dart.c | 2 +- tools/asahi/apple-dart-fw/fixture.c | 2 ++ tools/asahi/apple-dart-range-tests.py | 34 +++++++++++-------- .../drivers/apple/bcm4377-radio-sids.py | 8 +++-- .../drivers/apple/dart-s2idle-retention.py | 1 + 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/drivers/iommu/apple-dart-dcp-shared.h b/drivers/iommu/apple-dart-dcp-shared.h index 2396ac5e1d33c..cb48f6af17ffc 100644 --- a/drivers/iommu/apple-dart-dcp-shared.h +++ b/drivers/iommu/apple-dart-dcp-shared.h @@ -125,7 +125,7 @@ static int __apple_dart_dcp_shared_alloc(struct device *dev, struct device *piod if (!ret && mapped != size) ret = -EIO; if (!ret) - ret = apple_dart_domain_flush_tlb_range(a->domain, false, 0, 0); + ret = apple_dart_domain_flush_tlb_range(a->domain, false, 0, 0, NULL); if (!ret) ret = apple_dart_dcp_shared_verify(a, object); if (!ret) @@ -238,7 +238,7 @@ int apple_dart_dcp_shared_alloc_link(struct device *dev, size_t size, u64 *phys, if (!ret && mapped != size) ret = -EIO; if (!ret) - ret = apple_dart_domain_flush_tlb_range(a->domain, false, 0, 0); + ret = apple_dart_domain_flush_tlb_range(a->domain, false, 0, 0, NULL); if (!ret) ret = apple_dart_dcp_shared_verify(a, object); if (!ret) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 5665e1be41e97..5c48e6b5ebf30 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -3081,8 +3081,8 @@ static int apple_dart_domain_flush_tlb_range(struct apple_dart_domain *domain, if (gate == APPLE_DART_WRITER_CLOSED) { ret = stream.dart->locked ? apple_dart_hw_sync_locked(cfg, &stream, true) : 0; - apple_dart_sleep_writer_put(stream.dart, gate); apple_dart_retire_fail(retire, &stream, ret, false); + apple_dart_sleep_writer_put(stream.dart, gate); err = err ?: ret; continue; } diff --git a/tools/asahi/apple-dart-fw/fixture.c b/tools/asahi/apple-dart-fw/fixture.c index f8fd490ddc306..b1e946b4dfb94 100644 --- a/tools/asahi/apple-dart-fw/fixture.c +++ b/tools/asahi/apple-dart-fw/fixture.c @@ -18,6 +18,8 @@ typedef uint8_t u8;typedef uint32_t u32;typedef uint64_t u64;typedef uint64_t ph #define GFP_KERNEL 0 #define MEMREMAP_WB 0 #define READ_ONCE(x) (x) +/* Kernel options, all off on the host: KUnit-only hooks drop out. */ +#define IS_ENABLED(option) 0 #define WRITE_ONCE(x,v) ((x)=(v)) #define dma_wmb() ((void)0) #define IS_ALIGNED(x,a) (((x)&((a)-1))==0) diff --git a/tools/asahi/apple-dart-range-tests.py b/tools/asahi/apple-dart-range-tests.py index 8939de0a74e85..43b09481d3e62 100644 --- a/tools/asahi/apple-dart-range-tests.py +++ b/tools/asahi/apple-dart-range-tests.py @@ -45,7 +45,7 @@ def main(): struct apple_dart; struct apple_dart_stream_map { struct apple_dart *dart; unsigned long sidmap[1]; }; struct apple_dart_hw { int (*invalidate_tlb)(struct apple_dart_stream_map *); bool gen3; }; -struct apple_dart { int num_streams, locked, version; bool fw_mirror, fw_handoff, managed_sids, display_dark, display_tlb_stale; void *dev; struct apple_dart_hw *hw; }; +struct apple_dart { int num_streams, locked, version; bool fw_mirror, fw_handoff, managed_sids, display_dark, display_tlb_stale, commands_gated; void *dev; struct apple_dart_hw *hw; }; struct apple_dart_atomic_stream_map { struct apple_dart *dart; unsigned long sidmap[1]; }; struct apple_dart_domain { void *pgtbl_ops; bool dcp; struct apple_dart_atomic_stream_map streams[2]; int count; @@ -76,6 +76,10 @@ def main(): static int apple_dart_sleep_writer_get(struct apple_dart *dart) { writers++; return APPLE_DART_WRITER_COUNTED; } static void apple_dart_sleep_writer_put(struct apple_dart *dart, int gate) { assert(gate==APPLE_DART_WRITER_COUNTED && writers>0); writers--; } static void apple_dart_sleep_release_failed(void) { assert(!"sleep writer refused"); } +/* Map flushes: nothing is retired, so no retirement record. */ +struct apple_dart_retire; +static void apple_dart_retire_fail(struct apple_dart_retire *retire, + struct apple_dart_stream_map *stream, int ret, bool reachable) { assert(!retire); } ''' source += production source += r''' @@ -89,50 +93,50 @@ def main(): struct io_pgtable pg={0}; struct apple_dart_domain domain={.pgtbl_ops=&pg,.count=1,.dma_offset=1ULL<<40,.mask=(1ULL<<36)-1}; domain.streams[0].dart=&dart; domain.streams[0].sidmap[0]=1; - assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)); + assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)); assert(ranges==1 && fulls==0 && syncs==1 && powered==0); assert(seen_first==(1ULL<<40)+0x4000 && seen_last==(1ULL<<40)+0x7fff); reset(); domain.dma_offset=0; - assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)); + assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)); assert(seen_first==0x4000 && seen_last==0x7fff); reset(); dart.version=0x201; - assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)); + assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)); assert(fulls==1 && ranges==0); reset(); dart.version=0x202; dart.locked=0; - assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)); + assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)); assert(fulls==1 && ranges==0 && syncs==0); reset(); dart.locked=1; sync_error=-EBUSY; - assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)==-EBUSY); + assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)==-EBUSY); assert(ranges==1 && fulls==0 && powered==0); reset(); power_error=-EHOSTDOWN; - assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)==-EHOSTDOWN); + assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)==-EHOSTDOWN); assert(syncs==0 && ranges==0 && fulls==0 && powered==0); reset(); range_error=-ETIMEDOUT; - assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)==-ETIMEDOUT); + assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)==-ETIMEDOUT); assert(ranges==1 && powered==0); reset(); dart.managed_sids=true; range_error=-ETIMEDOUT; - assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)==-ETIMEDOUT); + assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)==-ETIMEDOUT); assert(ranges==1 && quarantines==1 && powered==0); reset(); sync_error=-EBUSY; range_error=-ETIMEDOUT; - assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)==-EBUSY); + assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)==-EBUSY); assert(ranges==1 && quarantines==1 && powered==0); reset(); dart.fw_mirror=true; - assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)); + assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)); assert(firmware_flushes==1 && ranges==0 && syncs==1); reset(); dart.fw_mirror=false; dart.fw_handoff=true; - assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)); + assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)); assert(firmware_flushes==1 && ranges==0); reset(); dart.fw_handoff=false; domain.dcp=true; - assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)==-EAGAIN); + assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)==-EAGAIN); assert(dcp_flushes==1 && ranges==0 && syncs==0); reset(); domain.dcp=false; struct apple_dart second=dart; second.dev=(void *)1; domain.streams[1].dart=&second; domain.streams[1].sidmap[0]=1; domain.count=2; power_error=-EHOSTDOWN; - assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)==-EHOSTDOWN); + assert(apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)==-EHOSTDOWN); assert(ranges==1 && syncs==1 && powered==0); reset(); domain.count=1; domain.streams[0].sidmap[0]=0; - assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff)); + assert(!apple_dart_domain_flush_tlb_range(&domain,true,0x4000,0x7fff,NULL)); assert(syncs==0 && ranges==0 && fulls==0 && powered==0 && writers==0); puts("PASS: device DVA, low window, legacy/unlocked fallback, sync/power/command errors and empty stream"); } diff --git a/tools/testing/selftests/drivers/apple/bcm4377-radio-sids.py b/tools/testing/selftests/drivers/apple/bcm4377-radio-sids.py index bf0ffb0b06e98..251ae04f8ffbc 100755 --- a/tools/testing/selftests/drivers/apple/bcm4377-radio-sids.py +++ b/tools/testing/selftests/drivers/apple/bcm4377-radio-sids.py @@ -114,7 +114,7 @@ def main(): bool locked, managed_failed, managed_lock, retained, retained_mtp, supports_bypass; bool four_level, per_stream, tunneled, fw_mirror, fw_handoff; bool pending, preserve_display; - bool isp_host, cold_ane, live_mtp, display_dark, display_tlb_stale; + bool isp_host, cold_ane, live_mtp, display_dark, display_tlb_stale, commands_gated; bool cold_gen3, cold_gen3_pcie; u32 cold_gen3_sids; void *tunables; @@ -288,6 +288,10 @@ def main(): static void apple_dart_sleep_writer_put(struct apple_dart *d, int gate) { (void)d; assert(gate==writer_gate && writer_refs>0); writer_refs--; } static void apple_dart_sleep_release_failed(void) { abort(); } +/* Attach and map flushes: nothing is retired, so no retirement record. */ +struct apple_dart_retire; +static void apple_dart_retire_fail(struct apple_dart_retire *retire, + struct apple_dart_stream_map *stream, int ret, bool reachable) { if (retire) abort(); } static u32 readl(const void *p) { (void)p; abort(); } static int apple_dart_t8110_hw_tlb_command_range(struct apple_dart_stream_map *p, int op, bool range, unsigned long a, unsigned long b) @@ -835,7 +839,7 @@ def main(): /* Later map/unmap synchronization also fails closed on a TLB timeout. */ d.managed_failed=false; d.regs[DART_TCR(&d,1)/4]=1; d.regs[DART_TCR(&d,2)/4]=1; - assert(apple_dart_domain_flush_tlb_range(&attach,false,0,0)==-ETIMEDOUT); + assert(apple_dart_domain_flush_tlb_range(&attach,false,0,0,NULL)==-ETIMEDOUT); assert(d.managed_failed && !d.managed_lock && !pm_refs); assert(!d.regs[DART_TCR(&d,1)/4] && !d.regs[DART_TCR(&d,2)/4]); for (unsigned sid=3; sid<64; sid++) { diff --git a/tools/testing/selftests/drivers/apple/dart-s2idle-retention.py b/tools/testing/selftests/drivers/apple/dart-s2idle-retention.py index 552e128c600af..3816ee4646e38 100755 --- a/tools/testing/selftests/drivers/apple/dart-s2idle-retention.py +++ b/tools/testing/selftests/drivers/apple/dart-s2idle-retention.py @@ -96,6 +96,7 @@ def macro(source, name): struct io_pgtable_ops *pgtbl_ops; struct apple_dart_dcp *dcp; struct apple_dart_stream_map stream_maps[1]; + bool retire_failed; u64 dma_offset, mask; }; #define to_dart_domain(d) container_of(d, struct apple_dart_domain, domain)