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