Skip to content
16 changes: 11 additions & 5 deletions drivers/gpu/drm/asahi/g14_v27_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ pub(crate) fn start<'a>(
gpu::GraphProfile::G14V27_2)?;
let root = RootV27_2::build(&graph, &board)?;
let pool = CommandPool::new(&mut gpu::GpuManager::alloc(&*graph), cfg)?;
// Timestamp buffers are bound in the timestamp aperture while the firmware runs, and the
// window only carries the tables that exist now: link the aperture's tables first.
graph.kernel_uat().prepare_kernel_tables(&[graph.timestamp_range()])?;
window.mirror(graph.kernel_uat())?;
window.install()?;
window.announce();
Expand Down Expand Up @@ -670,7 +673,8 @@ impl Runtime {
};
let last = index + 1 == packet.commands.len();
let result = match command {
Command::Compute(control) => self.publish_compute(state, &packet, control, last, slots[0]),
Command::Compute(control) =>
self.publish_compute(state, &packet, index, control, last, slots[0]),
Command::Render { command, usc } =>
self.publish_render(state, &packet, index, &command, usc, last, slots),
};
Expand All @@ -687,7 +691,7 @@ impl Runtime {
}

fn publish_compute(
&self, state: &mut State, packet: &Arc<Packet>,
&self, state: &mut State, packet: &Arc<Packet>, index: usize,
control: crate::m3::agx_compute::Control, last: bool, slot: usize,
) -> Result {
let bind = self.graph.kernel_uat().bind(&packet.vm)?;
Expand All @@ -700,6 +704,8 @@ impl Runtime {
cdm_end: control.end,
usc_base: control.usc_base,
preempt,
timestamps: packet.timestamp_addresses(index),
helper: control.helper,
};
let command = state.pool.encode(slot, &state.queue, stamp.raw(), &job)?;
let submission = state.root.stage_compute(command)?;
Expand Down Expand Up @@ -1210,8 +1216,8 @@ impl gpu::Gpu for Backend {
Ok(KBox::new(crate::m3::m3_submit::Queue::new_g14_v27_2(self.runtime.clone(), self.scheduler.clone(),
vm, priority, usc_exec_base, ualloc_priv, self.preempt_bytes, render)?, GFP_KERNEL)?)
}
fn map_timestamp_buffer(&self, _bo: gem::ObjectRef, _range: Range<usize>) -> Result<crate::timestamp::Buffer> {
// Timestamp buffers would need new tables in the firmware window after boot.
Err(ENOTSUPP)
fn map_timestamp_buffer(&self, bo: gem::ObjectRef, range: Range<usize>) -> Result<crate::timestamp::Buffer> {
// The aperture's tables were linked before the firmware window was mirrored (start()).
<GpuManagerG14V13_5 as gpu::Gpu>::map_timestamp_buffer(&self.graph, bo, range)
}
}
110 changes: 110 additions & 0 deletions drivers/gpu/drm/asahi/g14_v27_2_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,55 @@ pub(crate) const GLOBALS_PROGRESS: [(usize, u32); 8] = [
/// Size of the 27.2 Globals object.
pub(crate) const GLOBALS_BYTES: usize = 0xe48;

/// Where the 27.2 compute command (type 3, 0x358 bytes) keeps the user timestamp pair: right
/// after the command time (+0x2c4) and the internal start/end pointers (+0x2cc), the order of
/// the 13.5 compute command and of the 27.2 render commands (vertex +0x5e4, fragment +0x948).
pub(crate) const COMPUTE_USER_TIMESTAMPS: usize = 0x2dc;

/// The 27.2 compute command's JobMeta flush_stamps word, after the completion stamp, firmware
/// stamp, stamp value and slot (+0x298..+0x2b4); the render commands keep it at the same place
/// of their JobMeta (vertex +0x5a0, fragment +0x904), and the M3 27.2 compute command sets it
/// when it does not coalesce. Set, the firmware flushes the command's stamps when it completes.
pub(crate) const COMPUTE_FLUSH_STAMPS: usize = 0x2b4;
/// The 13.5 compute JobParameters1 inside the 27.2 compute command.
pub(crate) const COMPUTE_JOB_PARAMETERS1: usize = 0x68;

/// The value of a 27.2 Timestamp operation's user word: like the 13.5 and the M3 27.2
/// operations it names the command's start/end pair of user destinations, not a destination.
/// Zero when the command has no user timestamps.
pub(crate) fn user_timestamp_pointer(pair_va: u64, pair: [u64; 2]) -> u64 {
if pair == [0; 2] {
0
} else {
pair_va
}
}

fn put64(b: &mut [u8], at: usize, v: u64) {
b[at..at + 8].copy_from_slice(&v.to_le_bytes());
}

fn put32(b: &mut [u8], at: usize, v: u32) {
b[at..at + 4].copy_from_slice(&v.to_le_bytes());
}

/// Writes a compute helper program (binary: USC offset | 1, data, configuration) into the
/// 13.5 JobParameters1 of a 27.2 compute command, at the places the 13.5 G14 compute path
/// fills: program +0x40, argument +0x48, configuration +0x50.
pub(crate) fn encode_compute_helper(cmd: &mut [u8], binary: u32, data: u64, config: u32) {
put32(cmd, COMPUTE_JOB_PARAMETERS1 + 0x40, binary);
put64(cmd, COMPUTE_JOB_PARAMETERS1 + 0x48, data);
put32(cmd, COMPUTE_JOB_PARAMETERS1 + 0x50, config);
}

/// Writes the user timestamp pair of a zeroed 27.2 compute command at `cmd_va` and returns the
/// Timestamp operations' user word ([`user_timestamp_pointer`]).
pub(crate) fn encode_compute_timestamps(cmd: &mut [u8], cmd_va: u64, timestamps: [u64; 2]) -> u64 {
put64(cmd, COMPUTE_USER_TIMESTAMPS, timestamps[0]);
put64(cmd, COMPUTE_USER_TIMESTAMPS + 8, timestamps[1]);
user_timestamp_pointer(cmd_va + COMPUTE_USER_TIMESTAMPS as u64, timestamps)
}

#[cfg_attr(not(test), kunit_tests(asahi_g14_fw_queues))]
#[cfg_attr(test, allow(dead_code))]
mod tests {
Expand Down Expand Up @@ -186,4 +235,65 @@ mod tests {
assert!(GLOBALS_PROGRESS.iter().any(|&(o, v)| o == at && v != 0));
}
}

/// Size of the 27.2 compute command.
const COMPUTE_COMMAND_BYTES: usize = 0x358;

/// Byte ranges `encode_compute` (gpu/v27_2.rs) writes in the 27.2 compute command, and
/// the words its stream hands the firmware to write (+0x264, +0x2c4, +0x304, +0x314,
/// +0x340).
const COMPUTE_WRITES: &[(usize, usize)] = &[
(0, 4), (4, 8), (0xc, 4), (0x10, 8),
(0x68, 8), (0x70, 8), (0x78, 8), (0x80, 8), (0x88, 8), (0x90, 8), (0x98, 8), (0xa0, 8),
(0xc0, 8),
(0x1e8, 8), (0x1f0, 4), (0x220, 8), (0x228, 8), (0x264, 8), (0x270, 4),
(0x298, 0x20), (0x2c4, 0x18), (0x304, 8), (0x314, 8), (0x334, 0x14),
];

/// Whether `len` bytes at `at` fit the command and are none the compute encoder uses.
fn free(at: usize, len: usize) -> bool {
at + len <= COMPUTE_COMMAND_BYTES
&& COMPUTE_WRITES.iter().all(|&(o, l)| o + l <= at || at + len <= o)
}

#[test]
fn the_compute_helper_lands_in_free_job_parameters() {
let mut cmd = [0u8; COMPUTE_COMMAND_BYTES];
encode_compute_helper(&mut cmd, 0x1235, 0x11_2233_4400, 0x7_0000);
let read64 = |at: usize| u64::from_le_bytes(cmd[at..at + 8].try_into().unwrap());
let read32 = |at: usize| u32::from_le_bytes(cmd[at..at + 4].try_into().unwrap());
assert_eq!((read32(0xa8), read64(0xb0), read32(0xb8)), (0x1235, 0x11_2233_4400, 0x7_0000));
for (at, len) in [(0xa8, 4), (0xb0, 8), (0xb8, 4)] {
assert!(free(at, len));
}
}

#[test]
fn flush_stamps_follows_the_compute_job_meta_and_misses_the_user_pair() {
// The JobMeta words the encoder writes end right before it: slot at +0x2b0.
assert_eq!(COMPUTE_FLUSH_STAMPS, 0x2b0 + 4);
assert!(COMPUTE_FLUSH_STAMPS + 4 <= COMPUTE_USER_TIMESTAMPS);
assert_eq!(COMPUTE_FLUSH_STAMPS % 4, 0);
}

#[test]
fn a_timestamp_operation_names_the_pair_only_when_there_is_one() {
assert_eq!(user_timestamp_pointer(0x1000, [0, 0]), 0);
assert_eq!(user_timestamp_pointer(0x1000, [0x40, 0]), 0x1000);
assert_eq!(user_timestamp_pointer(0x1000, [0, 0x48]), 0x1000);
}

#[test]
fn the_compute_user_pair_lands_in_a_free_place() {
let mut cmd = [0u8; COMPUTE_COMMAND_BYTES];
let pair = [0xffff_ffa1_8140_0010, 0xffff_ffa1_8140_0018];
assert_eq!(encode_compute_timestamps(&mut cmd, 0x8000, pair), 0x8000 + 0x2dc);
let read64 = |at: usize| u64::from_le_bytes(cmd[at..at + 8].try_into().unwrap());
assert_eq!([read64(0x2dc), read64(0x2e4)], pair);
assert!(free(0x2dc, 16));
// No timestamps leave the command as it was.
let mut empty = [0u8; COMPUTE_COMMAND_BYTES];
assert_eq!(encode_compute_timestamps(&mut empty, 0x8000, [0; 2]), 0);
assert!(empty.iter().all(|&b| b == 0));
}
}
19 changes: 12 additions & 7 deletions drivers/gpu/drm/asahi/g14_v27_2_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ fn to_cmdbuf(r: &UapiRenderCommand) -> kernel::uapi::drm_asahi_cmd_render {
c.sample_size_B = r.sample_size;
c.ppp_ctrl = r.ppp_control;
c.ppp_multisamplectl = r.multisample_control;
// The tiling parameters carry the vertex helper's configuration, as in the 13.5 command.
c.vertex_helper.cfg = r.vertex_helper.config;
c
}

Expand Down Expand Up @@ -224,7 +226,8 @@ fn header(b: &mut [u8], kind: u32, c: &Common) {
put64(b, 0x10, c.tracking);
}

/// Write the destinations used by both internal timestamp paths.
/// Write the destinations used by both internal timestamp paths. `user` names the command's
/// pair of user destinations, or is 0 ([`crate::g14_v27_2_abi::user_timestamp_pointer`]).
fn timestamp_op(
s: &mut [u8],
at: usize,
Expand All @@ -234,7 +237,7 @@ fn timestamp_op(
ts_pointers: u64,
internal0: u64,
internal1: u64,
user: [u64; 2],
user: u64,
) {
put32(s, at, if start { 0x8000_0003 } else { 3 });
put64(s, at + 0x04, command_time);
Expand All @@ -245,7 +248,7 @@ fn timestamp_op(
if start { ts_pointers } else { ts_pointers + 8 },
);
put64(s, at + 0x1c, q.queue);
put64(s, at + 0x24, user[usize::from(!start)]);
put64(s, at + 0x24, user);
put64(s, at + 0x2c, internal0);
put64(s, at + 0x34, internal1);
}
Expand Down Expand Up @@ -430,6 +433,7 @@ pub(crate) fn encode_vertex(
put64(cmd, 0x5dc, c.timestamps + 8);
put64(cmd, 0x5e4, timestamps[0]);
put64(cmd, 0x5ec, timestamps[1]);
let user = crate::g14_v27_2_abi::user_timestamp_pointer(c.cmd_va + 0x5e4, timestamps);
let internal0 = c.cmd_va + 0x61c;
let internal1 = c.cmd_va + 0x538;
let flag = c.cmd_va + 0x568;
Expand Down Expand Up @@ -469,7 +473,7 @@ pub(crate) fn encode_vertex(
ts,
internal0,
internal1,
timestamps,
user,
);
put32(s, 0x1e8, 0x0000_0100);
timestamp_op(
Expand All @@ -481,7 +485,7 @@ pub(crate) fn encode_vertex(
ts,
internal0,
internal1,
timestamps,
user,
);
let fin = 0x238;
put32(s, fin, 6);
Expand Down Expand Up @@ -933,6 +937,7 @@ pub(crate) fn encode_fragment(
let ts = va(frag_at(offset_of!(Rf, timestamp_pointers)));
put64(cmd, 0x948, timestamps[0]);
put64(cmd, 0x950, timestamps[1]);
let user = crate::g14_v27_2_abi::user_timestamp_pointer(va(0x948), timestamps);
let internal0 = va(0x980);
let internal1 = va(0x89c);
let (jp1, jp2) = (va(0x80), va(0x3c8));
Expand Down Expand Up @@ -989,7 +994,7 @@ pub(crate) fn encode_fragment(
ts,
internal0,
internal1,
timestamps,
user,
);
put32(s, 0x200, 0x0001_0000); // wait for the fragment pipe
timestamp_op(
Expand All @@ -1001,7 +1006,7 @@ pub(crate) fn encode_fragment(
ts,
internal0,
internal1,
timestamps,
user,
);
// op 8 FinalizeFragment (0xbc).
let fin = 0x250;
Expand Down
16 changes: 11 additions & 5 deletions drivers/gpu/drm/asahi/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ pub(crate) struct GpuManager {
dyncfg: hw::DynConfig,
pub(crate) initdata: fw::types::GpuObject<fw::initdata::InitData::ver>,
uat: mmu::Uat,
/// The firmware's timestamp aperture in the kernel VM, where timestamp buffers are bound.
timestamp_range: Range<u64>,
crashed: AtomicBool,
/// Set once the over-limit firmware-private garbage was reported as held.
garbage_held: AtomicBool,
Expand Down Expand Up @@ -746,10 +748,10 @@ impl GpuManager::ver {
profile: GraphProfile,
) -> Result<Arc<GpuManager::ver>> {
let dyncfg = Self::make_dyncfg(dev, res, cfg, &uat)?;
let private_end = match profile {
GraphProfile::Native => IOVA_KERN_PRIV_RANGE.end,
let (private_end, timestamp_range) = match profile {
GraphProfile::Native => (IOVA_KERN_PRIV_RANGE.end, IOVA_KERN_TIMESTAMP_RANGE),
#[cfg(CONFIG_DRM_ASAHI_G14_V27_2)]
GraphProfile::G14V27_2 => IOVA_G14_V27_2_TIMESTAMP_RANGE.start,
GraphProfile::G14V27_2 => (IOVA_G14_V27_2_TIMESTAMP_RANGE.start, IOVA_G14_V27_2_TIMESTAMP_RANGE),
};

let mut alloc = KernelAllocators {
Expand Down Expand Up @@ -828,7 +830,8 @@ impl GpuManager::ver {
false,
)?);

let mut mgr = Self::make_mgr(dev, cfg, dyncfg, uat, alloc, event_manager, initdata)?;
let mut mgr = Self::make_mgr(dev, cfg, dyncfg, uat, alloc, event_manager, initdata,
timestamp_range)?;

{
let fwctl = mgr.fwctl_channel.lock();
Expand Down Expand Up @@ -1005,6 +1008,7 @@ impl GpuManager::ver {
///
/// Force disable inlining to avoid blowing up the stack.
#[inline(never)]
#[allow(clippy::too_many_arguments)]
fn make_mgr(
dev: &AsahiDrmDevice,
cfg: &'static hw::HwConfig,
Expand All @@ -1013,6 +1017,7 @@ impl GpuManager::ver {
mut alloc: KernelAllocators,
event_manager: Arc<event::EventManager>,
initdata: KBox<fw::types::GpuObject<fw::initdata::InitData::ver>>,
timestamp_range: Range<u64>,
) -> Result<Pin<UniqueArc<GpuManager::ver>>> {
let mut pipes = PipeChannels::ver {
vtx: KVec::new(),
Expand Down Expand Up @@ -1113,6 +1118,7 @@ impl GpuManager::ver {
dyncfg: KBox::<hw::DynConfig>::into_inner(dyncfg),
initdata: KBox::<fw::types::GpuObject<fw::initdata::InitData::ver>>::into_inner(initdata),
uat: KBox::<mmu::Uat>::into_inner(uat),
timestamp_range,
io_mappings: KVec::new(),
next_mmio_iova: IOVA_KERN_MMIO_RANGE.start,
rtkit <- new_mutex!(None, "rtkit"),
Expand Down Expand Up @@ -1787,7 +1793,7 @@ impl Gpu for GpuManager::ver {
crate::timestamp::Buffer::firmware(bo.map_range_into_range(
self.uat.kernel_vm(),
range,
IOVA_KERN_TIMESTAMP_RANGE,
self.timestamp_range.clone(),
mmu::UAT_PGSZ as u64,
mmu::PROT_FW_SHARED_RW,
false,
Expand Down
17 changes: 16 additions & 1 deletion drivers/gpu/drm/asahi/gpu/v27_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ pub(crate) struct ComputeJob {
/// VA of the preemption buffer in the same context (13.5 layout: buffer 1 at +0,
/// buffers 2 to 5 at +compute_preempt1_size + 8n).
pub(crate) preempt: u64,
/// Firmware VAs of the user start and end timestamps, or 0.
pub(crate) timestamps: [u64; 2],
/// The helper program (scratch and stack setup), or zero.
pub(crate) helper: crate::m3::agx_uapi::UapiHelperProgram,
}

fn put32(b: &mut [u8], at: usize, v: u32) {
Expand Down Expand Up @@ -116,12 +120,16 @@ fn encode_compute(
put64(cmd, 0x2a0, q.fw_stamp);
put32(cmd, 0x2a8, stamp_value);
put32(cmd, 0x2b0, q.slot);
// flush_stamps, as the render commands and the M3 27.2 compute command set it.
put32(cmd, g14_v27_2_abi::COMPUTE_FLUSH_STAMPS, 1);
put64(cmd, 0x2cc, timestamps);
put64(cmd, 0x2d4, timestamps + 8);
// Command-local dependency latch, enabled with seed 0; the firmware clears 0x10 bytes
// through the pointer at +0x334.
put32(cmd, 0x33c, 1);
put64(cmd, 0x334, timestamps + 0x20);
let user = g14_v27_2_abi::encode_compute_timestamps(cmd, cmd_va, job.timestamps);
g14_v27_2_abi::encode_compute_helper(cmd, job.helper.binary, job.helper.data, job.helper.config);

put32(stream, 0, 11);
// 13.5 StartCompute.unk_28, at the same place in this layout (the 13.5 G14 path writes 1).
Expand All @@ -141,6 +149,7 @@ fn encode_compute(
put64(stream, at + 0x0c, cmd_va + 0x2cc);
put64(stream, at + 0x14, cmd_va + selected);
put64(stream, at + 0x1c, q.queue);
put64(stream, at + 0x24, user);
put64(stream, at + 0x2c, cmd_va + 0x314);
put64(stream, at + 0x34, cmd_va + 0x264);
}
Expand Down Expand Up @@ -357,7 +366,8 @@ impl BootJob {
let cdm = self.client_va + self.preempt1 + 0x2000;
let job = ComputeJob {
context: 0, sequence: 0, cdm_base: cdm, cdm_end: cdm + 8,
usc_base: 0x11_0000_0000, preempt: self.client_va,
usc_base: 0x11_0000_0000, preempt: self.client_va, timestamps: [0; 2],
helper: crate::m3::agx_uapi::UapiHelperProgram { binary: 0, config: 0, data: 0 },
};
let (cmd_va, stream_va) = (self.command.gpu_va().get(), self.stream.gpu_va().get());
encode_compute(self.command.as_mut_slice(), cmd_va, self.stream.as_mut_slice(), stream_va,
Expand Down Expand Up @@ -529,6 +539,11 @@ impl GpuManagerG14V13_5 {
pub(crate) fn kernel_uat(&self) -> &mmu::Uat {
&self.uat
}

/// The firmware timestamp aperture that timestamp buffers are bound in.
pub(crate) fn timestamp_range(&self) -> Range<u64> {
self.timestamp_range.clone()
}
}

impl RootV27_2 {
Expand Down
4 changes: 4 additions & 0 deletions drivers/gpu/drm/asahi/m3/agx_compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ pub(crate) struct Control {
pub base: u64,
pub end: u64,
pub usc_base: u64,
/// The helper program, zero unless the queue's command builder encodes it
/// (`m3_submit::Queue::samplers_and_helpers`).
#[cfg_attr(not(CONFIG_DRM_ASAHI_G14_V27_2), allow(dead_code))]
pub helper: crate::m3::agx_uapi::UapiHelperProgram,
}
Loading
Loading