xref: /linux/drivers/gpu/nova-core/gsp.rs (revision 04b325fb3456df0ffa7d6a8ac5cb4f94b860b575)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 mod boot;
4 mod hal;
5 
6 use kernel::{
7     debugfs,
8     device,
9     dma::{
10         Coherent,
11         CoherentBox,
12         DmaAddress, //
13     },
14     pci,
15     prelude::*,
16     transmute::{
17         AsBytes,
18         FromBytes, //
19     }, //
20 };
21 
22 pub(crate) mod cmdq;
23 pub(crate) mod commands;
24 mod fw;
25 mod sequencer;
26 
27 pub(crate) use fw::{
28     GspFwWprMeta,
29     LibosParams, //
30 };
31 
32 use crate::{
33     gsp::cmdq::Cmdq,
34     gsp::fw::{
35         GspArgumentsPadded,
36         LibosMemoryRegionInitArgument, //
37     },
38     num,
39 };
40 
41 pub(crate) const GSP_PAGE_SHIFT: usize = 12;
42 pub(crate) const GSP_PAGE_SIZE: usize = 1 << GSP_PAGE_SHIFT;
43 
44 /// Number of GSP pages to use in a RM log buffer.
45 const RM_LOG_BUFFER_NUM_PAGES: usize = 0x10;
46 const LOG_BUFFER_SIZE: usize = RM_LOG_BUFFER_NUM_PAGES * GSP_PAGE_SIZE;
47 
48 /// Array of page table entries, as understood by the GSP bootloader.
49 #[repr(C)]
50 struct PteArray<const NUM_ENTRIES: usize>([u64; NUM_ENTRIES]);
51 
52 /// SAFETY: arrays of `u64` implement `FromBytes` and we are but a wrapper around one.
53 unsafe impl<const NUM_ENTRIES: usize> FromBytes for PteArray<NUM_ENTRIES> {}
54 
55 /// SAFETY: arrays of `u64` implement `AsBytes` and we are but a wrapper around one.
56 unsafe impl<const NUM_ENTRIES: usize> AsBytes for PteArray<NUM_ENTRIES> {}
57 
58 impl<const NUM_PAGES: usize> PteArray<NUM_PAGES> {
59     /// Returns the page table entry for `index`, for a mapping starting at `start`.
60     // TODO: Replace with `IoView` projection once available.
61     fn entry(start: DmaAddress, index: usize) -> Result<u64> {
62         start
63             .checked_add(num::usize_as_u64(index) << GSP_PAGE_SHIFT)
64             .ok_or(EOVERFLOW)
65     }
66 }
67 
68 /// The logging buffers are byte queues that contain encoded printf-like
69 /// messages from GSP-RM.  They need to be decoded by a special application
70 /// that can parse the buffers.
71 ///
72 /// The 'loginit' buffer contains logs from early GSP-RM init and
73 /// exception dumps.  The 'logrm' buffer contains the subsequent logs. Both are
74 /// written to directly by GSP-RM and can be any multiple of GSP_PAGE_SIZE.
75 ///
76 /// The physical address map for the log buffer is stored in the buffer
77 /// itself, starting with offset 1. Offset 0 contains the "put" pointer (pp).
78 /// Initially, pp is equal to 0. If the buffer has valid logging data in it,
79 /// then pp points to index into the buffer where the next logging entry will
80 /// be written. Therefore, the logging data is valid if:
81 ///   1 <= pp < sizeof(buffer)/sizeof(u64)
82 struct LogBuffer(Coherent<[u8; LOG_BUFFER_SIZE]>);
83 
84 impl LogBuffer {
85     /// Creates a new `LogBuffer` mapped on `dev`.
86     fn new(dev: &device::Device<device::Bound>) -> Result<Self> {
87         let obj = Self(Coherent::zeroed(dev, GFP_KERNEL)?);
88 
89         let start_addr = obj.0.dma_handle();
90 
91         // SAFETY: `obj` has just been created and we are its sole user.
92         let pte_region = unsafe {
93             &mut obj.0.as_mut()[size_of::<u64>()..][..RM_LOG_BUFFER_NUM_PAGES * size_of::<u64>()]
94         };
95 
96         // Write values one by one to avoid an on-stack instance of `PteArray`.
97         for (i, chunk) in pte_region.chunks_exact_mut(size_of::<u64>()).enumerate() {
98             let pte_value = PteArray::<0>::entry(start_addr, i)?;
99 
100             chunk.copy_from_slice(&pte_value.to_ne_bytes());
101         }
102 
103         Ok(obj)
104     }
105 }
106 
107 struct LogBuffers {
108     /// Init log buffer.
109     loginit: LogBuffer,
110     /// Interrupts log buffer.
111     logintr: LogBuffer,
112     /// RM log buffer.
113     logrm: LogBuffer,
114 }
115 
116 /// GSP runtime data.
117 #[pin_data]
118 pub(crate) struct Gsp {
119     /// Libos arguments.
120     pub(crate) libos: Coherent<[LibosMemoryRegionInitArgument]>,
121     /// Log buffers, optionally exposed via debugfs.
122     #[pin]
123     logs: debugfs::Scope<LogBuffers>,
124     /// Command queue.
125     #[pin]
126     pub(crate) cmdq: Cmdq,
127     /// RM arguments.
128     rmargs: Coherent<GspArgumentsPadded>,
129 }
130 
131 impl Gsp {
132     // Creates an in-place initializer for a `Gsp` manager for `pdev`.
133     pub(crate) fn new(pdev: &pci::Device<device::Bound>) -> impl PinInit<Self, Error> + '_ {
134         pin_init::pin_init_scope(move || {
135             let dev = pdev.as_ref();
136 
137             let loginit = LogBuffer::new(dev)?;
138             let logintr = LogBuffer::new(dev)?;
139             let logrm = LogBuffer::new(dev)?;
140 
141             // Initialise the logging structures. The OpenRM equivalents are in:
142             // _kgspInitLibosLoggingStructures (allocates memory for buffers)
143             // kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array)
144             Ok(try_pin_init!(Self {
145                 cmdq <- Cmdq::new(dev),
146                 rmargs: Coherent::init(dev, GFP_KERNEL, GspArgumentsPadded::new(&cmdq))?,
147                 libos: {
148                     let mut libos = CoherentBox::zeroed_slice(
149                         dev,
150                         GSP_PAGE_SIZE / size_of::<LibosMemoryRegionInitArgument>(),
151                         GFP_KERNEL,
152                     )?;
153 
154                     libos.init_at(0, LibosMemoryRegionInitArgument::new("LOGINIT", &loginit.0))?;
155                     libos.init_at(1, LibosMemoryRegionInitArgument::new("LOGINTR", &logintr.0))?;
156                     libos.init_at(2, LibosMemoryRegionInitArgument::new("LOGRM", &logrm.0))?;
157                     libos.init_at(3, LibosMemoryRegionInitArgument::new("RMARGS", rmargs))?;
158 
159                     libos.into()
160                 },
161                 logs <- {
162                     let log_buffers = LogBuffers {
163                         loginit,
164                         logintr,
165                         logrm,
166                     };
167 
168                     #[allow(static_mut_refs)]
169                     // SAFETY: `DEBUGFS_ROOT` is created before driver registration and cleared
170                     // after driver unregistration, so no probe() can race with its modification.
171                     //
172                     // PANIC: `DEBUGFS_ROOT` cannot be `None` here.  It is set before driver
173                     // registration and cleared after driver unregistration, so it is always
174                     // `Some` for the entire lifetime that probe() can be called.
175                     let log_parent: &debugfs::Dir = unsafe { crate::DEBUGFS_ROOT.as_ref() }
176                         .expect("DEBUGFS_ROOT not initialized");
177 
178                     log_parent.scope(log_buffers, dev.name(), |logs, dir| {
179                         dir.read_binary_file(c"loginit", &logs.loginit.0);
180                         dir.read_binary_file(c"logintr", &logs.logintr.0);
181                         dir.read_binary_file(c"logrm", &logs.logrm.0);
182                     })
183                 },
184             }))
185         })
186     }
187 }
188 
189 /// Opaque bundle required to unload the GSP. Created by [`Gsp::boot`], consumed by [`Gsp::unload`].
190 pub(crate) struct UnloadBundle(KBox<dyn hal::UnloadBundle>);
191