Skip to content
Merged
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
142 changes: 142 additions & 0 deletions drivers/iommu/apple-dart-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,146 @@ static void apple_dart_test_fw_mirror_prot(struct kunit *test)
pm_runtime_disable(dart->dev);
}

/* A locked four-level stream with one firmware page, as a T6050 boot loader hands it over. */
struct apple_dart_test_four {
struct apple_dart_master_cfg master;
struct apple_dart *dart;
u64 *root;
};

static const u64 apple_dart_test_four_dva = 0x10000000000ULL + SZ_16K;
static const phys_addr_t apple_dart_test_four_pa = 0x80200000ULL;

static void apple_dart_test_four_setup(struct kunit *test, struct apple_dart_test_four *four)
{
const u64 fields = GENMASK_ULL(51, 40) | BIT_ULL(0);
struct apple_dart *dart;
u64 *mid, *leaf;
void *regs;

dart = kunit_kzalloc(test, sizeof(*dart), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, dart);
dart->dev = kunit_device_register(test, "apple-dart-four-level-test");
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dart->dev);
regs = vzalloc(APPLE_DART_TEST_REGS_SIZE);
KUNIT_ASSERT_NOT_NULL(test, regs);
KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, apple_dart_test_vfree, regs), 0);
dart->regs = (__force void __iomem *)regs;
dart->hw = &apple_dart_hw_t8110;
dart->version = 0x0204;
dart->pgsize = SZ_16K;
dart->ias = 42;
dart->oas = 42;
dart->num_streams = 32;
dart->locked = true;
dart->fw_mirror = true;
dart->fw_tree = true;
spin_lock_init(&dart->lock);

/* Root entry 16 (1 TiB) -> table entry 0 -> page 1: the firmware's buffer. */
four->root = apple_dart_test_reserved_table(test);
mid = apple_dart_test_reserved_table(test);
leaf = apple_dart_test_reserved_table(test);
four->root[16] = (virt_to_phys(mid) >> 4) | BIT_ULL(0);
mid[0] = (virt_to_phys(leaf) >> 4) | BIT_ULL(0);
leaf[1] = fields | (apple_dart_test_four_pa >> 4);
writel(((virt_to_phys(four->root) >> dart->hw->ttbr_shift) <<
dart->hw->ttbr_addr_field_shift) | dart->hw->ttbr_valid,
dart->regs + DART_TTBR(dart, 23, 0));
writel(dart->hw->tcr_4level, dart->regs + DART_TCR(dart, 23));

four->dart = dart;
four->master.stream_maps[0].dart = dart;
__set_bit(23, four->master.stream_maps[0].sidmap);
/* The display DCPs' window: 3 TiB at 1 TiB. */
four->master.dma_min = 0x10000000000ULL;
four->master.dma_max = 0x3ffffffffffULL;
}

static void apple_dart_test_four_teardown(struct apple_dart_test_four *four,
struct apple_dart_domain *domain)
{
apple_dart_free_fw_tree(four->dart, four->dart->locked_fw_tree[23]);
four->dart->locked_fw_tree[23] = NULL;
free_io_pgtable_ops(domain->pgtbl_ops);
}

/*
* A running co-processor handed over on a locked four-level stream (a T6050
* external DCP with its live RTKit session): the boot loader re-exports the
* firmware's buffers as iommu-addresses inside the root slot firmware holds.
* The domain imports that root when it is finalized, so the firmware's page
* already resolves and the core does not copy it into Linux's table, and the
* attach then publishes nothing into the firmware slot. A copy there is what
* the attach refuses with -EBUSY.
*/
static void apple_dart_test_four_level_live_import(struct kunit *test)
{
struct apple_dart_test_four four = {};
struct apple_dart_domain *domain;
struct io_pgtable_cfg *cfg;
struct apple_dart_stream_map stream = {};
size_t mapped = 0;
u64 slot;
int ret;

if (PAGE_SIZE < SZ_16K)
kunit_skip(test, "needs kernel pages of at least 16 KiB");
apple_dart_test_four_setup(test, &four);
slot = four.root[16];
domain = kunit_kzalloc(test, sizeof(*domain), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, domain);
mutex_init(&domain->init_lock);

KUNIT_ASSERT_EQ(test, apple_dart_finalize_domain(domain, &four.master), 0);
cfg = &io_pgtable_ops_to_pgtable(domain->pgtbl_ops)->cfg;
KUNIT_EXPECT_EQ(test, cfg->apple_dart_cfg.n_levels, 4U);
KUNIT_EXPECT_NOT_NULL(test, four.dart->locked_ttbr[23][0]);
KUNIT_EXPECT_NOT_NULL(test, four.dart->locked_fw_tree[23]);
KUNIT_EXPECT_TRUE(test, test_bit(23, domain->fw_sidmap[0]));
/* What iommu_create_device_fw_mappings() asks before it maps a region. */
KUNIT_EXPECT_EQ(test, apple_dart_iova_to_phys(&domain->domain, apple_dart_test_four_dva),
apple_dart_test_four_pa);

/* The attach publishes nothing into the slot firmware holds. */
stream.dart = four.dart;
__set_bit(23, stream.sidmap);
KUNIT_EXPECT_EQ(test, apple_dart_hw_sync_locked(cfg, &stream, false), 0);
KUNIT_EXPECT_EQ(test, four.root[16], slot);

/* A Linux copy of the same page in that slot is refused, the slot kept. */
ret = domain->pgtbl_ops->map_pages(domain->pgtbl_ops, apple_dart_test_four_dva,
apple_dart_test_four_pa, SZ_16K, 1,
IOMMU_READ | IOMMU_WRITE, GFP_KERNEL, &mapped);
KUNIT_ASSERT_EQ(test, ret, 0);
KUNIT_EXPECT_EQ(test, apple_dart_hw_sync_locked(cfg, &stream, false), -EBUSY);
KUNIT_EXPECT_EQ(test, four.root[16], slot);

apple_dart_test_four_teardown(&four, domain);
}

/* A four-level stream whose firmware subtrees are not recorded keeps its root until attach. */
static void apple_dart_test_four_level_no_tree(struct kunit *test)
{
struct apple_dart_test_four four = {};
struct apple_dart_domain *domain;

if (PAGE_SIZE < SZ_16K)
kunit_skip(test, "needs kernel pages of at least 16 KiB");
apple_dart_test_four_setup(test, &four);
four.dart->fw_tree = false;
domain = kunit_kzalloc(test, sizeof(*domain), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, domain);
mutex_init(&domain->init_lock);

KUNIT_ASSERT_EQ(test, apple_dart_finalize_domain(domain, &four.master), 0);
KUNIT_EXPECT_NULL(test, four.dart->locked_ttbr[23][0]);
KUNIT_EXPECT_FALSE(test, test_bit(23, domain->fw_sidmap[0]));
KUNIT_EXPECT_EQ(test, apple_dart_iova_to_phys(&domain->domain, apple_dart_test_four_dva),
(phys_addr_t)0);
apple_dart_test_four_teardown(&four, domain);
}

static struct apple_dart *apple_dart_test_keep_dart(struct kunit *test, const char *name)
{
struct apple_dart *dart;
Expand Down Expand Up @@ -1815,6 +1955,8 @@ static struct kunit_case apple_dart_test_cases[] = {
KUNIT_CASE(apple_dart_test_firmware_roots),
KUNIT_CASE(apple_dart_test_t8132_fw_mirror_selection),
KUNIT_CASE(apple_dart_test_fw_mirror_prot),
KUNIT_CASE(apple_dart_test_four_level_live_import),
KUNIT_CASE(apple_dart_test_four_level_no_tree),
KUNIT_CASE(apple_dart_test_display_keep),
KUNIT_CASE(apple_dart_test_unmap_flush_after_publish_failure),
KUNIT_CASE(apple_dart_test_gated_commands),
Expand Down
21 changes: 18 additions & 3 deletions drivers/iommu/apple-dart.c
Original file line number Diff line number Diff line change
Expand Up @@ -3827,14 +3827,29 @@ static int __apple_dart_finalize_domain(struct apple_dart_domain *dart_domain,
dart_domain->domain.geometry.force_aperture = true;
dart_domain->dma_offset = dart->dma_offset;

if (pgtbl_cfg.apple_dart_cfg.n_levels == 3) {
if (pgtbl_cfg.apple_dart_cfg.n_levels == 3 ||
pgtbl_cfg.apple_dart_cfg.n_levels == 4) {
bool four = pgtbl_cfg.apple_dart_cfg.n_levels == 4;
struct apple_dart_stream_map *stream;
int index;

/* Make inherited translations visible before core reservation setup. */
/*
* Make inherited translations visible before core reservation setup.
*
* A four-level table here means a locked four-level stream (the
* lock check above kept its walk depth). Its root is imported
* only where firmware's subtrees are recorded (fw_tree): then
* iova_to_phys resolves the firmware's own buffers, which a boot
* loader re-exports as iommu-addresses when it hands over a
* running co-processor, so the core does not copy them into the
* root slot firmware holds, and apple_dart_get_fw_resv_regions()
* keeps that slot out of IOVA allocation. Without the import the
* core's copy lands in that slot and the attach refuses it.
*/
for_each_stream_map(i, cfg, stream) {
if (!stream->dart->locked ||
(!stream->dart->fw_handoff && !stream->dart->fw_mirror))
(!stream->dart->fw_handoff && !stream->dart->fw_mirror) ||
(four && !stream->dart->fw_tree))
continue;
apple_dart_set_locked_window(dart_domain, stream);
for (index = 0; index < pgtbl_cfg.apple_dart_cfg.n_ttbrs; index++) {
Expand Down
Loading