xref: /linux/drivers/gpu/nova-core/gpu.rs (revision 80213934d00fe09d9dcef3d6f17250be131435aa)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use kernel::{device, devres::Devres, error::code::*, pci, prelude::*};
4 
5 use crate::driver::Bar0;
6 use crate::falcon::{gsp::Gsp, sec2::Sec2, Falcon};
7 use crate::fb::FbLayout;
8 use crate::fb::SysmemFlush;
9 use crate::firmware::{Firmware, FIRMWARE_VERSION};
10 use crate::gfw;
11 use crate::regs;
12 use crate::util;
13 use crate::vbios::Vbios;
14 use core::fmt;
15 
16 macro_rules! define_chipset {
17     ({ $($variant:ident = $value:expr),* $(,)* }) =>
18     {
19         /// Enum representation of the GPU chipset.
20         #[derive(fmt::Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
21         pub(crate) enum Chipset {
22             $($variant = $value),*,
23         }
24 
25         impl Chipset {
26             pub(crate) const ALL: &'static [Chipset] = &[
27                 $( Chipset::$variant, )*
28             ];
29 
30             pub(crate) const NAMES: [&'static str; Self::ALL.len()] = [
31                 $( util::const_bytes_to_str(
32                         util::to_lowercase_bytes::<{ stringify!($variant).len() }>(
33                             stringify!($variant)
34                         ).as_slice()
35                 ), )*
36             ];
37         }
38 
39         // TODO replace with something like derive(FromPrimitive)
40         impl TryFrom<u32> for Chipset {
41             type Error = kernel::error::Error;
42 
43             fn try_from(value: u32) -> Result<Self, Self::Error> {
44                 match value {
45                     $( $value => Ok(Chipset::$variant), )*
46                     _ => Err(ENODEV),
47                 }
48             }
49         }
50     }
51 }
52 
53 define_chipset!({
54     // Turing
55     TU102 = 0x162,
56     TU104 = 0x164,
57     TU106 = 0x166,
58     TU117 = 0x167,
59     TU116 = 0x168,
60     // Ampere
61     GA100 = 0x170,
62     GA102 = 0x172,
63     GA103 = 0x173,
64     GA104 = 0x174,
65     GA106 = 0x176,
66     GA107 = 0x177,
67     // Ada
68     AD102 = 0x192,
69     AD103 = 0x193,
70     AD104 = 0x194,
71     AD106 = 0x196,
72     AD107 = 0x197,
73 });
74 
75 impl Chipset {
76     pub(crate) fn arch(&self) -> Architecture {
77         match self {
78             Self::TU102 | Self::TU104 | Self::TU106 | Self::TU117 | Self::TU116 => {
79                 Architecture::Turing
80             }
81             Self::GA100 | Self::GA102 | Self::GA103 | Self::GA104 | Self::GA106 | Self::GA107 => {
82                 Architecture::Ampere
83             }
84             Self::AD102 | Self::AD103 | Self::AD104 | Self::AD106 | Self::AD107 => {
85                 Architecture::Ada
86             }
87         }
88     }
89 }
90 
91 // TODO
92 //
93 // The resulting strings are used to generate firmware paths, hence the
94 // generated strings have to be stable.
95 //
96 // Hence, replace with something like strum_macros derive(Display).
97 //
98 // For now, redirect to fmt::Debug for convenience.
99 impl fmt::Display for Chipset {
100     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101         write!(f, "{self:?}")
102     }
103 }
104 
105 /// Enum representation of the GPU generation.
106 #[derive(fmt::Debug)]
107 pub(crate) enum Architecture {
108     Turing = 0x16,
109     Ampere = 0x17,
110     Ada = 0x19,
111 }
112 
113 impl TryFrom<u8> for Architecture {
114     type Error = Error;
115 
116     fn try_from(value: u8) -> Result<Self> {
117         match value {
118             0x16 => Ok(Self::Turing),
119             0x17 => Ok(Self::Ampere),
120             0x19 => Ok(Self::Ada),
121             _ => Err(ENODEV),
122         }
123     }
124 }
125 
126 pub(crate) struct Revision {
127     major: u8,
128     minor: u8,
129 }
130 
131 impl Revision {
132     fn from_boot0(boot0: regs::NV_PMC_BOOT_0) -> Self {
133         Self {
134             major: boot0.major_revision(),
135             minor: boot0.minor_revision(),
136         }
137     }
138 }
139 
140 impl fmt::Display for Revision {
141     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142         write!(f, "{:x}.{:x}", self.major, self.minor)
143     }
144 }
145 
146 /// Structure holding the metadata of the GPU.
147 pub(crate) struct Spec {
148     chipset: Chipset,
149     /// The revision of the chipset.
150     revision: Revision,
151 }
152 
153 impl Spec {
154     fn new(bar: &Bar0) -> Result<Spec> {
155         let boot0 = regs::NV_PMC_BOOT_0::read(bar);
156 
157         Ok(Self {
158             chipset: boot0.chipset()?,
159             revision: Revision::from_boot0(boot0),
160         })
161     }
162 }
163 
164 /// Structure holding the resources required to operate the GPU.
165 #[pin_data(PinnedDrop)]
166 pub(crate) struct Gpu {
167     spec: Spec,
168     /// MMIO mapping of PCI BAR 0
169     bar: Devres<Bar0>,
170     fw: Firmware,
171     /// System memory page required for flushing all pending GPU-side memory writes done through
172     /// PCIE into system memory.
173     sysmem_flush: SysmemFlush,
174 }
175 
176 #[pinned_drop]
177 impl PinnedDrop for Gpu {
178     fn drop(self: Pin<&mut Self>) {
179         // Unregister the sysmem flush page before we release it.
180         self.bar
181             .try_access_with(|b| self.sysmem_flush.unregister(b));
182     }
183 }
184 
185 impl Gpu {
186     pub(crate) fn new(
187         pdev: &pci::Device<device::Bound>,
188         devres_bar: Devres<Bar0>,
189     ) -> Result<impl PinInit<Self>> {
190         let bar = devres_bar.access(pdev.as_ref())?;
191         let spec = Spec::new(bar)?;
192         let fw = Firmware::new(pdev.as_ref(), spec.chipset, FIRMWARE_VERSION)?;
193 
194         dev_info!(
195             pdev.as_ref(),
196             "NVIDIA (Chipset: {}, Architecture: {:?}, Revision: {})\n",
197             spec.chipset,
198             spec.chipset.arch(),
199             spec.revision
200         );
201 
202         // We must wait for GFW_BOOT completion before doing any significant setup on the GPU.
203         gfw::wait_gfw_boot_completion(bar)
204             .inspect_err(|_| dev_err!(pdev.as_ref(), "GFW boot did not complete"))?;
205 
206         // System memory page required for sysmembar to properly flush into system memory.
207         let sysmem_flush = SysmemFlush::register(pdev.as_ref(), bar, spec.chipset)?;
208 
209         let gsp_falcon = Falcon::<Gsp>::new(
210             pdev.as_ref(),
211             spec.chipset,
212             bar,
213             spec.chipset > Chipset::GA100,
214         )?;
215         gsp_falcon.clear_swgen0_intr(bar);
216 
217         let _sec2_falcon = Falcon::<Sec2>::new(pdev.as_ref(), spec.chipset, bar, true)?;
218 
219         let fb_layout = FbLayout::new(spec.chipset, bar)?;
220         dev_dbg!(pdev.as_ref(), "{:#x?}\n", fb_layout);
221 
222         // Will be used in a later patch when fwsec firmware is needed.
223         let _bios = Vbios::new(pdev, bar)?;
224 
225         Ok(pin_init!(Self {
226             spec,
227             bar: devres_bar,
228             fw,
229             sysmem_flush,
230         }))
231     }
232 }
233