xref: /linux/drivers/gpu/nova-core/gsp/cmdq.rs (revision 6fa6b5cb60490db2591bb93872b95f72315e5f53)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use core::{
4     cmp,
5     mem, //
6 };
7 
8 use kernel::{
9     device,
10     dma::{
11         CoherentAllocation,
12         DmaAddress, //
13     },
14     dma_write,
15     io::poll::read_poll_timeout,
16     prelude::*,
17     sync::aref::ARef,
18     time::Delta,
19     transmute::{
20         AsBytes,
21         FromBytes, //
22     },
23 };
24 
25 use crate::{
26     driver::Bar0,
27     gsp::{
28         fw::{
29             GspMsgElement,
30             MsgFunction,
31             MsgqRxHeader,
32             MsgqTxHeader, //
33         },
34         PteArray,
35         GSP_PAGE_SHIFT,
36         GSP_PAGE_SIZE, //
37     },
38     num,
39     regs,
40     sbuffer::SBufferIter, //
41 };
42 
43 /// Trait implemented by types representing a command to send to the GSP.
44 ///
45 /// The main purpose of this trait is to provide [`Cmdq::send_command`] with the information it
46 /// needs to send a given command.
47 ///
48 /// [`CommandToGsp::init`] in particular is responsible for initializing the command directly
49 /// into the space reserved for it in the command queue buffer.
50 ///
51 /// Some commands may be followed by a variable-length payload. For these, the
52 /// [`CommandToGsp::variable_payload_len`] and [`CommandToGsp::init_variable_payload`] need to be
53 /// defined as well.
54 pub(crate) trait CommandToGsp {
55     /// Function identifying this command to the GSP.
56     const FUNCTION: MsgFunction;
57 
58     /// Type generated by [`CommandToGsp::init`], to be written into the command queue buffer.
59     type Command: FromBytes + AsBytes;
60 
61     /// Error type returned by [`CommandToGsp::init`].
62     type InitError;
63 
64     /// In-place command initializer responsible for filling the command in the command queue
65     /// buffer.
66     fn init(&self) -> impl Init<Self::Command, Self::InitError>;
67 
68     /// Size of the variable-length payload following the command structure generated by
69     /// [`CommandToGsp::init`].
70     ///
71     /// Most commands don't have a variable-length payload, so this is zero by default.
72     fn variable_payload_len(&self) -> usize {
73         0
74     }
75 
76     /// Method initializing the variable-length payload.
77     ///
78     /// The command buffer is circular, which means that we may need to jump back to its beginning
79     /// while in the middle of a command. For this reason, the variable-length payload is
80     /// initialized using a [`SBufferIter`].
81     ///
82     /// This method will receive a buffer of the length returned by
83     /// [`CommandToGsp::variable_payload_len`], and must write every single byte of it. Leaving
84     /// unwritten space will lead to an error.
85     ///
86     /// Most commands don't have a variable-length payload, so this does nothing by default.
87     fn init_variable_payload(
88         &self,
89         _dst: &mut SBufferIter<core::array::IntoIter<&mut [u8], 2>>,
90     ) -> Result {
91         Ok(())
92     }
93 }
94 
95 /// Trait representing messages received from the GSP.
96 ///
97 /// This trait tells [`Cmdq::receive_msg`] how it can receive a given type of message.
98 pub(crate) trait MessageFromGsp: Sized {
99     /// Function identifying this message from the GSP.
100     const FUNCTION: MsgFunction;
101 
102     /// Error type returned by [`MessageFromGsp::read`].
103     type InitError;
104 
105     /// Type containing the raw message to be read from the message queue.
106     type Message: FromBytes;
107 
108     /// Method reading the message from the message queue and returning it.
109     ///
110     /// From a `Self::Message` and a [`SBufferIter`], constructs an instance of `Self` and returns
111     /// it.
112     fn read(
113         msg: &Self::Message,
114         sbuffer: &mut SBufferIter<core::array::IntoIter<&[u8], 2>>,
115     ) -> Result<Self, Self::InitError>;
116 }
117 
118 /// Number of GSP pages making the [`Msgq`].
119 pub(crate) const MSGQ_NUM_PAGES: u32 = 0x3f;
120 
121 /// Circular buffer of a [`Msgq`].
122 ///
123 /// This area of memory is to be shared between the driver and the GSP to exchange commands or
124 /// messages.
125 #[repr(C, align(0x1000))]
126 #[derive(Debug)]
127 struct MsgqData {
128     data: [[u8; GSP_PAGE_SIZE]; num::u32_as_usize(MSGQ_NUM_PAGES)],
129 }
130 
131 // Annoyingly we are forced to use a literal to specify the alignment of
132 // `MsgqData`, so check that it corresponds to the actual GSP page size here.
133 static_assert!(align_of::<MsgqData>() == GSP_PAGE_SIZE);
134 
135 /// Unidirectional message queue.
136 ///
137 /// Contains the data for a message queue, that either the driver or GSP writes to.
138 ///
139 /// Note that while the write pointer of `tx` corresponds to the `msgq` of the same instance, the
140 /// read pointer of `rx` actually refers to the `Msgq` owned by the other side.
141 /// This design ensures that only the driver or GSP ever writes to a given instance of this struct.
142 #[repr(C)]
143 // There is no struct defined for this in the open-gpu-kernel-source headers.
144 // Instead it is defined by code in `GspMsgQueuesInit()`.
145 // TODO: Revert to private once `IoView` projections replace the `gsp_mem` module.
146 pub(super) struct Msgq {
147     /// Header for sending messages, including the write pointer.
148     pub(super) tx: MsgqTxHeader,
149     /// Header for receiving messages, including the read pointer.
150     pub(super) rx: MsgqRxHeader,
151     /// The message queue proper.
152     msgq: MsgqData,
153 }
154 
155 /// Structure shared between the driver and the GSP and containing the command and message queues.
156 #[repr(C)]
157 // TODO: Revert to private once `IoView` projections replace the `gsp_mem` module.
158 pub(super) struct GspMem {
159     /// Self-mapping page table entries.
160     ptes: PteArray<{ Self::PTE_ARRAY_SIZE }>,
161     /// CPU queue: the driver writes commands here, and the GSP reads them. It also contains the
162     /// write and read pointers that the CPU updates.
163     ///
164     /// This member is read-only for the GSP.
165     pub(super) cpuq: Msgq,
166     /// GSP queue: the GSP writes messages here, and the driver reads them. It also contains the
167     /// write and read pointers that the GSP updates.
168     ///
169     /// This member is read-only for the driver.
170     pub(super) gspq: Msgq,
171 }
172 
173 impl GspMem {
174     const PTE_ARRAY_SIZE: usize = GSP_PAGE_SIZE / size_of::<u64>();
175 }
176 
177 // SAFETY: These structs don't meet the no-padding requirements of AsBytes but
178 // that is not a problem because they are not used outside the kernel.
179 unsafe impl AsBytes for GspMem {}
180 
181 // SAFETY: These structs don't meet the no-padding requirements of FromBytes but
182 // that is not a problem because they are not used outside the kernel.
183 unsafe impl FromBytes for GspMem {}
184 
185 /// Wrapper around [`GspMem`] to share it with the GPU using a [`CoherentAllocation`].
186 ///
187 /// This provides the low-level functionality to communicate with the GSP, including allocation of
188 /// queue space to write messages to and management of read/write pointers.
189 ///
190 /// This is shared with the GSP, with clear ownership rules regarding the command queues:
191 ///
192 /// * The driver owns (i.e. can write to) the part of the CPU message queue between the CPU write
193 ///   pointer and the GSP read pointer. This region is returned by [`Self::driver_write_area`].
194 /// * The driver owns (i.e. can read from) the part of the GSP message queue between the CPU read
195 ///   pointer and the GSP write pointer. This region is returned by [`Self::driver_read_area`].
196 struct DmaGspMem(CoherentAllocation<GspMem>);
197 
198 impl DmaGspMem {
199     /// Allocate a new instance and map it for `dev`.
200     fn new(dev: &device::Device<device::Bound>) -> Result<Self> {
201         const MSGQ_SIZE: u32 = num::usize_into_u32::<{ size_of::<Msgq>() }>();
202         const RX_HDR_OFF: u32 = num::usize_into_u32::<{ mem::offset_of!(Msgq, rx) }>();
203 
204         let gsp_mem =
205             CoherentAllocation::<GspMem>::alloc_coherent(dev, 1, GFP_KERNEL | __GFP_ZERO)?;
206 
207         let start = gsp_mem.dma_handle();
208         // Write values one by one to avoid an on-stack instance of `PteArray`.
209         for i in 0..GspMem::PTE_ARRAY_SIZE {
210             dma_write!(gsp_mem, [0]?.ptes.0[i], PteArray::<0>::entry(start, i)?);
211         }
212 
213         dma_write!(
214             gsp_mem,
215             [0]?.cpuq.tx,
216             MsgqTxHeader::new(MSGQ_SIZE, RX_HDR_OFF, MSGQ_NUM_PAGES)
217         );
218         dma_write!(gsp_mem, [0]?.cpuq.rx, MsgqRxHeader::new());
219 
220         Ok(Self(gsp_mem))
221     }
222 
223     /// Returns the region of the CPU message queue that the driver is currently allowed to write
224     /// to.
225     ///
226     /// As the message queue is a circular buffer, the region may be discontiguous in memory. In
227     /// that case the second slice will have a non-zero length.
228     fn driver_write_area(&mut self) -> (&mut [[u8; GSP_PAGE_SIZE]], &mut [[u8; GSP_PAGE_SIZE]]) {
229         let tx = self.cpu_write_ptr() as usize;
230         let rx = self.gsp_read_ptr() as usize;
231 
232         // SAFETY:
233         // - The `CoherentAllocation` contains exactly one object.
234         // - We will only access the driver-owned part of the shared memory.
235         // - Per the safety statement of the function, no concurrent access will be performed.
236         let gsp_mem = &mut unsafe { self.0.as_slice_mut(0, 1) }.unwrap()[0];
237         // PANIC: per the invariant of `cpu_write_ptr`, `tx` is `<= MSGQ_NUM_PAGES`.
238         let (before_tx, after_tx) = gsp_mem.cpuq.msgq.data.split_at_mut(tx);
239 
240         if rx <= tx {
241             // The area from `tx` up to the end of the ring, and from the beginning of the ring up
242             // to `rx`, minus one unit, belongs to the driver.
243             if rx == 0 {
244                 let last = after_tx.len() - 1;
245                 (&mut after_tx[..last], &mut before_tx[0..0])
246             } else {
247                 (after_tx, &mut before_tx[..rx])
248             }
249         } else {
250             // The area from `tx` to `rx`, minus one unit, belongs to the driver.
251             //
252             // PANIC: per the invariants of `cpu_write_ptr` and `gsp_read_ptr`, `rx` and `tx` are
253             // `<= MSGQ_NUM_PAGES`, and the test above ensured that `rx > tx`.
254             (after_tx.split_at_mut(rx - tx).0, &mut before_tx[0..0])
255         }
256     }
257 
258     /// Returns the region of the GSP message queue that the driver is currently allowed to read
259     /// from.
260     ///
261     /// As the message queue is a circular buffer, the region may be discontiguous in memory. In
262     /// that case the second slice will have a non-zero length.
263     fn driver_read_area(&self) -> (&[[u8; GSP_PAGE_SIZE]], &[[u8; GSP_PAGE_SIZE]]) {
264         let tx = self.gsp_write_ptr() as usize;
265         let rx = self.cpu_read_ptr() as usize;
266 
267         // SAFETY:
268         // - The `CoherentAllocation` contains exactly one object.
269         // - We will only access the driver-owned part of the shared memory.
270         // - Per the safety statement of the function, no concurrent access will be performed.
271         let gsp_mem = &unsafe { self.0.as_slice(0, 1) }.unwrap()[0];
272         // PANIC: per the invariant of `cpu_read_ptr`, `xx` is `<= MSGQ_NUM_PAGES`.
273         let (before_rx, after_rx) = gsp_mem.gspq.msgq.data.split_at(rx);
274 
275         match tx.cmp(&rx) {
276             cmp::Ordering::Equal => (&after_rx[0..0], &after_rx[0..0]),
277             cmp::Ordering::Greater => (&after_rx[..tx], &before_rx[0..0]),
278             cmp::Ordering::Less => (after_rx, &before_rx[..tx]),
279         }
280     }
281 
282     /// Allocates a region on the command queue that is large enough to send a command of `size`
283     /// bytes.
284     ///
285     /// This returns a [`GspCommand`] ready to be written to by the caller.
286     ///
287     /// # Errors
288     ///
289     /// - `EAGAIN` if the driver area is too small to hold the requested command.
290     /// - `EIO` if the command header is not properly aligned.
291     fn allocate_command(&mut self, size: usize) -> Result<GspCommand<'_>> {
292         // Get the current writable area as an array of bytes.
293         let (slice_1, slice_2) = {
294             let (slice_1, slice_2) = self.driver_write_area();
295 
296             (slice_1.as_flattened_mut(), slice_2.as_flattened_mut())
297         };
298 
299         // If the GSP is still processing previous messages the shared region
300         // may be full in which case we will have to retry once the GSP has
301         // processed the existing commands.
302         if size_of::<GspMsgElement>() + size > slice_1.len() + slice_2.len() {
303             return Err(EAGAIN);
304         }
305 
306         // Extract area for the `GspMsgElement`.
307         let (header, slice_1) = GspMsgElement::from_bytes_mut_prefix(slice_1).ok_or(EIO)?;
308 
309         // Create the contents area.
310         let (slice_1, slice_2) = if slice_1.len() > size {
311             // Contents fits entirely in `slice_1`.
312             (&mut slice_1[..size], &mut slice_2[0..0])
313         } else {
314             // Need all of `slice_1` and some of `slice_2`.
315             let slice_2_len = size - slice_1.len();
316             (slice_1, &mut slice_2[..slice_2_len])
317         };
318 
319         Ok(GspCommand {
320             header,
321             contents: (slice_1, slice_2),
322         })
323     }
324 
325     // Returns the index of the memory page the GSP will write the next message to.
326     //
327     // # Invariants
328     //
329     // - The returned value is between `0` and `MSGQ_NUM_PAGES`.
330     fn gsp_write_ptr(&self) -> u32 {
331         super::fw::gsp_mem::gsp_write_ptr(&self.0)
332     }
333 
334     // Returns the index of the memory page the GSP will read the next command from.
335     //
336     // # Invariants
337     //
338     // - The returned value is between `0` and `MSGQ_NUM_PAGES`.
339     fn gsp_read_ptr(&self) -> u32 {
340         super::fw::gsp_mem::gsp_read_ptr(&self.0)
341     }
342 
343     // Returns the index of the memory page the CPU can read the next message from.
344     //
345     // # Invariants
346     //
347     // - The returned value is between `0` and `MSGQ_NUM_PAGES`.
348     fn cpu_read_ptr(&self) -> u32 {
349         super::fw::gsp_mem::cpu_read_ptr(&self.0)
350     }
351 
352     // Informs the GSP that it can send `elem_count` new pages into the message queue.
353     fn advance_cpu_read_ptr(&mut self, elem_count: u32) {
354         super::fw::gsp_mem::advance_cpu_read_ptr(&self.0, elem_count)
355     }
356 
357     // Returns the index of the memory page the CPU can write the next command to.
358     //
359     // # Invariants
360     //
361     // - The returned value is between `0` and `MSGQ_NUM_PAGES`.
362     fn cpu_write_ptr(&self) -> u32 {
363         super::fw::gsp_mem::cpu_write_ptr(&self.0)
364     }
365 
366     // Informs the GSP that it can process `elem_count` new pages from the command queue.
367     fn advance_cpu_write_ptr(&mut self, elem_count: u32) {
368         super::fw::gsp_mem::advance_cpu_write_ptr(&self.0, elem_count)
369     }
370 }
371 
372 /// A command ready to be sent on the command queue.
373 ///
374 /// This is the type returned by [`DmaGspMem::allocate_command`].
375 struct GspCommand<'a> {
376     // Writable reference to the header of the command.
377     header: &'a mut GspMsgElement,
378     // Writable slices to the contents of the command. The second slice is zero unless the command
379     // loops over the command queue.
380     contents: (&'a mut [u8], &'a mut [u8]),
381 }
382 
383 /// A message ready to be processed from the message queue.
384 ///
385 /// This is the type returned by [`Cmdq::wait_for_msg`].
386 struct GspMessage<'a> {
387     // Reference to the header of the message.
388     header: &'a GspMsgElement,
389     // Slices to the contents of the message. The second slice is zero unless the message loops
390     // over the message queue.
391     contents: (&'a [u8], &'a [u8]),
392 }
393 
394 /// GSP command queue.
395 ///
396 /// Provides the ability to send commands and receive messages from the GSP using a shared memory
397 /// area.
398 pub(crate) struct Cmdq {
399     /// Device this command queue belongs to.
400     dev: ARef<device::Device>,
401     /// Current command sequence number.
402     seq: u32,
403     /// Memory area shared with the GSP for communicating commands and messages.
404     gsp_mem: DmaGspMem,
405 }
406 
407 impl Cmdq {
408     /// Offset of the data after the PTEs.
409     const POST_PTE_OFFSET: usize = core::mem::offset_of!(GspMem, cpuq);
410 
411     /// Offset of command queue ring buffer.
412     pub(crate) const CMDQ_OFFSET: usize = core::mem::offset_of!(GspMem, cpuq)
413         + core::mem::offset_of!(Msgq, msgq)
414         - Self::POST_PTE_OFFSET;
415 
416     /// Offset of message queue ring buffer.
417     pub(crate) const STATQ_OFFSET: usize = core::mem::offset_of!(GspMem, gspq)
418         + core::mem::offset_of!(Msgq, msgq)
419         - Self::POST_PTE_OFFSET;
420 
421     /// Number of page table entries for the GSP shared region.
422     pub(crate) const NUM_PTES: usize = size_of::<GspMem>() >> GSP_PAGE_SHIFT;
423 
424     /// Creates a new command queue for `dev`.
425     pub(crate) fn new(dev: &device::Device<device::Bound>) -> Result<Cmdq> {
426         let gsp_mem = DmaGspMem::new(dev)?;
427 
428         Ok(Cmdq {
429             dev: dev.into(),
430             seq: 0,
431             gsp_mem,
432         })
433     }
434 
435     /// Computes the checksum for the message pointed to by `it`.
436     ///
437     /// A message is made of several parts, so `it` is an iterator over byte slices representing
438     /// these parts.
439     fn calculate_checksum<T: Iterator<Item = u8>>(it: T) -> u32 {
440         let sum64 = it
441             .enumerate()
442             .map(|(idx, byte)| (((idx % 8) * 8) as u32, byte))
443             .fold(0, |acc, (rol, byte)| acc ^ u64::from(byte).rotate_left(rol));
444 
445         ((sum64 >> 32) as u32) ^ (sum64 as u32)
446     }
447 
448     /// Notifies the GSP that we have updated the command queue pointers.
449     fn notify_gsp(bar: &Bar0) {
450         regs::NV_PGSP_QUEUE_HEAD::default()
451             .set_address(0)
452             .write(bar);
453     }
454 
455     /// Sends `command` to the GSP.
456     ///
457     /// # Errors
458     ///
459     /// - `EAGAIN` if there was not enough space in the command queue to send the command.
460     /// - `EIO` if the variable payload requested by the command has not been entirely
461     ///   written to by its [`CommandToGsp::init_variable_payload`] method.
462     ///
463     /// Error codes returned by the command initializers are propagated as-is.
464     pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> Result
465     where
466         M: CommandToGsp,
467         // This allows all error types, including `Infallible`, to be used for `M::InitError`.
468         Error: From<M::InitError>,
469     {
470         let command_size = size_of::<M::Command>() + command.variable_payload_len();
471         let dst = self.gsp_mem.allocate_command(command_size)?;
472 
473         // Extract area for the command itself.
474         let (cmd, payload_1) = M::Command::from_bytes_mut_prefix(dst.contents.0).ok_or(EIO)?;
475 
476         // Fill the header and command in-place.
477         let msg_element = GspMsgElement::init(self.seq, command_size, M::FUNCTION);
478         // SAFETY: `msg_header` and `cmd` are valid references, and not touched if the initializer
479         // fails.
480         unsafe {
481             msg_element.__init(core::ptr::from_mut(dst.header))?;
482             command.init().__init(core::ptr::from_mut(cmd))?;
483         }
484 
485         // Fill the variable-length payload.
486         if command_size > size_of::<M::Command>() {
487             let mut sbuffer =
488                 SBufferIter::new_writer([&mut payload_1[..], &mut dst.contents.1[..]]);
489             command.init_variable_payload(&mut sbuffer)?;
490 
491             if !sbuffer.is_empty() {
492                 return Err(EIO);
493             }
494         }
495 
496         // Compute checksum now that the whole message is ready.
497         dst.header
498             .set_checksum(Cmdq::calculate_checksum(SBufferIter::new_reader([
499                 dst.header.as_bytes(),
500                 dst.contents.0,
501                 dst.contents.1,
502             ])));
503 
504         dev_dbg!(
505             &self.dev,
506             "GSP RPC: send: seq# {}, function={}, length=0x{:x}\n",
507             self.seq,
508             M::FUNCTION,
509             dst.header.length(),
510         );
511 
512         // All set - update the write pointer and inform the GSP of the new command.
513         let elem_count = dst.header.element_count();
514         self.seq += 1;
515         self.gsp_mem.advance_cpu_write_ptr(elem_count);
516         Cmdq::notify_gsp(bar);
517 
518         Ok(())
519     }
520 
521     /// Wait for a message to become available on the message queue.
522     ///
523     /// This works purely at the transport layer and does not interpret or validate the message
524     /// beyond the advertised length in its [`GspMsgElement`].
525     ///
526     /// This method returns:
527     ///
528     /// - A reference to the [`GspMsgElement`] of the message,
529     /// - Two byte slices with the contents of the message. The second slice is empty unless the
530     ///   message loops across the message queue.
531     ///
532     /// # Errors
533     ///
534     /// - `ETIMEDOUT` if `timeout` has elapsed before any message becomes available.
535     /// - `EIO` if there was some inconsistency (e.g. message shorter than advertised) on the
536     ///   message queue.
537     ///
538     /// Error codes returned by the message constructor are propagated as-is.
539     fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
540         // Wait for a message to arrive from the GSP.
541         let (slice_1, slice_2) = read_poll_timeout(
542             || Ok(self.gsp_mem.driver_read_area()),
543             |driver_area| !driver_area.0.is_empty(),
544             Delta::from_millis(1),
545             timeout,
546         )
547         .map(|(slice_1, slice_2)| (slice_1.as_flattened(), slice_2.as_flattened()))?;
548 
549         // Extract the `GspMsgElement`.
550         let (header, slice_1) = GspMsgElement::from_bytes_prefix(slice_1).ok_or(EIO)?;
551 
552         dev_dbg!(
553             self.dev,
554             "GSP RPC: receive: seq# {}, function={:?}, length=0x{:x}\n",
555             header.sequence(),
556             header.function(),
557             header.length(),
558         );
559 
560         let payload_length = header.payload_length();
561 
562         // Check that the driver read area is large enough for the message.
563         if slice_1.len() + slice_2.len() < payload_length {
564             return Err(EIO);
565         }
566 
567         // Cut the message slices down to the actual length of the message.
568         let (slice_1, slice_2) = if slice_1.len() > payload_length {
569             // PANIC: we checked above that `slice_1` is at least as long as `payload_length`.
570             (slice_1.split_at(payload_length).0, &slice_2[0..0])
571         } else {
572             (
573                 slice_1,
574                 // PANIC: we checked above that `slice_1.len() + slice_2.len()` is at least as
575                 // large as `payload_length`.
576                 slice_2.split_at(payload_length - slice_1.len()).0,
577             )
578         };
579 
580         // Validate checksum.
581         if Cmdq::calculate_checksum(SBufferIter::new_reader([
582             header.as_bytes(),
583             slice_1,
584             slice_2,
585         ])) != 0
586         {
587             dev_err!(
588                 self.dev,
589                 "GSP RPC: receive: Call {} - bad checksum\n",
590                 header.sequence()
591             );
592             return Err(EIO);
593         }
594 
595         Ok(GspMessage {
596             header,
597             contents: (slice_1, slice_2),
598         })
599     }
600 
601     /// Receive a message from the GSP.
602     ///
603     /// `init` is a closure tasked with processing the message. It receives a reference to the
604     /// message in the message queue, and a [`SBufferIter`] pointing to its variable-length
605     /// payload, if any.
606     ///
607     /// The expected message is specified using the `M` generic parameter. If the pending message
608     /// is different, `EAGAIN` is returned and the unexpected message is dropped.
609     ///
610     /// This design is by no means final, but it is simple and will let us go through GSP
611     /// initialization.
612     ///
613     /// # Errors
614     ///
615     /// - `ETIMEDOUT` if `timeout` has elapsed before any message becomes available.
616     /// - `EIO` if there was some inconsistency (e.g. message shorter than advertised) on the
617     ///   message queue.
618     /// - `EINVAL` if the function of the message was unrecognized.
619     pub(crate) fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
620     where
621         // This allows all error types, including `Infallible`, to be used for `M::InitError`.
622         Error: From<M::InitError>,
623     {
624         let message = self.wait_for_msg(timeout)?;
625         let function = message.header.function().map_err(|_| EINVAL)?;
626 
627         // Extract the message. Store the result as we want to advance the read pointer even in
628         // case of failure.
629         let result = if function == M::FUNCTION {
630             let (cmd, contents_1) = M::Message::from_bytes_prefix(message.contents.0).ok_or(EIO)?;
631             let mut sbuffer = SBufferIter::new_reader([contents_1, message.contents.1]);
632 
633             M::read(cmd, &mut sbuffer).map_err(|e| e.into())
634         } else {
635             Err(ERANGE)
636         };
637 
638         // Advance the read pointer past this message.
639         self.gsp_mem.advance_cpu_read_ptr(u32::try_from(
640             message.header.length().div_ceil(GSP_PAGE_SIZE),
641         )?);
642 
643         result
644     }
645 
646     /// Returns the DMA handle of the command queue's shared memory region.
647     pub(crate) fn dma_handle(&self) -> DmaAddress {
648         self.gsp_mem.0.dma_handle()
649     }
650 }
651