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
14 changes: 12 additions & 2 deletions drivers/gpu/drm/asahi/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,20 @@ config DRM_ASAHI_DEBUG_ALLOCATOR
Say N unless you are debugging firmware structures or porting to a
new firmware version.

config DRM_ASAHI_UAT_ADOPTION
bool
help
The firmware UAT handoff adoption shared by the G15 backend and the
T8122 runtime (g15/uat.rs, g15/regs.rs).

config DRM_ASAHI_M3_T8122
bool "M3 MacBook Air GPU runtime (T8122, 14.8.3 firmware)"
depends on DRM_ASAHI
# The runtime adopts the firmware's UAT through the G15 backend's handoff helpers.
depends on DRM_ASAHI_G15
# The runtime adopts the firmware's UAT as the G15 backend does, without
# needing that backend: M3 Pro/Max keep their own setting.
# Its PMP votes call apple-pmp directly (m3_device.rs).
depends on APPLE_PMP=y || (APPLE_PMP=m && DRM_ASAHI=m)
select DRM_ASAHI_UAT_ADOPTION
select APPLE_T8122_GPU_PMP_LINK
select CRYPTO_LIB_SHA256
default n
Expand All @@ -59,6 +68,7 @@ config DRM_ASAHI_G15
select APPLE_T6030_GPU_PMP_LINK
select CRYPTO_LIB_SHA256
select RUST_FW_LOADER_ABSTRACTIONS
select DRM_ASAHI_UAT_ADOPTION
default n
help
Enable the native G15 backend for explicitly admitted M3 Pro/Max
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ obj-$(CONFIG_DRM_ASAHI) += asahi.o
asahi-y := asahi_core.o sysfs.o

asahi-$(CONFIG_DRM_ASAHI_G15) += g15/glue.o
asahi-$(CONFIG_DRM_ASAHI_UAT_ADOPTION) += g15/uat_glue.o
4 changes: 4 additions & 0 deletions drivers/gpu/drm/asahi/asahi_core.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
#![recursion_limit = "2048"]

Expand All @@ -17,6 +17,10 @@
mod g17;
#[cfg(CONFIG_DRM_ASAHI_G15)]
mod g15;
// Without the G15 backend, only the UAT adoption the T8122 runtime shares with it.
#[cfg(all(CONFIG_DRM_ASAHI_UAT_ADOPTION, not(CONFIG_DRM_ASAHI_G15)))]
#[path = "g15/adopt.rs"]
mod g15;
mod gpu;
mod hw;
mod initdata;
Expand Down
32 changes: 32 additions & 0 deletions drivers/gpu/drm/asahi/g15/adopt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! Without the G15 backend (`CONFIG_DRM_ASAHI_G15=n`), the parts of it the T8122 runtime uses to
//! adopt the firmware's UAT: the handoff window ([`uat`]), its mappings ([`regs`]) and the
//! hardware constants they name ([`hw`]). Nothing here probes a device.

// Only the adoption is used; the rest of these files serves the G15 backend.
#![allow(dead_code)]

// hw.rs's `t6031` submodule is cfg-gated on G15; were it built here, a #[path]-loaded file
// would look for it at g15/t6031.rs, not g15/hw/t6031.rs.
#[path = "hw.rs"]
pub(crate) mod hw;
#[path = "regs.rs"]
pub(crate) mod regs;
#[path = "uat.rs"]
pub(crate) mod uat;

use kernel::prelude::*;

extern "C" {
fn asahi_g15_memory_mode(start: u64, size: u64) -> u32;
}

/// WB for complete reserved RAM, WC for aligned non-RAM carveouts; reject everything else.
pub(crate) fn memory_mode(start: u64, size: u64) -> Result<u32> {
// SAFETY: Only queries resources and page metadata; it does not create a mapping.
match unsafe { asahi_g15_memory_mode(start, size) } {
0 => Err(EINVAL),
mode => Ok(mode),
}
}
46 changes: 0 additions & 46 deletions drivers/gpu/drm/asahi/g15/glue.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
* G15 (T6030/T6031, M3 Pro/Max) helpers that have no Rust bindings: static inlines,
Expand Down Expand Up @@ -30,9 +30,6 @@
void asahi_g15_reprobe(void);
void asahi_g15_report_loader_gpu(void);
int asahi_g15_adt_phram(u64 *start, u64 *size);
bool asahi_g15_ram_disjoint(u64 start, u64 size);
bool asahi_g15_ram_within(u64 start, u64 size);
unsigned int asahi_g15_memory_mode(u64 start, u64 size);
void __iomem *asahi_g15_ioremap_wc(u64 start, size_t size);
bool asahi_g15_dma_coherent(struct device *dev);
int asahi_g15_pmp_prepare(struct device *dev);
Expand Down Expand Up @@ -237,13 +234,6 @@
return 0;
}

/* The range is disjoint from System RAM (ADT region admission). */
bool asahi_g15_ram_disjoint(u64 start, u64 size)
{
return region_intersects(start, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE) ==
REGION_DISJOINT;
}

/*
* Whether DMA mappings of the device skip cache maintenance. The G15 firmware
* objects rely on the DMA mapping cleaning their pages to the point of
Expand All @@ -254,42 +244,6 @@
return dev_is_dma_coherent(dev);
}

/*
* Every page must be valid, System RAM and still reserved from the allocator.
* REGION_INTERSECTS alone also accepts RAM mixed with undefined memory holes.
*/
bool asahi_g15_ram_within(u64 start, u64 size)
{
u64 end, pa;

if (!size || !IS_ALIGNED(start, PAGE_SIZE) ||
!IS_ALIGNED(size, PAGE_SIZE) || check_add_overflow(start, size, &end))
return false;
for (pa = start; pa < end; pa += PAGE_SIZE) {
unsigned long pfn = PHYS_PFN(pa);

if (!pfn_valid(pfn) || !page_is_ram(pfn) ||
!PageReserved(pfn_to_page(pfn)))
return false;
}
return true;
}

/* One classification for every adopted-UAT and firmware-region CPU mapping. */
unsigned int asahi_g15_memory_mode(u64 start, u64 size)
{
u64 end;

if (!size || !IS_ALIGNED(start, PAGE_SIZE) ||
!IS_ALIGNED(size, PAGE_SIZE) || check_add_overflow(start, size, &end))
return 0;
if (asahi_g15_ram_within(start, size))
return MEMREMAP_WB;
if (asahi_g15_ram_disjoint(start, size))
return MEMREMAP_WC;
return 0;
}

/* The inherited RTKit buffer inside firmware DATA. */
void __iomem *asahi_g15_ioremap_wc(u64 start, size_t size)
{
Expand Down
2 changes: 2 additions & 0 deletions drivers/gpu/drm/asahi/g15/hw.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! G15 hardware and firmware-ABI constants (layout values are shared by several modules; change
Expand All @@ -7,6 +7,7 @@

use core::ops::Range;

#[cfg(CONFIG_DRM_ASAHI_G15)]
pub(crate) mod t6031;

/// Identification register values of a qualified G15S.
Expand Down Expand Up @@ -94,6 +95,7 @@
}

/// The T6030 configuration.
#[cfg(CONFIG_DRM_ASAHI_G15)]
pub(crate) mod t6030 {
use super::super::Config;
use super::{ComputeRegs, FirmwareBuild, HwId, Layout};
Expand Down
3 changes: 2 additions & 1 deletion drivers/gpu/drm/asahi/g15/regs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! G15 MMIO: SGX identification, power, core and status windows, and ASC control.
Expand Down Expand Up @@ -125,7 +125,7 @@
if size == 0 {
return Err(EINVAL);
}
let mode = super::boot::glue::memory_mode(phys, size as u64)?;
let mode = super::memory_mode(phys, size as u64)?;
if mode == bindings::MEMREMAP_WC as u32 {
return Self::write_combined(phys, size);
}
Expand Down Expand Up @@ -252,6 +252,7 @@

/// Reads the identification words and the enabled-core mask (once the GPU is powered). A
/// SoC with more than 32 core slots carries the mask in two words.
#[cfg(CONFIG_DRM_ASAHI_G15)]
pub(crate) fn identity(&self, cfg: &super::Config) -> Result<Identity> {
let version = self.sgx.io().try_read32(SGX_ID)?;
let counts = self.sgx.io().try_read32(SGX_ID + 0x10)?;
Expand Down
6 changes: 5 additions & 1 deletion drivers/gpu/drm/asahi/g15/uat.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! The adopted UAT: PPL handoff, TTBAT context 0, the host window and TLB maintenance.
Expand Down Expand Up @@ -36,12 +36,14 @@
bindings, device,
io::Io,
prelude::*,
sync::Arc,
time::{delay::fsleep, Delta, Instant, Monotonic},
};
#[cfg(CONFIG_DRM_ASAHI_G15)]
use kernel::sync::Arc;

use super::hw;
use super::regs::{mb, rmb, wmb, Mapping};
#[cfg(CONFIG_DRM_ASAHI_G15)]
use crate::driver::AsahiDrmDevice as AsahiDevice;
use crate::{mmu, pgtable};

Expand All @@ -67,6 +69,7 @@
}

/// Creates the adopted UAT over the ADT regions (step 2 above).
#[cfg(CONFIG_DRM_ASAHI_G15)]
pub(crate) fn adopt(
dev: &AsahiDevice,
cfg: &'static super::Config,
Expand Down Expand Up @@ -199,6 +202,7 @@

impl Window {
/// Maps the UAT handoff regions ([`Mapping::region`]).
#[cfg(CONFIG_DRM_ASAHI_G15)]
pub(crate) fn new(adt: &super::adt::GpuAdt) -> Result<Self> {
Self::from_regions(adt.handoff.start, adt.ttbs.start, adt.tables.start, adt.shared_l2.start)
}
Expand Down
59 changes: 59 additions & 0 deletions drivers/gpu/drm/asahi/g15/uat_glue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
* C helpers of the UAT adoption (g15/uat.rs, g15/regs.rs), built for the G15
* backend and for the T8122 runtime, which adopts the firmware's UAT the same
* way (CONFIG_DRM_ASAHI_UAT_ADOPTION).
*/
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/mm.h>
#include <linux/overflow.h>
#include <linux/page-flags.h>
#include <linux/pfn.h>

bool asahi_g15_ram_disjoint(u64 start, u64 size);
bool asahi_g15_ram_within(u64 start, u64 size);
unsigned int asahi_g15_memory_mode(u64 start, u64 size);

/* The range is disjoint from System RAM (ADT region admission). */
bool asahi_g15_ram_disjoint(u64 start, u64 size)
{
return region_intersects(start, size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE) ==
REGION_DISJOINT;
}

/*
* Every page must be valid, System RAM and still reserved from the allocator.
* REGION_INTERSECTS alone also accepts RAM mixed with undefined memory holes.
*/
bool asahi_g15_ram_within(u64 start, u64 size)
{
u64 end, pa;

if (!size || !IS_ALIGNED(start, PAGE_SIZE) ||
!IS_ALIGNED(size, PAGE_SIZE) || check_add_overflow(start, size, &end))
return false;
for (pa = start; pa < end; pa += PAGE_SIZE) {
unsigned long pfn = PHYS_PFN(pa);

if (!pfn_valid(pfn) || !page_is_ram(pfn) ||
!PageReserved(pfn_to_page(pfn)))
return false;
}
return true;
}

/* One classification for every adopted-UAT and firmware-region CPU mapping. */
unsigned int asahi_g15_memory_mode(u64 start, u64 size)
{
u64 end;

if (!size || !IS_ALIGNED(start, PAGE_SIZE) ||
!IS_ALIGNED(size, PAGE_SIZE) || check_add_overflow(start, size, &end))
return 0;
if (asahi_g15_ram_within(start, size))
return MEMREMAP_WB;
if (asahi_g15_ram_disjoint(start, size))
return MEMREMAP_WC;
return 0;
}
22 changes: 11 additions & 11 deletions drivers/gpu/drm/asahi/mmu.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! GPU UAT (MMU) management
Expand Down Expand Up @@ -1477,7 +1477,7 @@
#[pin_data]
struct UatInner {
firmware: UatFirmware,
#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
clean_poc: bool,
#[pin]
shared: Mutex<UatShared>,
Expand Down Expand Up @@ -1665,7 +1665,7 @@
UatPageTable::new(geometry.ias, geometry.oas)?
};

#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
let page_table = {
let mut page_table = page_table;
if uat_inner.clean_poc {
Expand Down Expand Up @@ -1753,15 +1753,15 @@
/// Attach a status and job lifetime tracking before sharing a newly created user VM with
/// queues, without the G17 canonical context views, shared-object footprints and idle
/// residency reclaim of [`Self::with_status`]. Used by G15, whose user objects stay pinned.
#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
pub(crate) fn with_tracked_status(mut self) -> Result<Self> {
self.status = Some(Arc::pin_init(crate::g17::status::VmStatus::new(), GFP_KERNEL)?);
self.lifetime = Some(lifetime::VmLifetime::new()?);
Ok(self)
}

/// The identifier this VM was created with (G15 keeps per-VM state keyed by it).
#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
pub(crate) fn id(&self) -> u64 {
self.id
}
Expand All @@ -1773,7 +1773,7 @@
/// and invalidated to the point of coherency first (never only invalidated: CPU-written lines
/// may be dirty), so the copy sees what the GPU or a write-combined CPU view stored. The
/// mappings cannot change during the copy (GPUVM exec lock).
#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
pub(crate) fn read_user(&self, va: u64, buf: &mut [u8]) -> Result {
let mut inner = lock_vm(&self.gpuvm, None, false)?;
let mut done = 0usize;
Expand Down Expand Up @@ -2420,7 +2420,7 @@

/// Physical regions of a UAT whose handoff the generation has already established itself
/// ([`Uat::new_adopted`]). None of them needs to be in the kernel linear map.
#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
pub(crate) struct AdoptedRegions {
/// The handoff region (PPL magic, lock and flush requests).
pub(crate) handoff: Range<PhysicalAddr>,
Expand All @@ -2431,7 +2431,7 @@
}

impl Uat {
#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
/// Map a region given by its physical range, write-combined.
fn map_physical(range: &Range<PhysicalAddr>, min_size: usize) -> Result<UatRegion> {
let size: usize = (range.end.checked_sub(range.start).ok_or(EINVAL)?).try_into()?;
Expand Down Expand Up @@ -2466,7 +2466,7 @@
})
}

#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
/// Creates a `Uat` for a handoff that the caller establishes itself (G15).
///
/// This only maps the handoff and the TTBAT (write-combined); neither is written here. The
Expand Down Expand Up @@ -2548,13 +2548,13 @@
})
}

#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
/// Root of the kernel lower `Vm`, for TTBAT context 0 of an adopted handoff.
pub(crate) fn kernel_lower_root(&self) -> PhysicalAddr {
self.kernel_lower_vm.ttb()
}

#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
/// Root whose driver-window entry names the second-level table of [`Self::kernel_vm`]'s
/// driver window. With [`Self::new_adopted`] this is the shadow root that no context names.
pub(crate) fn kernel_window_root(&self) -> PhysicalAddr {
Expand Down Expand Up @@ -2900,7 +2900,7 @@
Arc::pin_init(
try_pin_init!(UatInner {
firmware,
#[cfg(CONFIG_DRM_ASAHI_G15)]
#[cfg(CONFIG_DRM_ASAHI_UAT_ADOPTION)]
clean_poc: false,
handoff_flush <- pin_init::pin_init_array_from_fn(|i| {
new_mutex!(HandoffFlush(&handoff.flush[i]), "handoff_flush")
Expand Down
Loading
Loading