1 // SPDX-License-Identifier: GPL-2.0 2 // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 3 4 use core::{ 5 array, 6 convert::Infallible, 7 ffi::FromBytesUntilNulError, 8 ops::Range, 9 str::Utf8Error, // 10 }; 11 12 use kernel::{ 13 device, 14 pci, 15 prelude::*, 16 transmute::{ 17 AsBytes, 18 FromBytes, // 19 }, // 20 }; 21 22 use crate::{ 23 gpu::Chipset, 24 gsp::{ 25 cmdq::{ 26 Cmdq, 27 CommandToGsp, 28 MessageFromGsp, 29 NoReply, // 30 }, 31 fw::{ 32 self, 33 MsgFunction, // 34 }, 35 }, 36 sbuffer::SBufferIter, 37 vgpu::VgpuState, // 38 }; 39 40 /// The `GspSetSystemInfo` command. 41 pub(crate) struct SetSystemInfo<'a> { 42 pdev: &'a pci::Device<device::Bound>, 43 chipset: Chipset, 44 } 45 46 impl<'a> SetSystemInfo<'a> { 47 /// Creates a new `GspSetSystemInfo` command using the parameters of `pdev`. 48 pub(crate) fn new(pdev: &'a pci::Device<device::Bound>, chipset: Chipset) -> Self { 49 Self { pdev, chipset } 50 } 51 } 52 53 impl<'a> CommandToGsp for SetSystemInfo<'a> { 54 const FUNCTION: MsgFunction = MsgFunction::GspSetSystemInfo; 55 type Command = fw::commands::GspSetSystemInfo; 56 type Reply = NoReply; 57 type InitError = Error; 58 59 fn init(&self) -> impl Init<Self::Command, Self::InitError> { 60 Self::Command::init(self.pdev, self.chipset) 61 } 62 } 63 64 struct RegistryEntry { 65 key: &'static str, 66 value: u32, 67 } 68 69 /// The `SetRegistry` command. 70 pub(crate) struct SetRegistry { 71 entries: KVec<RegistryEntry>, 72 } 73 74 impl SetRegistry { 75 /// Creates a new `SetRegistry` command, using a set of hardcoded entries. 76 pub(crate) fn new(vgpu_state: VgpuState) -> Result<Self> { 77 let mut entries = KVec::new(); 78 79 // RMSecBusResetEnable - enables PCI secondary bus reset 80 entries.push( 81 RegistryEntry { 82 key: "RMSecBusResetEnable", 83 value: 1, 84 }, 85 GFP_KERNEL, 86 )?; 87 88 // RMForcePcieConfigSave - forces GSP-RM to preserve PCI configuration registers on 89 // any PCI reset. 90 entries.push( 91 RegistryEntry { 92 key: "RMForcePcieConfigSave", 93 value: 1, 94 }, 95 GFP_KERNEL, 96 )?; 97 98 // RMDevidCheckIgnore - allows GSP-RM to boot even if the PCI dev ID is not found 99 // in the internal product name database. 100 entries.push( 101 RegistryEntry { 102 key: "RMDevidCheckIgnore", 103 value: 1, 104 }, 105 GFP_KERNEL, 106 )?; 107 108 if matches!(vgpu_state, VgpuState::Enabled { .. }) { 109 // RMSetSriovMode - required when vGPU is enabled. 110 entries.push( 111 RegistryEntry { 112 key: "RMSetSriovMode", 113 value: 1, 114 }, 115 GFP_KERNEL, 116 )?; 117 } 118 119 Ok(Self { entries }) 120 } 121 } 122 123 impl CommandToGsp for SetRegistry { 124 const FUNCTION: MsgFunction = MsgFunction::SetRegistry; 125 type Command = fw::commands::PackedRegistryTable; 126 type Reply = NoReply; 127 type InitError = Infallible; 128 129 fn init(&self) -> impl Init<Self::Command, Self::InitError> { 130 Self::Command::init(self.entries.len() as u32, self.size() as u32) 131 } 132 133 fn variable_payload_len(&self) -> usize { 134 let mut key_size = 0; 135 for entry in self.entries.iter() { 136 key_size += entry.key.len() + 1; // +1 for NULL terminator 137 } 138 self.entries.len() * size_of::<fw::commands::PackedRegistryEntry>() + key_size 139 } 140 141 fn init_variable_payload( 142 &self, 143 dst: &mut SBufferIter<core::array::IntoIter<&mut [u8], 2>>, 144 ) -> Result { 145 let string_data_start_offset = size_of::<Self::Command>() 146 + self.entries.len() * size_of::<fw::commands::PackedRegistryEntry>(); 147 148 // Array for string data. 149 let mut string_data = KVec::new(); 150 151 for entry in self.entries.iter() { 152 dst.write_all( 153 fw::commands::PackedRegistryEntry::new( 154 (string_data_start_offset + string_data.len()) as u32, 155 entry.value, 156 ) 157 .as_bytes(), 158 )?; 159 160 let key_bytes = entry.key.as_bytes(); 161 string_data.extend_from_slice(key_bytes, GFP_KERNEL)?; 162 string_data.push(0, GFP_KERNEL)?; 163 } 164 165 dst.write_all(string_data.as_slice()) 166 } 167 } 168 169 /// Message type for GSP initialization done notification. 170 struct GspInitDone; 171 172 // SAFETY: `GspInitDone` is a zero-sized type with no bytes, therefore it 173 // trivially has no uninitialized bytes. 174 unsafe impl FromBytes for GspInitDone {} 175 176 impl MessageFromGsp for GspInitDone { 177 const FUNCTION: MsgFunction = MsgFunction::GspInitDone; 178 type InitError = Infallible; 179 type Message = (); 180 181 fn read( 182 _msg: &Self::Message, 183 _sbuffer: &mut SBufferIter<array::IntoIter<&[u8], 2>>, 184 ) -> Result<Self, Self::InitError> { 185 Ok(GspInitDone) 186 } 187 } 188 189 /// Waits for GSP initialization to complete. 190 pub(crate) fn wait_gsp_init_done(cmdq: &Cmdq<'_>) -> Result { 191 cmdq.await_msg::<GspInitDone>().map(|_| ()) 192 } 193 194 /// The `GetGspStaticInfo` command. 195 pub(crate) struct GetGspStaticInfo; 196 197 impl CommandToGsp for GetGspStaticInfo { 198 const FUNCTION: MsgFunction = MsgFunction::GetGspStaticInfo; 199 type Command = fw::commands::GspStaticConfigInfo; 200 type Reply = GetGspStaticInfoReply; 201 type InitError = Infallible; 202 203 fn init(&self) -> impl Init<Self::Command, Self::InitError> { 204 Self::Command::init_zeroed() 205 } 206 } 207 208 /// The reply from the GSP to the [`GetGspStaticInfo`] command. 209 pub struct GetGspStaticInfoReply { 210 gpu_name: [u8; 64], 211 gpu_short_name: [u8; 64], 212 /// The 16-byte SHA-1 based GPU identifier (GID) reported by GSP-RM. 213 pub gpu_gid: [u8; 16], 214 /// BAR1 Page Directory Entry base address. 215 pub(crate) bar1_pde_base: u64, 216 /// Usable FB (VRAM) regions for driver memory allocation. 217 pub(crate) usable_fb_regions: KVec<Range<u64>>, 218 /// Exclusive end of the FB physical address space. 219 pub(crate) total_fb_end: u64, 220 } 221 222 impl MessageFromGsp for GetGspStaticInfoReply { 223 const FUNCTION: MsgFunction = MsgFunction::GetGspStaticInfo; 224 type Message = fw::commands::GspStaticConfigInfo; 225 type InitError = Error; 226 227 fn read( 228 msg: &Self::Message, 229 _sbuffer: &mut SBufferIter<array::IntoIter<&[u8], 2>>, 230 ) -> Result<Self, Self::InitError> { 231 let mut usable_fb_regions = KVec::new(); 232 for region in msg.usable_fb_regions() { 233 usable_fb_regions.push(region, GFP_KERNEL)?; 234 } 235 let total_fb_end = msg.total_fb_end().ok_or(EINVAL)?; 236 237 Ok(GetGspStaticInfoReply { 238 gpu_name: msg.gpu_name_str(), 239 gpu_short_name: msg.gpu_short_name_str(), 240 gpu_gid: msg.gpu_gid(), 241 bar1_pde_base: msg.bar1_pde_base(), 242 usable_fb_regions, 243 total_fb_end, 244 }) 245 } 246 } 247 248 /// Error type for [`GetGspStaticInfoReply::gpu_name`]. 249 #[derive(Debug)] 250 pub enum GpuNameError { 251 /// The GPU name string does not contain a null terminator. 252 NoNullTerminator(FromBytesUntilNulError), 253 254 /// The GPU name string contains invalid UTF-8. 255 InvalidUtf8(Utf8Error), 256 } 257 258 impl GetGspStaticInfoReply { 259 /// Returns the name of the GPU as a string. 260 /// 261 /// Returns an error if the string given by the GSP does not contain a null terminator or 262 /// contains invalid UTF-8. 263 pub fn gpu_name(&self) -> Result<&str, GpuNameError> { 264 CStr::from_bytes_until_nul(&self.gpu_name) 265 .map_err(GpuNameError::NoNullTerminator)? 266 .to_str() 267 .map_err(GpuNameError::InvalidUtf8) 268 } 269 270 /// Returns the short name of the GPU as a string. 271 /// 272 /// Returns an error if the string given by the GSP does not contain a null terminator or 273 /// contains invalid UTF-8. 274 pub fn gpu_short_name(&self) -> core::result::Result<&str, GpuNameError> { 275 CStr::from_bytes_until_nul(&self.gpu_short_name) 276 .map_err(GpuNameError::NoNullTerminator)? 277 .to_str() 278 .map_err(GpuNameError::InvalidUtf8) 279 } 280 281 /// Returns the total usable VRAM size in bytes, i.e. the summed lengths of all usable FB 282 /// regions. 283 pub fn vram_size(&self) -> u64 { 284 self.usable_fb_regions.iter().fold(0, |size, region| { 285 size.saturating_add(region.end - region.start) 286 }) 287 } 288 } 289 290 pub(crate) use fw::commands::PowerStateLevel; 291 292 /// The `UnloadingGuestDriver` command, used to shut down the GSP. 293 /// 294 /// Only used within the `gsp` module. 295 pub(super) struct UnloadingGuestDriver { 296 level: PowerStateLevel, 297 } 298 299 impl UnloadingGuestDriver { 300 /// Creates a new `UnloadingGuestDriver` command for the given [`PowerStateLevel`]. 301 pub(super) fn new(level: PowerStateLevel) -> Self { 302 Self { level } 303 } 304 } 305 306 impl CommandToGsp for UnloadingGuestDriver { 307 const FUNCTION: MsgFunction = MsgFunction::UnloadingGuestDriver; 308 type Command = fw::commands::UnloadingGuestDriver; 309 type Reply = UnloadingGuestDriverReply; 310 type InitError = Infallible; 311 312 fn init(&self) -> impl Init<Self::Command, Self::InitError> { 313 fw::commands::UnloadingGuestDriver::new(self.level) 314 } 315 } 316 317 /// The reply from the GSP to the [`UnloadingGuestDriver`] command. 318 pub(super) struct UnloadingGuestDriverReply; 319 320 impl MessageFromGsp for UnloadingGuestDriverReply { 321 const FUNCTION: MsgFunction = MsgFunction::UnloadingGuestDriver; 322 type InitError = Infallible; 323 type Message = (); 324 325 fn read( 326 _msg: &Self::Message, 327 _sbuffer: &mut SBufferIter<array::IntoIter<&[u8], 2>>, 328 ) -> Result<Self, Self::InitError> { 329 Ok(UnloadingGuestDriverReply) 330 } 331 } 332