From 174fed543a8f869f86f48cff21edd16406ad2825 Mon Sep 17 00:00:00 2001 From: vlad Date: Thu, 3 Dec 2026 23:49:43 +0200 Subject: [PATCH 1/2] drm/asahi: mem: do not use ASID-only TLBI for global upper mappings Kernel mappings live in the upper half of the GPU VA space, in the one table that is not owned by a user VM, and its leaves are written global (nG clear). tlbi aside1os only removes non-global entries, so it leaves cached global translations of an unmapped kernel range behind. tlbi_range_or_asid() falls back to an ASID flush for ranges of 32 MiB or more, and tlbi_pages(), used in page TLBI mode, does the same above 2048 pages. A kernel mapping of that size that is unmapped through either path keeps stale translations for its old pages. For an upper-half range, never take the ASID fallback. In range mode, issue as many tlbi rvae1os operations as the range needs: a by-VA operation matches global entries whatever the ASID, and even the whole 64 GiB kernel window takes two operations. In page mode, which avoids range operations, use tlbi vmalle1os. Lower-half ranges, whose leaves are all non-global, keep the ASID fallback. Signed-off-by: vlad --- drivers/gpu/drm/asahi/mem.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/asahi/mem.rs b/drivers/gpu/drm/asahi/mem.rs index 1f4518be0bf29..59f3722bbf286 100644 --- a/drivers/gpu/drm/asahi/mem.rs +++ b/drivers/gpu/drm/asahi/mem.rs @@ -34,13 +34,27 @@ pub(crate) fn set_page_tlbi(enabled: bool) { PAGE_TLBI.store(enabled, Ordering::Relaxed); } -/// Invalidates `len` bytes at `va` one 16 KiB page at a time, or the whole ASID above -/// [`PAGE_TLBI_MAX_PAGES`]. +/// Upper half of the GPU VA space. It holds the kernel mappings, the only ones whose leaves are +/// global (nG clear), and `tlbi aside1os` leaves global entries cached. A range here is +/// invalidated by VA, which matches global entries whatever the ASID, or with `tlbi vmalle1os`. +const UAT_UPPER_HALF: usize = 1 << 63; + +/// Whether `va` lies in the upper half, where leaves may be global. +fn may_be_global(va: usize) -> bool { + va & UAT_UPPER_HALF != 0 +} + +/// Invalidates `len` bytes at `va` one 16 KiB page at a time. Above [`PAGE_TLBI_MAX_PAGES`], it +/// invalidates the whole ASID instead, or every entry for an upper-half range. fn tlbi_pages(asid: Asid, va: usize, len: usize) { let start = va >> mmu::UAT_PGBIT; let end = (va + len + mmu::UAT_PGMSK) >> mmu::UAT_PGBIT; if end - start > PAGE_TLBI_MAX_PAGES { - tlbi_asid(asid); + if may_be_global(va) { + tlbi_all(); + } else { + tlbi_asid(asid); + } return; } for page in start..end { @@ -112,7 +126,12 @@ pub(crate) fn tlbi_range(asid: Asid, va: usize, len: usize) { tlbi_pages(asid, va, len); return; } + tlbi_range_ops(asid, va, len); +} +/// Invalidates `len` bytes at `va` with as many range operations as the range needs. +#[inline(always)] +fn tlbi_range_ops(asid: Asid, va: usize, len: usize) { let start_pg = va >> mmu::UAT_PGBIT; let end_pg = (va + len + mmu::UAT_PGMSK) >> mmu::UAT_PGBIT; @@ -183,8 +202,9 @@ const TLBI_RANGE_BADDR_MASK: u64 = (1 << 37) - 1; /// Invalidate a range of pages for a given ASID with a single range operation. /// /// Unlike [`tlbi_range`], which issues as many range operations as needed, this invalidates -/// ranges that do not fit one operation (and ranges starting at VA 0) by ASID. Single pages use a -/// page operation, at the CPU page size. +/// ranges that do not fit one operation (and ranges starting at VA 0) by ASID. Upper-half ranges +/// that do not fit one operation still take range operations, as their leaves may be global. +/// Single pages use a page operation, at the CPU page size. #[inline(always)] pub(crate) fn tlbi_range_or_asid(asid: Asid, va: usize, len: usize) { if debug_enabled(DebugFlags::ConservativeTlbi) { @@ -202,7 +222,11 @@ pub(crate) fn tlbi_range_or_asid(asid: Asid, va: usize, len: usize) { } if va == 0 || len >= TLBI_RANGE_MAX { - tlbi_asid(asid); + if may_be_global(va) { + tlbi_range_ops(asid, va, len); + } else { + tlbi_asid(asid); + } return; } if len <= PAGE_SIZE { From 3c14eebe3558d72d99f063f5b984779f4f94f4c7 Mon Sep 17 00:00:00 2001 From: vlad Date: Thu, 3 Dec 2026 23:49:56 +0200 Subject: [PATCH 2/2] drm/asahi: hw: t6050: enable range TLBI (page_tlbi = false) The M5 Pro inherited page_tlbi from the M5 Ultra policy. With it, every unmap of up to 2048 pages issues one tlbi vae1os per 16 KiB page, and any larger one flushes the whole ASID. The CPU is ARMv8.4, and the PublishedRoots path already encodes tlbi rvae1os operations for 16 KiB granules, so a range below 32 MiB takes one instruction. Larger user ranges flush the ASID, and larger kernel ranges, whose leaves are global, take range operations as well. Clear page_tlbi for the M5 Pro only. clean_tables stays set: no M4 or M5 SoC has run without it (T6040 and T6041 set it, and the T8142 entry stops at the identity check before the UAT is touched), so there is no proof yet that the walker snoops the CPU caches. Signed-off-by: vlad --- drivers/gpu/drm/asahi/hw/t6050.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/asahi/hw/t6050.rs b/drivers/gpu/drm/asahi/hw/t6050.rs index 57382e9622126..b53b3dec3123a 100644 --- a/drivers/gpu/drm/asahi/hw/t6050.rs +++ b/drivers/gpu/drm/asahi/hw/t6050.rs @@ -33,8 +33,14 @@ pub(crate) const CONFIG: Config = Config { reserved_slots: &[], perf: None, opps: 0, - // The M5 Max and Ultra's UAT policy, for the same firmware family. - uat_quirks: CONFIG_ULTRA.uat_quirks, + // The M5 Max and Ultra's UAT policy, for the same firmware family, with ranges invalidated + // by range TLBI operations (`tlbi rvae1os`, ARMv8.4) instead of one `tlbi vae1os` per page + // and a whole-ASID flush past 2048 pages. Table writes are still cleaned to the point of + // coherency: no M4 or M5 SoC has run with a walker assumed to snoop the CPU caches. + uat_quirks: crate::mmu::UatQuirks { + page_tlbi: false, + ..CONFIG_ULTRA.uat_quirks + }, region_c: None, chip: None, m5: Some(&crate::g17::fw::m5::initdata::J714),