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