Implement the GSP sequencer which culminates in INIT_DONE message being
received from the GSP indicating that the GSP has successfully booted.

This is just initial sequencer support, the actual commands will be
added in the next patches.

Signed-off-by: Joel Fernandes <joelagn...@nvidia.com>
---
 drivers/gpu/nova-core/gpu.rs           |  16 +-
 drivers/gpu/nova-core/gsp.rs           |   2 +
 drivers/gpu/nova-core/gsp/cmdq.rs      |   4 -
 drivers/gpu/nova-core/gsp/sequencer.rs | 212 +++++++++++++++++++++++++
 drivers/gpu/nova-core/sbuffer.rs       |   1 -
 5 files changed, 229 insertions(+), 6 deletions(-)
 create mode 100644 drivers/gpu/nova-core/gsp/sequencer.rs

diff --git a/drivers/gpu/nova-core/gpu.rs b/drivers/gpu/nova-core/gpu.rs
index f86221a681e2..bf670f6b6773 100644
--- a/drivers/gpu/nova-core/gpu.rs
+++ b/drivers/gpu/nova-core/gpu.rs
@@ -312,7 +312,7 @@ pub(crate) fn new(
 
         Self::run_fwsec_frts(pdev.as_ref(), &gsp_falcon, bar, &bios, 
&fb_layout)?;
 
-        let libos = gsp::GspMemObjects::new(pdev, bar, &fw, &fb_layout)?;
+        let mut libos = gsp::GspMemObjects::new(pdev, bar, &fw, &fb_layout)?;
         let libos_handle = libos.libos_dma_handle();
         let wpr_handle = libos.wpr_meta.dma_handle();
 
@@ -366,6 +366,20 @@ pub(crate) fn new(
             gsp_falcon.is_riscv_active(bar)?,
         );
 
+        let libos_dma_handle = libos.libos_dma_handle();
+
+        // Create and run the GSP sequencer
+        gsp::sequencer::GspSequencer::run(
+            &mut libos.cmdq,
+            &fw,
+            libos_dma_handle,
+            &gsp_falcon,
+            &sec2_falcon,
+            pdev.as_ref(),
+            &bar,
+            Delta::from_secs(10),
+        )?;
+
         Ok(pin_init!(Self {
             spec,
             bar: devres_bar,
diff --git a/drivers/gpu/nova-core/gsp.rs b/drivers/gpu/nova-core/gsp.rs
index 9776c643f527..467d0453130a 100644
--- a/drivers/gpu/nova-core/gsp.rs
+++ b/drivers/gpu/nova-core/gsp.rs
@@ -27,6 +27,8 @@
 pub(crate) mod cmdq;
 pub(crate) mod commands;
 
+pub(crate) mod sequencer;
+
 pub(crate) const GSP_PAGE_SHIFT: usize = 12;
 pub(crate) const GSP_PAGE_SIZE: usize = 1 << GSP_PAGE_SHIFT;
 pub(crate) const GSP_HEAP_ALIGNMENT: Alignment = Alignment::new(1 << 20);
diff --git a/drivers/gpu/nova-core/gsp/cmdq.rs 
b/drivers/gpu/nova-core/gsp/cmdq.rs
index 4e4fbaa81e8e..1a7785627af3 100644
--- a/drivers/gpu/nova-core/gsp/cmdq.rs
+++ b/drivers/gpu/nova-core/gsp/cmdq.rs
@@ -182,7 +182,6 @@ pub(crate) struct GspQueueMessage<'a> {
 type GspQueueMessageData<'a, M> = (&'a M, 
Option<SBuffer<core::array::IntoIter<&'a [u8], 2>>>);
 
 impl<'a> GspQueueMessage<'a> {
-    #[expect(unused)]
     pub(crate) fn try_as<M: GspMessageFromGsp>(&'a self) -> 
Result<GspQueueMessageData<'a, M>> {
         if self.rpc_header.function != M::FUNCTION {
             return Err(ERANGE);
@@ -204,7 +203,6 @@ pub(crate) fn try_as<M: GspMessageFromGsp>(&'a self) -> 
Result<GspQueueMessageDa
         Ok((msg, sbuf))
     }
 
-    #[expect(unused)]
     pub(crate) fn ack(self) -> Result {
         self.cmdq.ack_msg(self.rpc_header.length)?;
 
@@ -519,7 +517,6 @@ pub(crate) fn msg_from_gsp_available(&self) -> bool {
         true
     }
 
-    #[expect(unused)]
     pub(crate) fn wait_for_msg_from_gsp(&self, timeout: Delta) -> Result {
         wait_on(timeout, || {
             if self.msg_from_gsp_available() {
@@ -530,7 +527,6 @@ pub(crate) fn wait_for_msg_from_gsp(&self, timeout: Delta) 
-> Result {
         })
     }
 
-    #[expect(unused)]
     pub(crate) fn receive_msg_from_gsp<'a>(&'a mut self) -> 
Result<GspQueueMessage<'a>> {
         const HEADER_SIZE: u32 = (size_of::<GspMsgHeader>() + 
size_of::<GspRpcHeader>()) as u32;
 
diff --git a/drivers/gpu/nova-core/gsp/sequencer.rs 
b/drivers/gpu/nova-core/gsp/sequencer.rs
new file mode 100644
index 000000000000..918c3405b33c
--- /dev/null
+++ b/drivers/gpu/nova-core/gsp/sequencer.rs
@@ -0,0 +1,212 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! GSP Sequencer implementation for Pre-hopper GSP boot sequence.
+
+use core::mem::size_of;
+use kernel::alloc::flags::GFP_KERNEL;
+use kernel::device;
+use kernel::prelude::*;
+use kernel::time::Delta;
+
+use crate::driver::Bar0;
+use crate::falcon::{gsp::Gsp, sec2::Sec2, Falcon};
+use crate::firmware::Firmware;
+use crate::gsp::cmdq::{GspCmdq, GspMessageFromGsp};
+use crate::nvfw as fw;
+
+use kernel::transmute::FromBytes;
+use kernel::{dev_dbg, dev_err};
+
+unsafe impl FromBytes for fw::GSP_SEQUENCER_BUFFER_CMD {}
+unsafe impl FromBytes for fw::rpc_run_cpu_sequencer_v17_00 {}
+impl GspMessageFromGsp for fw::rpc_run_cpu_sequencer_v17_00 {
+    const FUNCTION: u32 = fw::NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER;
+}
+
+const CMD_SIZE: usize = size_of::<fw::GSP_SEQUENCER_BUFFER_CMD>();
+
+pub(crate) struct GspSequencerInfo<'a> {
+    pub info: &'a fw::rpc_run_cpu_sequencer_v17_00,
+    pub cmd_data: KVec<u8>,
+}
+
+/// GSP Sequencer Command types with payload data
+/// Commands have an opcode and a opcode-dependent struct.
+#[expect(dead_code)]
+pub(crate) enum GspSeqCmd {
+    RegWrite(fw::GSP_SEQ_BUF_PAYLOAD_REG_WRITE),
+    RegModify(fw::GSP_SEQ_BUF_PAYLOAD_REG_MODIFY),
+    RegPoll(fw::GSP_SEQ_BUF_PAYLOAD_REG_POLL),
+    RegStore(fw::GSP_SEQ_BUF_PAYLOAD_REG_STORE),
+}
+
+impl GspSeqCmd {
+    /// Creates a new GspSeqCmd from a firmware GSP_SEQUENCER_BUFFER_CMD
+    pub(crate) fn from_fw_cmd(cmd: &fw::GSP_SEQUENCER_BUFFER_CMD) -> 
Result<Self> {
+        match cmd.opCode {
+            _ => Err(EINVAL),
+        }
+    }
+
+    pub(crate) fn new(data: &[u8], dev: &device::Device<device::Bound>) -> 
Result<Self> {
+        let fw_cmd = 
fw::GSP_SEQUENCER_BUFFER_CMD::from_bytes(data).ok_or(EINVAL)?;
+        let cmd = Self::from_fw_cmd(fw_cmd)?;
+
+        if data.len() < cmd.size_bytes() {
+            dev_err!(dev, "data is not enough for command.\n");
+            return Err(EINVAL);
+        }
+
+        Ok(cmd)
+    }
+
+    /// Get the size of this command in bytes, the command consists of
+    /// a 4-byte opcode, and a variable-sized payload.
+    pub(crate) fn size_bytes(&self) -> usize {
+        0
+    }
+}
+
+#[expect(dead_code)]
+pub(crate) struct GspSequencer<'a> {
+    pub seq_info: GspSequencerInfo<'a>,
+    pub bar: &'a Bar0,
+    pub sec2_falcon: &'a Falcon<Sec2>,
+    pub gsp_falcon: &'a Falcon<Gsp>,
+    pub libos_dma_handle: u64,
+    pub fw: &'a Firmware,
+    pub dev: &'a device::Device<device::Bound>,
+}
+
+pub(crate) trait GspSeqCmdRunner {
+    fn run(&self, sequencer: &GspSequencer<'_>) -> Result;
+}
+
+impl GspSeqCmdRunner for GspSeqCmd {
+    fn run(&self, _seq: &GspSequencer<'_>) -> Result {
+        Ok(())
+    }
+}
+
+pub(crate) struct GspSeqIter<'a> {
+    cmd_data: &'a [u8],
+    current_offset: usize, // Tracking the current position
+    total_cmds: u32,
+    cmds_processed: u32,
+    dev: &'a device::Device<device::Bound>,
+}
+
+impl<'a> Iterator for GspSeqIter<'a> {
+    type Item = Result<GspSeqCmd>;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        // Stop if we've processed all commands or reached the end of data
+        if self.cmds_processed >= self.total_cmds || self.current_offset >= 
self.cmd_data.len() {
+            return None;
+        }
+
+        // Check if we have enough data for opcode
+        let opcode_size = size_of::<fw::GSP_SEQ_BUF_OPCODE>();
+        if self.current_offset + opcode_size > self.cmd_data.len() {
+            return Some(Err(EINVAL));
+        }
+
+        let offset = self.current_offset;
+
+        // Handle command creation based on available data,
+        // zero-pad if necessary (since last command may not be full size).
+        let mut buffer = [0u8; CMD_SIZE];
+        let copy_len = if offset + CMD_SIZE <= self.cmd_data.len() {
+            CMD_SIZE
+        } else {
+            self.cmd_data.len() - offset
+        };
+        buffer[..copy_len].copy_from_slice(&self.cmd_data[offset..offset + 
copy_len]);
+        let cmd_result = GspSeqCmd::new(&buffer, self.dev);
+
+        cmd_result.map_or_else(
+            |_err| {
+                dev_err!(self.dev, "Error parsing command at offset {}", 
offset);
+                None
+            },
+            |cmd| {
+                self.current_offset += cmd.size_bytes();
+                self.cmds_processed += 1;
+                Some(Ok(cmd))
+            },
+        )
+    }
+}
+
+impl<'a, 'b> IntoIterator for &'b GspSequencer<'a> {
+    type Item = Result<GspSeqCmd>;
+    type IntoIter = GspSeqIter<'b>;
+
+    fn into_iter(self) -> Self::IntoIter {
+        let cmd_data = &self.seq_info.cmd_data[..];
+
+        GspSeqIter {
+            cmd_data,
+            current_offset: 0,
+            total_cmds: self.seq_info.info.cmdIndex,
+            cmds_processed: 0,
+            dev: self.dev,
+        }
+    }
+}
+
+impl<'a> GspSequencer<'a> {
+    pub(crate) fn run(
+        cmdq: &mut GspCmdq,
+        fw: &'a Firmware,
+        libos_dma_handle: u64,
+        gsp_falcon: &'a Falcon<Gsp>,
+        sec2_falcon: &'a Falcon<Sec2>,
+        dev: &'a device::Device<device::Bound>,
+        bar: &'a Bar0,
+        timeout: Delta,
+    ) -> Result {
+        cmdq.wait_for_msg_from_gsp(timeout)?;
+        let msg = cmdq.receive_msg_from_gsp()?;
+
+        let (info, mut sbuf) = 
msg.try_as::<fw::rpc_run_cpu_sequencer_v17_00>()?;
+        let cmd_data = match sbuf {
+            Some(ref mut sbuf) => sbuf.read_into_kvec(GFP_KERNEL),
+            _ => Err(EINVAL),
+        }?;
+        let seq_info = GspSequencerInfo { info, cmd_data };
+
+        let sequencer = GspSequencer {
+            seq_info,
+            bar,
+            sec2_falcon,
+            gsp_falcon,
+            libos_dma_handle,
+            fw,
+            dev,
+        };
+
+        dev_dbg!(dev, "Running CPU Sequencer commands\n");
+
+        for cmd_result in &sequencer {
+            match cmd_result {
+                Ok(cmd) => cmd.run(&sequencer)?,
+                Err(e) => {
+                    dev_err!(
+                        dev,
+                        "Error running command at index {}\n",
+                        sequencer.seq_info.info.cmdIndex
+                    );
+                    return Err(e);
+                }
+            }
+        }
+
+        dev_dbg!(dev, "CPU Sequencer commands completed successfully\n");
+
+        drop(sbuf);
+        msg.ack()?;
+
+        Ok(())
+    }
+}
diff --git a/drivers/gpu/nova-core/sbuffer.rs b/drivers/gpu/nova-core/sbuffer.rs
index b1b8c4536d2f..528ff8dc6062 100644
--- a/drivers/gpu/nova-core/sbuffer.rs
+++ b/drivers/gpu/nova-core/sbuffer.rs
@@ -129,7 +129,6 @@ pub(crate) fn read_exact(&mut self, mut dst: &mut [u8]) -> 
Result {
     /// Read all the remaining data into a `KVec`.
     ///
     /// `self` will be empty after this operation.
-    #[expect(unused)]
     pub(crate) fn read_into_kvec(&mut self, flags: kernel::alloc::Flags) -> 
Result<KVec<u8>> {
         let mut buf = KVec::<u8>::new();
 
-- 
2.34.1

Reply via email to