1 // SPDX-License-Identifier: GPL-2.0 2 // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 3 4 use kernel::{ 5 bits, 6 io::poll::read_poll_timeout, 7 prelude::*, 8 time::Delta, 9 types::ScopeGuard, // 10 }; 11 12 use crate::{ 13 falcon::{ 14 gsp::Gsp, 15 Falcon, // 16 }, 17 firmware::gsp::GspFirmware, 18 gsp::{ 19 cmdq::Cmdq, 20 commands, // 21 }, 22 }; 23 24 impl<'gsp> super::Gsp<'gsp> { 25 /// Attempt to boot the GSP. 26 /// 27 /// This is a GPU-dependent and complex procedure that involves loading firmware files from 28 /// user-space, patching them with signatures, and building firmware-specific intricate data 29 /// structures that the GSP will use at runtime. 30 /// 31 /// Upon return, the GSP is up and running, and its unload bundle (to be given as argument to 32 /// [`Self::unload`]) returned. 33 pub(crate) fn boot( 34 self: Pin<&mut Self>, 35 mut ctx: super::GspBootContext<'_, 'gsp>, 36 ) -> Result<Option<super::UnloadBundle<'gsp>>> { 37 let pdev = ctx.pdev; 38 let chipset = ctx.chipset; 39 let gsp_falcon = ctx.gsp_falcon; 40 let dev = pdev.as_ref(); 41 let hal = super::hal::gsp_hal(chipset); 42 43 let gsp_fw = KBox::pin_init(GspFirmware::new(dev, chipset), GFP_KERNEL)?; 44 45 self.cmdq 46 .send_command_no_wait(commands::SetSystemInfo::new(pdev, chipset))?; 47 self.cmdq 48 .send_command_no_wait(commands::SetRegistry::new(ctx.vgpu.state())?)?; 49 50 // Perform the chipset-specific boot sequence, and retrieve the unload bundle. 51 let unload_bundle = hal.boot(&self, &mut ctx, &gsp_fw)?.or_else(|| { 52 dev_warn!(dev, "The GSP won't be able to unload properly on unbind.\n"); 53 dev_warn!( 54 dev, 55 "The GPU will need to be reset before the driver can bind again.\n" 56 ); 57 58 None 59 }); 60 61 let mut unload_guard = 62 ScopeGuard::new_with_data((ctx, unload_bundle), |(ctx, unload_bundle)| { 63 let _ = self.unload(ctx, unload_bundle); 64 }); 65 let ctx = &mut unload_guard.0; 66 67 gsp_falcon.write_os_version(gsp_fw.bootloader.app_version); 68 69 // Poll for RISC-V to become active before continuing. 70 read_poll_timeout( 71 || Ok(gsp_falcon.is_riscv_active()), 72 |val: &bool| *val, 73 Delta::from_millis(10), 74 Delta::from_secs(5), 75 )?; 76 77 dev_dbg!(pdev, "RISC-V active? {}\n", gsp_falcon.is_riscv_active(),); 78 79 hal.post_boot(&self, ctx, &gsp_fw)?; 80 81 // Wait until GSP is fully initialized. 82 commands::wait_gsp_init_done(&self.cmdq)?; 83 84 Ok(unload_guard.dismiss().1) 85 } 86 87 /// Shut down the GSP and wait until it is offline. 88 fn shutdown_gsp( 89 cmdq: &Cmdq<'_>, 90 gsp_falcon: &Falcon<'_, Gsp>, 91 mode: commands::PowerStateLevel, 92 ) -> Result { 93 // Command to shut the GSP down. 94 cmdq.send_command(commands::UnloadingGuestDriver::new(mode))?; 95 96 // Wait until GSP signals it is suspended. 97 const LIBOS_INTERRUPT_PROCESSOR_SUSPENDED: u32 = bits::bit_u32(31); 98 read_poll_timeout( 99 || Ok(gsp_falcon.read_mailbox0()), 100 |&mb0| mb0 & LIBOS_INTERRUPT_PROCESSOR_SUSPENDED != 0, 101 Delta::from_millis(10), 102 Delta::from_secs(5), 103 ) 104 .map(|_| ()) 105 } 106 107 /// Attempts to unload the GSP firmware. 108 /// 109 /// This stops all activity on the GSP. 110 pub(crate) fn unload( 111 &self, 112 mut ctx: super::GspBootContext<'_, '_>, 113 unload_bundle: Option<super::UnloadBundle<'_>>, 114 ) -> Result { 115 let dev = ctx.dev(); 116 117 // Shut down the GSP. Keep going even in case of error. 118 let mut res = Self::shutdown_gsp( 119 &self.cmdq, 120 ctx.gsp_falcon, 121 commands::PowerStateLevel::Level0, 122 ) 123 .inspect_err(|e| dev_err!(dev, "GSP shutdown failed: {:?}\n", e)); 124 125 // Run the unload bundle to reset the GSP so it can be booted again. 126 if let Some(unload_bundle) = unload_bundle { 127 res = res.and( 128 unload_bundle 129 .0 130 .run(&mut ctx) 131 .inspect_err(|e| dev_err!(dev, "Unload bundle failed: {:?}\n", e)), 132 ); 133 } else { 134 dev_warn!( 135 dev, 136 "Unload bundle is missing, GSP won't be properly reset.\n" 137 ); 138 139 res = Err(EAGAIN); 140 } 141 142 res.inspect(|()| dev_info!(dev, "GSP successfully unloaded\n")) 143 } 144 } 145