xref: /linux/drivers/gpu/nova-core/gfw.rs (revision 220994d61cebfc04f071d69049127657c7e8191b)
1bbe5db76SAlexandre Courbot // SPDX-License-Identifier: GPL-2.0
2bbe5db76SAlexandre Courbot 
3*4a4d4e32SJoel Fernandes //! GPU Firmware (`GFW`) support, a.k.a `devinit`.
4bbe5db76SAlexandre Courbot //!
5bbe5db76SAlexandre Courbot //! Upon reset, the GPU runs some firmware code from the BIOS to setup its core parameters. Most of
6bbe5db76SAlexandre Courbot //! the GPU is considered unusable until this step is completed, so we must wait on it before
7bbe5db76SAlexandre Courbot //! performing driver initialization.
8*4a4d4e32SJoel Fernandes //!
9*4a4d4e32SJoel Fernandes //! A clarification about devinit terminology: devinit is a sequence of register read/writes after
10*4a4d4e32SJoel Fernandes //! reset that performs tasks such as:
11*4a4d4e32SJoel Fernandes //! 1. Programming VRAM memory controller timings.
12*4a4d4e32SJoel Fernandes //! 2. Power sequencing.
13*4a4d4e32SJoel Fernandes //! 3. Clock and PLL configuration.
14*4a4d4e32SJoel Fernandes //! 4. Thermal management.
15*4a4d4e32SJoel Fernandes //!
16*4a4d4e32SJoel Fernandes //! devinit itself is a 'script' which is interpreted by an interpreter program typically running
17*4a4d4e32SJoel Fernandes //! on the PMU microcontroller.
18*4a4d4e32SJoel Fernandes //!
19*4a4d4e32SJoel Fernandes //! Note that the devinit sequence also needs to run during suspend/resume.
20bbe5db76SAlexandre Courbot 
21bbe5db76SAlexandre Courbot use kernel::bindings;
22bbe5db76SAlexandre Courbot use kernel::prelude::*;
234092e1b4SAlexandre Courbot use kernel::time::Delta;
24bbe5db76SAlexandre Courbot 
25bbe5db76SAlexandre Courbot use crate::driver::Bar0;
26bbe5db76SAlexandre Courbot use crate::regs;
27bbe5db76SAlexandre Courbot use crate::util;
28bbe5db76SAlexandre Courbot 
29*4a4d4e32SJoel Fernandes /// Wait for the `GFW` (GPU firmware) boot completion signal (`GFW_BOOT`), or a 4 seconds timeout.
30*4a4d4e32SJoel Fernandes ///
31*4a4d4e32SJoel Fernandes /// Upon GPU reset, several microcontrollers (such as PMU, SEC2, GSP etc) run some firmware code to
32*4a4d4e32SJoel Fernandes /// setup its core parameters. Most of the GPU is considered unusable until this step is completed,
33*4a4d4e32SJoel Fernandes /// so it must be waited on very early during driver initialization.
34*4a4d4e32SJoel Fernandes ///
35*4a4d4e32SJoel Fernandes /// The `GFW` code includes several components that need to execute before the driver loads. These
36*4a4d4e32SJoel Fernandes /// components are located in the VBIOS ROM and executed in a sequence on these different
37*4a4d4e32SJoel Fernandes /// microcontrollers. The devinit sequence typically runs on the PMU, and the FWSEC runs on the
38*4a4d4e32SJoel Fernandes /// GSP.
39*4a4d4e32SJoel Fernandes ///
40*4a4d4e32SJoel Fernandes /// This function waits for a signal indicating that core initialization is complete. Before this
41*4a4d4e32SJoel Fernandes /// signal is received, little can be done with the GPU. This signal is set by the FWSEC running on
42*4a4d4e32SJoel Fernandes /// the GSP in Heavy-secured mode.
wait_gfw_boot_completion(bar: &Bar0) -> Result43bbe5db76SAlexandre Courbot pub(crate) fn wait_gfw_boot_completion(bar: &Bar0) -> Result {
44*4a4d4e32SJoel Fernandes     // Before accessing the completion status in `NV_PGC6_AON_SECURE_SCRATCH_GROUP_05`, we must
45*4a4d4e32SJoel Fernandes     // first check `NV_PGC6_AON_SECURE_SCRATCH_GROUP_05_PRIV_LEVEL_MASK`. This is because
46*4a4d4e32SJoel Fernandes     // `NV_PGC6_AON_SECURE_SCRATCH_GROUP_05` becomes accessible only after the secure firmware
47*4a4d4e32SJoel Fernandes     // (FWSEC) lowers the privilege level to allow CPU (LS/Light-secured) access. We can only
48*4a4d4e32SJoel Fernandes     // safely read the status register from CPU (LS/Light-secured) once the mask indicates
49*4a4d4e32SJoel Fernandes     // that the privilege level has been lowered.
50*4a4d4e32SJoel Fernandes     //
51bbe5db76SAlexandre Courbot     // TIMEOUT: arbitrarily large value. GFW starts running immediately after the GPU is put out of
52bbe5db76SAlexandre Courbot     // reset, and should complete in less time than that.
534092e1b4SAlexandre Courbot     util::wait_on(Delta::from_secs(4), || {
54*4a4d4e32SJoel Fernandes         // Check that FWSEC has lowered its protection level before reading the GFW_BOOT status.
55bbe5db76SAlexandre Courbot         let gfw_booted = regs::NV_PGC6_AON_SECURE_SCRATCH_GROUP_05_PRIV_LEVEL_MASK::read(bar)
56bbe5db76SAlexandre Courbot             .read_protection_level0()
57bbe5db76SAlexandre Courbot             && regs::NV_PGC6_AON_SECURE_SCRATCH_GROUP_05_0_GFW_BOOT::read(bar).completed();
58bbe5db76SAlexandre Courbot 
59bbe5db76SAlexandre Courbot         if gfw_booted {
60bbe5db76SAlexandre Courbot             Some(())
61bbe5db76SAlexandre Courbot         } else {
623606620bSAlexandre Courbot             // TODO[DLAY]: replace with [1] once it merges.
63bbe5db76SAlexandre Courbot             // [1] https://lore.kernel.org/rust-for-linux/20250423192857.199712-6-fujita.tomonori@gmail.com/
64bbe5db76SAlexandre Courbot             //
65bbe5db76SAlexandre Courbot             // SAFETY: `msleep()` is safe to call with any parameter.
66bbe5db76SAlexandre Courbot             unsafe { bindings::msleep(1) };
67bbe5db76SAlexandre Courbot 
68bbe5db76SAlexandre Courbot             None
69bbe5db76SAlexandre Courbot         }
70bbe5db76SAlexandre Courbot     })
71bbe5db76SAlexandre Courbot }
72