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
12 changes: 12 additions & 0 deletions drivers/gpu/drm/asahi/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,18 @@ impl SceneG14V13_5 {
pub(crate) fn render_addresses(&self) -> (u64, u64) {
self.object.with(|raw, _| (raw.user_buffer.or(0).into(), raw.stats.into()))
}

/// Zeroes the first `bytes` of the Tail Pointer Cache, so that a fragment pass run after
/// a firmware recovery stopped its tiler finds no tiles.
///
/// # Safety
///
/// The GPU must not access the TPC during the call (its tiler is stopped and no other
/// render using this buffer is in flight), and no CPU reference into it may be live.
pub(crate) unsafe fn clear_tpc(&self, bytes: usize) -> Result {
// SAFETY: forwarded from the caller.
unsafe { self.object.tpc.zero_prefix(bytes) }
}
}

/// Host-owned parameter-buffer inputs that persist across firmware submissions.
Expand Down
532 changes: 508 additions & 24 deletions drivers/gpu/drm/asahi/g14_v27_2.rs

Large diffs are not rendered by default.

382 changes: 382 additions & 0 deletions drivers/gpu/drm/asahi/g14_v27_2_abi.rs

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions drivers/gpu/drm/asahi/g14_v27_2_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ impl Prep {
pub(crate) fn slot(&self) -> u32 {
self.scene.slot()
}

/// Clears the tiler's TPC storage after a firmware recovery stopped the tiler.
/// Zeroes the scene's TPC after a firmware recovery stopped its tiler.
///
/// # Safety
///
/// As [`crate::buffer::Scene::clear_tpc`].
pub(crate) unsafe fn clear_tpc(&self) -> Result {
// SAFETY: forwarded from the caller.
unsafe { self.scene.clear_tpc(self.tile.tpc_size) }
}
}

fn to_cmdbuf(r: &UapiRenderCommand) -> kernel::uapi::drm_asahi_cmd_render {
Expand Down
114 changes: 106 additions & 8 deletions drivers/gpu/drm/asahi/gpu/v27_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use super::*;
use kernel::device;
use crate::g14_v27_2_abi::{self, GrowRequest, QueueError};
use crate::g14_v27_2_abi::{self, EventKind, GrowRequest, QueueError};
use crate::g17::fw::m5::initdata as abi;
use crate::hw::{RegisterBaseV27_2, RegisterV27_2};
use crate::m3::m3_init_26 as directory26;
Expand Down Expand Up @@ -40,6 +40,11 @@ const STATISTICS_RING: usize = 16;
/// Ring geometries: the trace ring has 512 records of 0x48 bytes, the others 256.
const TRACE_RECORDS: u32 = 512;
const RING_RECORDS: u32 = 256;
use crate::g14_v27_2_abi::{
RECOVERY_GENERATION as CONTROL_RECOVERY_GENERATION, RECOVERY_PHASE as CONTROL_RECOVERY_PHASE,
RECOVERY_SNAPSHOT as CONTROL_RECOVERY_SNAPSHOT,
};
pub(crate) use crate::g14_v27_2_abi::{stopped_tiler, RECOVERY_SNAPSHOT_ENTRIES};
/// Queue capacity at position block +0x50 (16 x the 0x50 channels).
const QUEUE_CAPACITY: u32 = 0x500;
/// Globals offsets of the 13.5 shared power tables (HwDataShared1 and HwDataShared2): the 27.2
Expand Down Expand Up @@ -604,6 +609,11 @@ impl CommandPool {
}

pub(crate) enum FirmwareEvent {
/// Kind 0: the firmware stopped on a GPU fault and halted for recovery.
Fault,
/// Kind 4: the firmware timed out a command (the slot it names at +0xc) and halted for
/// recovery.
Timeout { slot: i32 },
Grow(GrowRequest),
ChannelError,
Notification,
Expand Down Expand Up @@ -695,6 +705,15 @@ impl RootV27_2 {
(queue.info(self.render_states[dm]), queue.event.current_noncoherent())
}

/// Whether the render queue's retired position reached `target`, stamp or not: a command
/// the firmware stopped for recovery leaves the queue without writing its stamp.
pub(crate) fn render_retired(&self, dm: usize, target: u32) -> Result<bool> {
let queue = &self.render_queues[dm];
crate::pgtable::clean_invalidate_poc(queue.positions.as_slice().as_ptr(), 0x60);
crate::pgtable::poc_barrier();
Ok(Self::word(&queue.positions, 0)? == target)
}

pub(crate) fn render_done(&self, dm: usize, target: u32, stamp: event::EventValue) -> Result<bool> {
let queue = &self.render_queues[dm];
crate::pgtable::clean_invalidate_poc(queue.positions.as_slice().as_ptr(), 0x60);
Expand All @@ -719,6 +738,27 @@ impl RootV27_2 {
for (name, queue) in ["tiler", "fragment"].into_iter().zip(&self.render_queues) {
Self::log_queue(dev, name, &queue.positions, &queue.queue, &queue.event);
}
let (generation, phase) = (self.halt_count(), self.recovery_phase());
let (snapshot, entries) = self.recovery_snapshot();
dev_err!(dev, "GPU: recovery generation={:?} phase={:?} snapshot ({} entries) {:x?}\n",
generation, phase, entries, &snapshot[..entries.min(snapshot.len())]);
}

/// The first valid recovery snapshot entries, {flags, target} in hex, and how many valid
/// entries there are in all (the first invalid entry ends the snapshot). Logging only.
pub(crate) fn recovery_snapshot(&self) -> ([(u32, u32); 4], usize) {
let mut first = [(0, 0); 4];
let mut entries = 0;
for i in 0..RECOVERY_SNAPSHOT_ENTRIES {
match self.recovery_entry(i) {
Ok((flags, target)) if flags & 1 != 0 => {
if let Some(slot) = first.get_mut(entries) { *slot = (flags, target); }
entries += 1;
}
_ => break,
}
}
(first, entries)
}

pub(crate) fn render_kick(dm: usize) -> u64 {
Expand Down Expand Up @@ -831,7 +871,8 @@ impl RootV27_2 {
&& job.event.current_noncoherent().raw() == job.expected_stamp)
}

pub(crate) fn peek_event(&self) -> Result<Option<(u32, FirmwareEvent)>> {
/// The record at the head of the event ring: its cursor, its raw kind word and its decoding.
pub(crate) fn peek_event(&self) -> Result<Option<(u32, u32, FirmwareEvent)>> {
use crate::m3::m3_channel_format::RECEIVE_BYTES;
let state = &self.channels[2 * EVENT_RING];
let ring = &self.channels[2 * EVENT_RING + 1];
Expand All @@ -844,14 +885,48 @@ impl RootV27_2 {
crate::pgtable::clean_invalidate_poc(ring.as_slice()[at..].as_ptr(), RECEIVE_BYTES);
crate::pgtable::poc_barrier();
let word = |offset| Self::word(ring, at + offset);
let event = match word(0)? {
6 => FirmwareEvent::Grow(GrowRequest::decode([
let kind = word(0)?;
let event = match g14_v27_2_abi::event_kind(kind) {
EventKind::Grow => FirmwareEvent::Grow(GrowRequest::decode([
word(4)?, word(8)?, word(0x0c)?, word(0x10)?, word(0x14)?, word(0x18)?,
])),
7 => FirmwareEvent::ChannelError,
_ => FirmwareEvent::Notification,
EventKind::Fault => FirmwareEvent::Fault,
EventKind::Timeout => FirmwareEvent::Timeout {
slot: word(g14_v27_2_abi::TIMEOUT_SLOT_OFFSET)? as i32,
},
EventKind::ChannelError => FirmwareEvent::ChannelError,
EventKind::Notification => FirmwareEvent::Notification,
};
Ok(Some((read, event)))
Ok(Some((read, kind, event)))
}

/// Validates a byte range of the control object before it is sliced or cache-maintained.
fn control_range(control: &fw::types::GpuArray<u8>, from: usize, bytes: usize) -> Result {
match from.checked_add(bytes) {
Some(end) if end <= control.as_slice().len() && from % 4 == 0 => Ok(()),
_ => Err(EINVAL),
}
}

/// Reads control words from `from` into `out`.
pub(crate) fn control_snapshot(&self, from: usize, out: &mut [u32]) -> Result {
Self::control_range(&self.control, from, out.len() * 4)?;
let at = self.control.as_slice()[from..].as_ptr();
crate::pgtable::clean_invalidate_poc(at, out.len() * 4);
crate::pgtable::poc_barrier();
for (i, w) in out.iter_mut().enumerate() {
*w = Self::word(&self.control, from + 4 * i)?;
}
Ok(())
}

/// Writes one control word and makes it visible to the firmware.
pub(crate) fn control_store(&self, at: usize, value: u32) -> Result {
Self::control_range(&self.control, at, 4)?;
self.control.shared_index(at)?.store(value, Ordering::Release);
crate::pgtable::clean_poc(self.control.as_slice()[at..].as_ptr(), 4);
crate::pgtable::poc_barrier();
Ok(())
}

pub(crate) fn consume_event(&self, cursor: u32) -> Result {
Expand All @@ -862,8 +937,31 @@ impl RootV27_2 {
Ok(())
}

/// The recovery phase. The firmware publishes 1 after it reported a fault or a command
/// timeout and stopped every queue; it runs nothing until the host's answers.
pub(crate) fn recovery_phase(&self) -> Result<u32> {
let mut word = [0u32];
self.control_snapshot(CONTROL_RECOVERY_PHASE, &mut word)?;
Ok(word[0])
}

/// Answers a recovery phase (2 at phase 1, 0 at phase 3).
pub(crate) fn answer_recovery(&self, phase: u32) -> Result {
self.control_store(CONTROL_RECOVERY_PHASE, phase)
}

/// Snapshot entry `i` of the stopped work: (flags, target). Bit 0 of flags marks a valid
/// entry; the first invalid one ends the snapshot.
pub(crate) fn recovery_entry(&self, i: usize) -> Result<(u32, u32)> {
if i >= RECOVERY_SNAPSHOT_ENTRIES { return Err(EINVAL); }
let mut words = [0u32; 2];
self.control_snapshot(CONTROL_RECOVERY_SNAPSHOT + 8 * i, &mut words)?;
Ok((words[0], words[1]))
}

/// The recovery generation (u64). Growth requests carry the value current when sent.
pub(crate) fn halt_count(&self) -> Result<u64> {
const OFFSET: usize = 0x4bf0;
const OFFSET: usize = CONTROL_RECOVERY_GENERATION;
crate::pgtable::clean_invalidate_poc(self.control.as_slice()[OFFSET..].as_ptr(), 8);
crate::pgtable::poc_barrier();
Ok(u64::from(Self::word(&self.control, OFFSET)?)
Expand Down
18 changes: 18 additions & 0 deletions drivers/gpu/drm/asahi/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,24 @@ impl<U: Allocation<u8>> GpuArray<u8, U> {
// this word only in aligned 32-bit transactions while the ring is live.
Ok(unsafe { core::sync::atomic::AtomicU32::from_ptr(ptr) })
}

/// Zeroes the first `bytes` through the raw CPU mapping, without creating a Rust reference
/// to the payload, and cleans them to the point of coherency for the GPU.
///
/// # Safety
///
/// No CPU reference into the range may be live, and no device may access it during the call.
pub(crate) unsafe fn zero_prefix(&self, bytes: usize) -> Result {
if bytes > self.len {
return Err(ERANGE);
}
// SAFETY: `raw` is valid for `len` bytes (type invariant) and `bytes <= len`; the caller
// guarantees that nothing else accesses the range meanwhile.
unsafe { core::ptr::write_bytes(self.raw, 0, bytes) };
crate::pgtable::clean_poc(self.raw.cast_const(), bytes);
crate::pgtable::poc_barrier();
Ok(())
}
}

impl<T, U: Allocation<T>> Deref for GpuArray<T, U> {
Expand Down
5 changes: 5 additions & 0 deletions tools/asahi/g14-v27-2-abi-tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
//! Host tests of the production G14 firmware 27.2 queue, event and recovery records.
#![allow(dead_code)]
#[path = "../../drivers/gpu/drm/asahi/g14_v27_2_abi.rs"]
mod g14_v27_2_abi;
2 changes: 2 additions & 0 deletions tools/asahi/run-host-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ def mutations(flag, names, base, expect=KILL):
# Production G17 decoder vectors also build directly with rustc --test.
ASAHI_RUST += ['g17-telemetry-tests.rs', 'legacy-frequency-tests.rs']
ASAHI_RUST.append('m3-firmware-tests.rs')
# G14 firmware 27.2 queue, event and recovery records (its KUnit suite, run on the host).
ASAHI_RUST.append('g14-v27-2-abi-tests.rs')
ASAHI_RUST.append('pmp-alloc-tests.rs')

# Python that runs on the host but imports its neighbours, so it keeps the
Expand Down
Loading