From 95e86af75693544719d0dd72a5c6e013528ce3d0 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 23 Oct 2026 09:46:48 -0400 Subject: [PATCH 1/3] iommu/apple-dart: accept mirrored pages in handed-off firmware slots Since the boot loader marks the T8140 display DARTs with apple,firmware-root-handoff, the J700 internal display stays on simpledrm. disp0_dart fails to probe: iommu apple-dart.412300000.iommu: IOMMU driver was not able to establish FW requested direct mapping. apple-dart 412300000.iommu: probe with driver apple-dart failed with error -16 and the internal DCP waits for it forever. The display subsystem is one domain over two DARTs, disp0 and dispext0. Its framebuffer and region94 reservations come from disp0's firmware tables, which scan out the boot framebuffer. dispext0 does not hold the same translation, so apple_dart_inherited_phys() cannot report the region as mapped for the whole domain, and the core maps it. In handoff mode apple_dart_check_fw_map() refuses any page in a firmware-held slot, including one that repeats the firmware translation exactly, so the direct mapping fails with -EBUSY both before and after attach and the DART probe is rolled back. Without the handoff property the same DARTs take the retained-root mirror path, which accepts such a page when the firmware leaf maps it to the same physical address with the same protection. With that path the same 7.2.10 package bound the internal DCP on this machine before the boot loader started setting the property. Apply the same per-page rule to handed-off slots. Every other page in a firmware slot is still refused, four-level streams still refuse everything, and apple_dart_publish_root() already requires the whole Linux leaf to mirror the firmware leaf before it accepts the slot, so the firmware slot is never written. dispext0 receives the copy in a vacant root slot. In the host fixture, core-second-dart reproduces the two-DART -EBUSY on the old code. The map cases now cover a hole, a wrong physical address, a wrong protection, a changed root and a four-level stream, all still refused. The T8112 SEP DMA host test, which compiles apple_dart_check_fw_map(), stubs the new helper like the other firmware-slot helpers it does not reach. (cherry picked from commit e772e7f87659243ab7b94c2b5bb77ab485dc1128) Signed-off-by: DjDeveloperr --- drivers/iommu/apple-dart.c | 53 +++++++++++++++----- tools/asahi/apple-dart-fw-tests.py | 4 +- tools/asahi/apple-dart-fw/fixture.c | 21 ++++++-- tools/aurora-sep/tests/test_t8112_sep_dma.py | 5 ++ 4 files changed, 66 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 73af536772fc20..a3dfe045faa81a 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -668,6 +668,24 @@ static bool apple_dart_fw_mapping_matches(phys_addr_t phys, int prot, return phys == expected && (prot & mask) == (required & mask); } +/* + * Whether the firmware leaf behind root @slot of a three-level stream already + * translates the page at @addr to @paddr with the protection Linux asks for. + */ +static bool apple_dart_fw_page_matches(const struct apple_dart *dart, + const struct apple_dart_fw_root *fw, + u64 slot, u64 addr, phys_addr_t paddr, + int prot) +{ + u64 leaf = (addr / dart->pgsize) % (dart->pgsize / sizeof(u64)); + phys_addr_t phys; + int fw_prot; + + return apple_dart_fw_leaf_decode(READ_ONCE(fw->leaf[slot][leaf]), addr, + &phys, &fw_prot) && + apple_dart_fw_mapping_matches(phys, fw_prot, paddr, prot); +} + /* * A root slot firmware already uses can still back Linux mappings when the * firmware's leaf table translates every page Linux mapped there to the same @@ -1535,6 +1553,15 @@ static bool apple_dart_domain_uses_fw(struct apple_dart_domain *domain, int i, /* * Hardware continues to use every firmware-owned slot. Its entire IOVA * span is excluded from Linux allocation, including currently unmapped holes. + * + * Linux may still repeat a page the firmware leaf already translates, with + * the same physical address and protection. That happens when one domain + * spans several DARTs and only some of them inherited a reserved region: + * the J700 display subsystem uses disp0 and dispext0, and only disp0 scans + * out the boot framebuffer. apple_dart_inherited_phys() cannot report the + * region as mapped for the whole domain, so the core maps it, and the copy + * must be accepted on disp0 while dispext0 gets it in a vacant slot. + * apple_dart_publish_root() never writes the firmware slot itself. */ static int apple_dart_check_fw_map(struct apple_dart_domain *domain, u64 dva, phys_addr_t paddr, size_t size, int prot) @@ -1556,6 +1583,7 @@ static int apple_dart_check_fw_map(struct apple_dart_domain *domain, u64 dva, u64 addr = dva + off; for (sid = 0; sid < dart->num_streams; sid++) { + const struct apple_dart_fw_root *fw; bool four; u64 idx, slot; const u64 *live, *owned; @@ -1563,12 +1591,6 @@ static int apple_dart_check_fw_map(struct apple_dart_domain *domain, u64 dva, if (!apple_dart_domain_uses_fw(domain, i, map, sid)) continue; if (!dart->fw_handoff) { - const struct apple_dart_fw_root *fw; - phys_addr_t phys; - u64 pte; - int fw_prot; - u64 leaf = (addr >> pgshift) & (entries - 1); - slot = (addr >> (pgshift + bits)) & (entries - 1); idx = addr >> (pgshift + 2 * bits); if (idx >= DART_MAX_TTBR) @@ -1577,10 +1599,8 @@ static int apple_dart_check_fw_map(struct apple_dart_domain *domain, u64 dva, live = dart->locked_ttbr[sid][idx]; if (!live || !apple_dart_fw_slot_owned(fw, live, slot)) continue; - pte = READ_ONCE(fw->leaf[slot][leaf]); - if (!apple_dart_fw_leaf_decode(pte, addr, &phys, &fw_prot) || - !apple_dart_fw_mapping_matches(phys, fw_prot, - paddr + off, prot)) { + if (!apple_dart_fw_page_matches(dart, fw, slot, addr, + paddr + off, prot)) { ret = -EBUSY; goto unlock; } @@ -1595,8 +1615,17 @@ static int apple_dart_check_fw_map(struct apple_dart_domain *domain, u64 dva, continue; live = dart->locked_ttbr[sid][idx]; owned = dart->locked_owned[sid][idx]; - /* All pages of an inherited slot remain firmware's. */ - if (live && apple_dart_fw_slot_foreign(live, owned, slot)) { + if (!live || !apple_dart_fw_slot_foreign(live, owned, slot)) + continue; + /* + * All pages of an inherited slot remain firmware's; + * only a page its snapshotted leaf already maps the + * same way may be repeated. + */ + fw = dart->locked_fw[sid][idx]; + if (four || !apple_dart_fw_slot_owned(fw, live, slot) || + !apple_dart_fw_page_matches(dart, fw, slot, addr, + paddr + off, prot)) { ret = -EBUSY; goto unlock; } diff --git a/tools/asahi/apple-dart-fw-tests.py b/tools/asahi/apple-dart-fw-tests.py index f54dff336a0562..e0996d02f8ec49 100644 --- a/tools/asahi/apple-dart-fw-tests.py +++ b/tools/asahi/apple-dart-fw-tests.py @@ -4,8 +4,8 @@ from pathlib import Path import argparse, hashlib, json, re, subprocess ROOT=Path(__file__).resolve().parents[2] -NAMES=('apple_dart_fw_handoff_enabled','apple_dart_pte_to_paddr','apple_dart_fw_table_ram','apple_dart_fw_table_decode','apple_dart_fw_slot_owned','apple_dart_fw_leaf_decode','apple_dart_fw_mapping_matches','apple_dart_fw_slot_mirrors','apple_dart_publish_root','apple_dart_retire_root','apple_dart_free_fw_root','apple_dart_snapshot_fw_root','apple_dart_fw_slot_foreign','apple_dart_domain_uses_fw','apple_dart_check_fw_map','apple_dart_inherited_phys','apple_dart_fw_slot_range','apple_dart_get_fw_resv_regions','apple_dart_fw_invalidate') -CASES=("legacy-map-match","legacy-map-mismatch","legacy-lookup","legacy-snapshot",'mirror','changed-leaf','wrong-permission','wrong-physical','malformed-leaf','vacant','changed-owner','retire','reserved-ram','unreserved','mmio','misalignment','address-width','root-encoding','leaf-as-table','snapshot','snapshot-malformed','snapshot-unreserved','snapshot-root-change','snapshot-map-failure','snapshot-allocation-failure','range-three','range-offset','range-width','range-overflow','range-underflow','range-four','range-four-last','map-firmware','map-hole','map-host','map-owned','map-unmatched','map-attached','lookup-inherited','lookup-changed-leaf','lookup-changed-root','lookup-unmatched','reserve-firmware','reserve-teardown','reserve-unmatched','reserve-owned','invalidate-three','invalidate-four','invalidate-changed-root','invalidate-error','core-inherited','core-hole','core-changed-leaf','snapshot-unmatched','snapshot-four','range-four-width','range-three-last','handoff-matched','handoff-missing','handoff-duplicate','handoff-zero','handoff-unlocked','handoff-other-soc') +NAMES=('apple_dart_fw_handoff_enabled','apple_dart_pte_to_paddr','apple_dart_fw_table_ram','apple_dart_fw_table_decode','apple_dart_fw_slot_owned','apple_dart_fw_leaf_decode','apple_dart_fw_mapping_matches','apple_dart_fw_page_matches','apple_dart_fw_slot_mirrors','apple_dart_publish_root','apple_dart_retire_root','apple_dart_free_fw_root','apple_dart_snapshot_fw_root','apple_dart_fw_slot_foreign','apple_dart_domain_uses_fw','apple_dart_check_fw_map','apple_dart_inherited_phys','apple_dart_fw_slot_range','apple_dart_get_fw_resv_regions','apple_dart_fw_invalidate') +CASES=("legacy-map-match","legacy-map-mismatch","legacy-lookup","legacy-snapshot",'mirror','changed-leaf','wrong-permission','wrong-physical','malformed-leaf','vacant','changed-owner','retire','reserved-ram','unreserved','mmio','misalignment','address-width','root-encoding','leaf-as-table','snapshot','snapshot-malformed','snapshot-unreserved','snapshot-root-change','snapshot-map-failure','snapshot-allocation-failure','range-three','range-offset','range-width','range-overflow','range-underflow','range-four','range-four-last','map-firmware','map-hole','map-host','map-owned','map-unmatched','map-attached','map-wrong-physical','map-wrong-permission','map-changed-root','map-four','lookup-inherited','lookup-changed-leaf','lookup-changed-root','lookup-unmatched','reserve-firmware','reserve-teardown','reserve-unmatched','reserve-owned','invalidate-three','invalidate-four','invalidate-changed-root','invalidate-error','core-inherited','core-hole','core-changed-leaf','core-second-dart','snapshot-unmatched','snapshot-four','range-four-width','range-three-last','handoff-matched','handoff-missing','handoff-duplicate','handoff-zero','handoff-unlocked','handoff-other-soc') def function(s,name): match=re.search(r'^static[^;{}]*\b'+re.escape(name)+r'\([^;{}]*\)\s*\{',s,re.M) if not match:return None diff --git a/tools/asahi/apple-dart-fw/fixture.c b/tools/asahi/apple-dart-fw/fixture.c index cd3ade12ae769d..41866dc1222aa1 100644 --- a/tools/asahi/apple-dart-fw/fixture.c +++ b/tools/asahi/apple-dart-fw/fixture.c @@ -140,9 +140,16 @@ int main(int argc,char**argv){ regions[1]=(struct iommu_resv_region){.start=0x8000,.dva=0,.length=SZ_16K,.type=IOMMU_RESV_TRANSLATED};region_count=2; if(!strcmp(c,"core-hole"))firmware_leaf[0]=0; if(!strcmp(c,"core-changed-leaf"))firmware_leaf[0]=leaf(0xc000); + /* A second DART of the domain whose root slot is vacant cannot inherit + * the region, so the core maps it and the first DART accepts the copy. */ + static u64 vacant_root[2048],vacant_shadow[2048];struct apple_dart d2=d; + if(!strcmp(c,"core-second-dart")){ + d2.locked_ttbr[0][0]=vacant_root;d2.locked_owned[0][0]=vacant_shadow;d2.locked_fw[0][0]=NULL; + domain.stream_maps[1].dart=&d2;domain.fw_sidmap[1][0]=1;regions[1].prot=IOMMU_READ|IOMMU_WRITE|IOMMU_CACHE; + } int result=iommu_create_device_fw_mappings(&io_domain,&dev); assert(io.require_translated&&result==(!strcmp(c,"core-hole")?-EBUSY:0)); - assert(core_maps==(!strcmp(c,"core-hole")?1:0)); + assert(core_maps==(!strcmp(c,"core-hole")||!strcmp(c,"core-second-dart")?1:0)); } else if(!strncmp(c,"range-",6)){ u64 start=0,length=0;bool four=!strncmp(c,"range-four",10);u64 window=1ULL<<40;size_t slot=four?16:1; @@ -161,12 +168,20 @@ int main(int argc,char**argv){ } else if(!strncmp(c,"map-",4)){ root[0]=0x1401;u64 addr=0; + u64 paddr=0x8000;int prot=IOMMU_READ|IOMMU_WRITE|IOMMU_CACHE; if(!strcmp(c,"map-host")){addr=1ULL<<25;root[1]=0;} if(!strcmp(c,"map-owned"))shadow[0]=root[0]; if(!strcmp(c,"map-unmatched"))d.fw_handoff=0; if(!strcmp(c,"map-attached")){domain.fw_sidmap[0][0]=0;domain.stream_maps[0].sidmap[0]=1;} - int expected=!strcmp(c,"map-host")||!strcmp(c,"map-owned")||!strcmp(c,"map-unmatched")?0:-EBUSY; - assert(apple_dart_check_fw_map(&domain,addr,0x8000,SZ_16K,IOMMU_READ|IOMMU_WRITE|IOMMU_CACHE)==expected); + /* Only a page the firmware leaf already maps the same way is repeated. */ + if(!strcmp(c,"map-hole"))firmware_leaf[0]=0; + if(!strcmp(c,"map-wrong-physical"))paddr=0xc000; + if(!strcmp(c,"map-wrong-permission"))prot&=~IOMMU_CACHE; + if(!strcmp(c,"map-changed-root"))root[0]=0x1801; + if(!strcmp(c,"map-four"))d.tcr=1; + int expected=!strcmp(c,"map-host")||!strcmp(c,"map-owned")||!strcmp(c,"map-unmatched")|| + !strcmp(c,"map-firmware")||!strcmp(c,"map-attached")?0:-EBUSY; + assert(apple_dart_check_fw_map(&domain,addr,paddr,SZ_16K,prot)==expected); } else if(!strncmp(c,"lookup-",7)){ root[0]=0x1401;assert(apple_dart_inherited_phys(&domain,0)==0x8000); diff --git a/tools/aurora-sep/tests/test_t8112_sep_dma.py b/tools/aurora-sep/tests/test_t8112_sep_dma.py index 45fdae561ead39..c2a3f8a80ee0f5 100644 --- a/tools/aurora-sep/tests/test_t8112_sep_dma.py +++ b/tools/aurora-sep/tests/test_t8112_sep_dma.py @@ -130,6 +130,11 @@ def setUpClass(cls): static bool apple_dart_fw_mapping_matches(phys_addr_t phys, int prot, phys_addr_t expected, int required) { abort(); } +static bool apple_dart_fw_page_matches(const struct apple_dart *dart, + const struct apple_dart_fw_root *fw, + u64 slot, u64 addr, phys_addr_t paddr, + int prot) +{ abort(); } static bool apple_dart_fw_handoff_page_busy(struct apple_dart *dart, struct apple_dart_domain *domain, int i, struct apple_dart_atomic_stream_map *map, From e2fecd940c5b68ab4e262c410ad9561064399f95 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 23 Oct 2026 09:47:05 -0400 Subject: [PATCH 2/3] iommu/apple-dart: keep stream invalidates out of handed-off slots A whole-stream invalidate on a T8140 DART can break DMA that still runs through firmware-held root slots, such as the boot framebuffer scanout of the internal display, as apple_dart_fw_invalidate() describes. Range flushes already route both the retained-root mirror and the reserved-table handoff through apple_dart_fw_invalidate(), but the stream invalidate used at attach, at release and on resume did so only for the mirror path. A handed-off DART, which the boot loader now selects for the J700 display DARTs, got a whole-stream flush there. Treat handed-off DARTs like mirrored ones: invalidate only the slots Linux published, by DVA range, and do it while retiring a root, before the root is unmapped. This is what these DARTs did before the boot loader started handing their roots off. (cherry picked from commit ff5c39ae1782cd1d826eddf48e0b08b1b2af61eb) Signed-off-by: DjDeveloperr --- drivers/iommu/apple-dart.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index a3dfe045faa81a..e519ec8225cd57 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -945,7 +945,8 @@ apple_dart_hw_unmap_locked_ttbr(struct apple_dart_stream_map *stream_map, u8 idx if (!live) continue; apple_dart_retire_root(live, owned, dart->pgsize / sizeof(*live)); - if (dart->fw_mirror && dart->version >= 0x0202 && !idx) { + if ((dart->fw_mirror || dart->fw_handoff) && + dart->version >= 0x0202 && !idx) { struct apple_dart_stream_map stream = { .dart = dart }; __set_bit(sid, stream.sidmap); @@ -1258,7 +1259,9 @@ apple_dart_t8020_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map) static int apple_dart_t8110_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map) { - if (stream_map->dart->fw_mirror && stream_map->dart->version >= 0x0202) + /* Keep out of firmware-held slots; see apple_dart_fw_invalidate(). */ + if ((stream_map->dart->fw_mirror || stream_map->dart->fw_handoff) && + stream_map->dart->version >= 0x0202) return apple_dart_fw_invalidate(stream_map, 0, U64_MAX); return apple_dart_t8110_hw_tlb_command_range( stream_map, DART_T8110_TLB_CMD_OP_FLUSH_SID, false, 0, 0); From 07d64c6b829aba03ac782fba9de91066940cf8f7 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 28 Oct 2026 08:33:01 -0400 Subject: [PATCH 3/3] iommu/apple-dart: keep page tables after a failed invalidate at root retire On DARTs with a firmware root mirror or handoff, retiring a locked root runs a range invalidate of the slots Linux published. apple_dart_hw_unmap_locked_ttbr() dropped its result, returned 0 and cleared the root. The invalidate that apple_dart_domain_free() checks afterwards then found no root, issued no command and returned 0, so the domain's page tables were freed even after a TLB timeout, while the hardware could still cache translations through them. Before the handoff DARTs took that path, a whole-stream flush there returned the error and the page tables were kept. Return the first retire-time invalidate error, and remember it per stream on the DART. apple_dart_domain_free() keeps the page tables when its own retire or invalidate fails, and also when a consumer release or an attach rollback retired the root first and its invalidate failed. The remembered error is never cleared, because only the hardware could show that the TLB no longer holds those entries; later domains on that stream keep their page tables too. The host fixture now builds apple_dart_hw_unmap_locked_ttbr(), apple_dart_release_device() and apple_dart_domain_free(), and forces the TLB command to time out for a direct free, a release before the free and a rollback before the free. Each keeps the page tables, and the timeout cases fail on the old code. A comment in apple_dart_check_fw_map() now says the firmware leaf is checked, because only the root entry is snapshotted. The previous commit's message also lists resume as a user of the stream invalidate, but apple_dart_resume() returns early for locked DARTs, and handoff requires a locked DART, so resume never reaches it. Signed-off-by: DjDeveloperr --- drivers/iommu/apple-dart.c | 63 ++++++++++++++++++++++++----- tools/asahi/apple-dart-fw-tests.py | 4 +- tools/asahi/apple-dart-fw/fixture.c | 44 ++++++++++++++++++-- 3 files changed, 95 insertions(+), 16 deletions(-) diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index e519ec8225cd57..4a180821442329 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -326,6 +326,14 @@ struct apple_dart { * rather than the DART's. */ u64 locked_window[DART_MAX_STREAMS]; + /* + * First error of a TLB invalidate that failed while retiring a locked + * root, per stream. The hardware may still cache translations through + * the retired page tables, so apple_dart_domain_free() keeps the page + * tables of every domain on such a stream. It is never cleared: only + * the hardware could show that the TLB no longer holds those entries. + */ + int locked_retire_err[DART_MAX_STREAMS]; }; /* @@ -930,17 +938,24 @@ apple_dart_hw_map_locked_ttbr(struct apple_dart_stream_map *stream_map, u8 idx) return 0; } +/* + * Retire the locked roots @idx of @stream_map's streams. Returns the first + * error of the TLB invalidate that retiring needs, which is also remembered + * per stream for apple_dart_domain_free(): a consumer release or an attach + * rollback can retire the root before the domain is freed. + */ static int apple_dart_hw_unmap_locked_ttbr(struct apple_dart_stream_map *stream_map, u8 idx) { struct apple_dart *dart = stream_map->dart; - int sid; + int sid, ret = 0; for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) { u64 *live = dart->locked_ttbr[sid][idx]; u64 *owned = dart->locked_owned[sid][idx]; struct apple_dart_fw_root *fw; unsigned long flags; + int err = 0; if (!live) continue; @@ -950,9 +965,12 @@ apple_dart_hw_unmap_locked_ttbr(struct apple_dart_stream_map *stream_map, u8 idx struct apple_dart_stream_map stream = { .dart = dart }; __set_bit(sid, stream.sidmap); - (void)apple_dart_fw_invalidate(&stream, 0, U64_MAX); + err = apple_dart_fw_invalidate(&stream, 0, U64_MAX); + ret = ret ?: err; } spin_lock_irqsave(&dart->lock, flags); + if (err && !dart->locked_retire_err[sid]) + dart->locked_retire_err[sid] = err; fw = dart->locked_fw[sid][idx]; dart->locked_fw[sid][idx] = NULL; dart->locked_ttbr[sid][idx] = NULL; @@ -962,7 +980,24 @@ apple_dart_hw_unmap_locked_ttbr(struct apple_dart_stream_map *stream_map, u8 idx devm_kfree(dart->dev, owned); dart->locked_owned[sid][idx] = NULL; } - return 0; + return ret; +} + +/* The remembered retire-time invalidate error of @stream_map's streams, or 0. */ +static int apple_dart_retire_error(struct apple_dart_stream_map *stream_map) +{ + struct apple_dart *dart = stream_map->dart; + unsigned long flags; + int sid, ret = 0; + + spin_lock_irqsave(&dart->lock, flags); + for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) { + ret = dart->locked_retire_err[sid]; + if (ret) + break; + } + spin_unlock_irqrestore(&dart->lock, flags); + return ret; } static int @@ -1622,7 +1657,7 @@ static int apple_dart_check_fw_map(struct apple_dart_domain *domain, u64 dva, continue; /* * All pages of an inherited slot remain firmware's; - * only a page its snapshotted leaf already maps the + * only a page its firmware leaf already maps the * same way may be repeated. */ fw = dart->locked_fw[sid][idx]; @@ -2163,7 +2198,7 @@ static void apple_dart_domain_free(struct iommu_domain *domain) struct apple_dart_domain *dart_domain = to_dart_domain(domain); struct apple_dart_atomic_stream_map *map; struct apple_dart_stream_map stream; - int i, j, idx; + int i, j, idx, ret; for_each_stream_map(i, dart_domain, map) { if (!map->dart->locked) @@ -2179,14 +2214,22 @@ static void apple_dart_domain_free(struct iommu_domain *domain) "retaining page tables after power failure: %d\n", j); return; } - for (idx = 0; idx < stream.dart->hw->ttbr_count; idx++) - apple_dart_hw_unmap_locked_ttbr(&stream, idx); + ret = 0; + for (idx = 0; idx < stream.dart->hw->ttbr_count; idx++) { + j = apple_dart_hw_unmap_locked_ttbr(&stream, idx); + ret = ret ?: j; + } j = stream.dart->hw->invalidate_tlb(&stream); + ret = ret ?: j; pm_runtime_put(stream.dart->dev); - /* Do not free page tables that hardware may still cache. */ - if (j) { + /* + * Do not free page tables that hardware may still cache, also + * when a consumer release or an attach rollback retired the root. + */ + ret = ret ?: apple_dart_retire_error(&stream); + if (ret) { dev_err_ratelimited(stream.dart->dev, - "retaining page tables after TLB invalidation failure: %d\n", j); + "retaining page tables after TLB invalidation failure: %d\n", ret); return; } } diff --git a/tools/asahi/apple-dart-fw-tests.py b/tools/asahi/apple-dart-fw-tests.py index e0996d02f8ec49..99e54a7dd33d4b 100644 --- a/tools/asahi/apple-dart-fw-tests.py +++ b/tools/asahi/apple-dart-fw-tests.py @@ -4,8 +4,8 @@ from pathlib import Path import argparse, hashlib, json, re, subprocess ROOT=Path(__file__).resolve().parents[2] -NAMES=('apple_dart_fw_handoff_enabled','apple_dart_pte_to_paddr','apple_dart_fw_table_ram','apple_dart_fw_table_decode','apple_dart_fw_slot_owned','apple_dart_fw_leaf_decode','apple_dart_fw_mapping_matches','apple_dart_fw_page_matches','apple_dart_fw_slot_mirrors','apple_dart_publish_root','apple_dart_retire_root','apple_dart_free_fw_root','apple_dart_snapshot_fw_root','apple_dart_fw_slot_foreign','apple_dart_domain_uses_fw','apple_dart_check_fw_map','apple_dart_inherited_phys','apple_dart_fw_slot_range','apple_dart_get_fw_resv_regions','apple_dart_fw_invalidate') -CASES=("legacy-map-match","legacy-map-mismatch","legacy-lookup","legacy-snapshot",'mirror','changed-leaf','wrong-permission','wrong-physical','malformed-leaf','vacant','changed-owner','retire','reserved-ram','unreserved','mmio','misalignment','address-width','root-encoding','leaf-as-table','snapshot','snapshot-malformed','snapshot-unreserved','snapshot-root-change','snapshot-map-failure','snapshot-allocation-failure','range-three','range-offset','range-width','range-overflow','range-underflow','range-four','range-four-last','map-firmware','map-hole','map-host','map-owned','map-unmatched','map-attached','map-wrong-physical','map-wrong-permission','map-changed-root','map-four','lookup-inherited','lookup-changed-leaf','lookup-changed-root','lookup-unmatched','reserve-firmware','reserve-teardown','reserve-unmatched','reserve-owned','invalidate-three','invalidate-four','invalidate-changed-root','invalidate-error','core-inherited','core-hole','core-changed-leaf','core-second-dart','snapshot-unmatched','snapshot-four','range-four-width','range-three-last','handoff-matched','handoff-missing','handoff-duplicate','handoff-zero','handoff-unlocked','handoff-other-soc') +NAMES=('apple_dart_fw_handoff_enabled','apple_dart_pte_to_paddr','apple_dart_fw_table_ram','apple_dart_fw_table_decode','apple_dart_fw_slot_owned','apple_dart_fw_leaf_decode','apple_dart_fw_mapping_matches','apple_dart_fw_page_matches','apple_dart_fw_slot_mirrors','apple_dart_publish_root','apple_dart_retire_root','apple_dart_free_fw_root','apple_dart_snapshot_fw_root','apple_dart_fw_slot_foreign','apple_dart_domain_uses_fw','apple_dart_check_fw_map','apple_dart_inherited_phys','apple_dart_fw_slot_range','apple_dart_get_fw_resv_regions','apple_dart_fw_invalidate','apple_dart_hw_unmap_locked_ttbr','apple_dart_retire_error','apple_dart_t8110_hw_invalidate_tlb','apple_dart_release_device','apple_dart_domain_free') +CASES=("legacy-map-match","legacy-map-mismatch","legacy-lookup","legacy-snapshot",'mirror','changed-leaf','wrong-permission','wrong-physical','malformed-leaf','vacant','changed-owner','retire','reserved-ram','unreserved','mmio','misalignment','address-width','root-encoding','leaf-as-table','snapshot','snapshot-malformed','snapshot-unreserved','snapshot-root-change','snapshot-map-failure','snapshot-allocation-failure','range-three','range-offset','range-width','range-overflow','range-underflow','range-four','range-four-last','map-firmware','map-hole','map-host','map-owned','map-unmatched','map-attached','map-wrong-physical','map-wrong-permission','map-changed-root','map-four','lookup-inherited','lookup-changed-leaf','lookup-changed-root','lookup-unmatched','reserve-firmware','reserve-teardown','reserve-unmatched','reserve-owned','invalidate-three','invalidate-four','invalidate-changed-root','invalidate-error','core-inherited','core-hole','core-changed-leaf','core-second-dart','snapshot-unmatched','snapshot-four','range-four-width','range-three-last','handoff-matched','handoff-missing','handoff-duplicate','handoff-zero','handoff-unlocked','handoff-other-soc','retire-free','retire-free-timeout','retire-release','retire-release-timeout','retire-rollback-timeout') def function(s,name): match=re.search(r'^static[^;{}]*\b'+re.escape(name)+r'\([^;{}]*\)\s*\{',s,re.M) if not match:return None diff --git a/tools/asahi/apple-dart-fw/fixture.c b/tools/asahi/apple-dart-fw/fixture.c index 41866dc1222aa1..65727266dc2ccd 100644 --- a/tools/asahi/apple-dart-fw/fixture.c +++ b/tools/asahi/apple-dart-fw/fixture.c @@ -6,7 +6,7 @@ #include #include #include -typedef uint32_t u32;typedef uint64_t u64;typedef uint64_t phys_addr_t;typedef uint64_t dma_addr_t; +typedef uint8_t u8;typedef uint32_t u32;typedef uint64_t u64;typedef uint64_t phys_addr_t;typedef uint64_t dma_addr_t; #define BIT_ULL(n) (1ULL<<(n)) #define GENMASK_ULL(h,l) ((~0ULL>>(63-(h)))&(~0ULL<<(l))) #define SZ_16K 16384 @@ -28,7 +28,8 @@ typedef uint32_t u32;typedef uint64_t u64;typedef uint64_t phys_addr_t;typedef u #define ERR_PTR(x) ((void*)(intptr_t)(x)) #define IS_ERR(x) ((uintptr_t)(x)>=(uintptr_t)-4095) #define PTR_ERR(x) ((intptr_t)(x)) -struct apple_dart_hw {int fmt;unsigned tcr_4level,ttbr_count;}; +struct apple_dart_stream_map;struct iommu_domain; +struct apple_dart_hw {int fmt;unsigned tcr_4level,ttbr_count;int(*invalidate_tlb)(struct apple_dart_stream_map*);}; #define DART_MAX_TTBR 4 #define DART_MAX_ROOT_ENTRIES 2048 #define U64_MAX UINT64_MAX @@ -49,10 +50,10 @@ static unsigned lock_depth; static size_t find_next_bit(unsigned long*p,size_t n,size_t a){for(;astream_maps[i])->dart;(i)++) #define DART_T8110_TLB_CMD_OP_FLUSH_SID 1 @@ -87,6 +88,16 @@ static void memunmap(void*p){assert(p==firmware_leaf);unmap_calls++;} static void*phys_to_virt(u64 phys){assert(phys==0x4000);return ours_leaf;} static u64 compare(u64*p,u64 old,u64 next){u64 before=*p;if(before==old)*p=next;return before;} #define cmpxchg(p,o,n) compare(p,o,n) +#define BITS_TO_LONGS(n) (((n)+63)/64) +#define bitmap_empty(p,n) (!((p)[0]&((n)>=64?~0UL:(1UL<<(n))-1))) +#define devm_memunmap(d,p) ((void)(p)) +#define devm_kfree(d,p) ((void)(p)) +#define to_dart_domain(d) ((struct apple_dart_domain*)(void*)(d)) +static int pm_gets,pm_puts,errors_logged,pgtable_frees; +static int pm_runtime_resume_and_get(struct device*d){pm_gets++;return 0;} +static void pm_runtime_put(struct device*d){pm_puts++;} +#define dev_err_ratelimited(d,...) (errors_logged++,fprintf(stderr,__VA_ARGS__)) +static void free_io_pgtable_ops(void*ops){assert(ops);pgtable_frees++;} /* PRODUCTION */ #define IOMMU_RESV_DIRECT 1 #define IOMMU_RESV_DIRECT_RELAXABLE 2 @@ -210,6 +221,31 @@ int main(int argc,char**argv){ assert(apple_dart_fw_invalidate(&stream,base,base+2*span-1)==(command_error?-ETIMEDOUT:0)); assert(commands==1&&command_start[0]==base+span&&command_end[0]==base+2*span-1); } + else if(!strncmp(c,"retire-",7)){ + /* A TLB timeout while retiring a root keeps the domain's page tables, + * whether the domain free, a consumer release or an attach rollback + * retired it first. */ + root[0]=0x1401;root[1]=shadow[1]=0x801;d.locked_window[0]=1ULL<<40;d.locked_fw[0][0]=NULL;d.version=0x0202;hw.invalidate_tlb=apple_dart_t8110_hw_invalidate_tlb; + struct apple_dart_domain*dom=calloc(1,sizeof(*dom));assert(dom);dom->stream_maps[0].dart=&d;dom->stream_maps[0].sidmap[0]=1;dom->pgtbl_ops=&d; + bool timeout=!!strstr(c,"timeout");if(timeout)command_error=-ETIMEDOUT; + if(!strncmp(c,"retire-release",14)){ + struct apple_dart_master_cfg*owner=calloc(1,sizeof(*owner));assert(owner);*owner=cfg;dev.cfg=owner; + apple_dart_release_device(&dev);assert(pm_gets==1&&pm_puts==1); + }else if(!strcmp(c,"retire-rollback-timeout")){ + struct apple_dart_stream_map stream={.dart=&d,.sidmap={1}}; + assert(apple_dart_hw_unmap_locked_ttbr(&stream,0)==-ETIMEDOUT); + }else assert(!strcmp(c,"retire-free")||!strcmp(c,"retire-free-timeout")); + if(strncmp(c,"retire-free",11)){ + /* Retired first: the free sees no root left and issues no command. */ + assert(!d.locked_ttbr[0][0]&&commands==1&&d.locked_retire_err[0]==(timeout?-ETIMEDOUT:0)); + command_error=0; + } + apple_dart_domain_free((struct iommu_domain*)dom); + assert(!d.locked_ttbr[0][0]&&root[0]==0x1401&&!root[1]&&!shadow[1]); + assert(commands==1&&command_start[0]==(1ULL<<40)+(1ULL<<25)&&command_end[0]==(1ULL<<40)+(1ULL<<36)-1); + assert(d.locked_retire_err[0]==(timeout?-ETIMEDOUT:0)); + assert(pgtable_frees==!timeout&&errors_logged==timeout); + } else if(!strcmp(c,"mirror")){assert(!apple_dart_publish_root(&d,live,owned,ours,&fw,2));assert(live[0]==0x1401&&owned[0]==0);} else if(!strcmp(c,"changed-leaf")){assert(!apple_dart_publish_root(&d,live,owned,ours,&fw,2));firmware_leaf[0]=leaf(0xc000);assert(apple_dart_publish_root(&d,live,owned,ours,&fw,2)==-EBUSY);} else if(!strcmp(c,"wrong-permission")){firmware_leaf[0]|=4;assert(apple_dart_publish_root(&d,live,owned,ours,&fw,2)==-EBUSY);}