gpu.rs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: GPL-2.0
  2. use kernel::{
  3. device,
  4. devres::Devres,
  5. fmt,
  6. pci,
  7. prelude::*,
  8. sync::Arc, //
  9. };
  10. use crate::{
  11. driver::Bar0,
  12. falcon::{
  13. gsp::Gsp as GspFalcon,
  14. sec2::Sec2 as Sec2Falcon,
  15. Falcon, //
  16. },
  17. fb::SysmemFlush,
  18. gfw,
  19. gsp::Gsp,
  20. regs,
  21. };
  22. macro_rules! define_chipset {
  23. ({ $($variant:ident = $value:expr),* $(,)* }) =>
  24. {
  25. /// Enum representation of the GPU chipset.
  26. #[derive(fmt::Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
  27. pub(crate) enum Chipset {
  28. $($variant = $value),*,
  29. }
  30. impl Chipset {
  31. pub(crate) const ALL: &'static [Chipset] = &[
  32. $( Chipset::$variant, )*
  33. ];
  34. ::kernel::macros::paste!(
  35. /// Returns the name of this chipset, in lowercase.
  36. ///
  37. /// # Examples
  38. ///
  39. /// ```
  40. /// let chipset = Chipset::GA102;
  41. /// assert_eq!(chipset.name(), "ga102");
  42. /// ```
  43. pub(crate) const fn name(&self) -> &'static str {
  44. match *self {
  45. $(
  46. Chipset::$variant => stringify!([<$variant:lower>]),
  47. )*
  48. }
  49. }
  50. );
  51. }
  52. // TODO[FPRI]: replace with something like derive(FromPrimitive)
  53. impl TryFrom<u32> for Chipset {
  54. type Error = kernel::error::Error;
  55. fn try_from(value: u32) -> Result<Self, Self::Error> {
  56. match value {
  57. $( $value => Ok(Chipset::$variant), )*
  58. _ => Err(ENODEV),
  59. }
  60. }
  61. }
  62. }
  63. }
  64. define_chipset!({
  65. // Turing
  66. TU102 = 0x162,
  67. TU104 = 0x164,
  68. TU106 = 0x166,
  69. TU117 = 0x167,
  70. TU116 = 0x168,
  71. // Ampere
  72. GA100 = 0x170,
  73. GA102 = 0x172,
  74. GA103 = 0x173,
  75. GA104 = 0x174,
  76. GA106 = 0x176,
  77. GA107 = 0x177,
  78. // Ada
  79. AD102 = 0x192,
  80. AD103 = 0x193,
  81. AD104 = 0x194,
  82. AD106 = 0x196,
  83. AD107 = 0x197,
  84. });
  85. impl Chipset {
  86. pub(crate) fn arch(&self) -> Architecture {
  87. match self {
  88. Self::TU102 | Self::TU104 | Self::TU106 | Self::TU117 | Self::TU116 => {
  89. Architecture::Turing
  90. }
  91. Self::GA100 | Self::GA102 | Self::GA103 | Self::GA104 | Self::GA106 | Self::GA107 => {
  92. Architecture::Ampere
  93. }
  94. Self::AD102 | Self::AD103 | Self::AD104 | Self::AD106 | Self::AD107 => {
  95. Architecture::Ada
  96. }
  97. }
  98. }
  99. }
  100. // TODO
  101. //
  102. // The resulting strings are used to generate firmware paths, hence the
  103. // generated strings have to be stable.
  104. //
  105. // Hence, replace with something like strum_macros derive(Display).
  106. //
  107. // For now, redirect to fmt::Debug for convenience.
  108. impl fmt::Display for Chipset {
  109. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  110. write!(f, "{self:?}")
  111. }
  112. }
  113. /// Enum representation of the GPU generation.
  114. ///
  115. /// TODO: remove the `Default` trait implementation, and the `#[default]`
  116. /// attribute, once the register!() macro (which creates Architecture items) no
  117. /// longer requires it for read-only fields.
  118. #[derive(fmt::Debug, Default, Copy, Clone)]
  119. #[repr(u8)]
  120. pub(crate) enum Architecture {
  121. #[default]
  122. Turing = 0x16,
  123. Ampere = 0x17,
  124. Ada = 0x19,
  125. }
  126. impl TryFrom<u8> for Architecture {
  127. type Error = Error;
  128. fn try_from(value: u8) -> Result<Self> {
  129. match value {
  130. 0x16 => Ok(Self::Turing),
  131. 0x17 => Ok(Self::Ampere),
  132. 0x19 => Ok(Self::Ada),
  133. _ => Err(ENODEV),
  134. }
  135. }
  136. }
  137. impl From<Architecture> for u8 {
  138. fn from(value: Architecture) -> Self {
  139. // CAST: `Architecture` is `repr(u8)`, so this cast is always lossless.
  140. value as u8
  141. }
  142. }
  143. pub(crate) struct Revision {
  144. major: u8,
  145. minor: u8,
  146. }
  147. impl From<regs::NV_PMC_BOOT_42> for Revision {
  148. fn from(boot0: regs::NV_PMC_BOOT_42) -> Self {
  149. Self {
  150. major: boot0.major_revision(),
  151. minor: boot0.minor_revision(),
  152. }
  153. }
  154. }
  155. impl fmt::Display for Revision {
  156. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  157. write!(f, "{:x}.{:x}", self.major, self.minor)
  158. }
  159. }
  160. /// Structure holding a basic description of the GPU: `Chipset` and `Revision`.
  161. pub(crate) struct Spec {
  162. chipset: Chipset,
  163. revision: Revision,
  164. }
  165. impl Spec {
  166. fn new(dev: &device::Device, bar: &Bar0) -> Result<Spec> {
  167. // Some brief notes about boot0 and boot42, in chronological order:
  168. //
  169. // NV04 through NV50:
  170. //
  171. // Not supported by Nova. boot0 is necessary and sufficient to identify these GPUs.
  172. // boot42 may not even exist on some of these GPUs.
  173. //
  174. // Fermi through Volta:
  175. //
  176. // Not supported by Nova. boot0 is still sufficient to identify these GPUs, but boot42
  177. // is also guaranteed to be both present and accurate.
  178. //
  179. // Turing and later:
  180. //
  181. // Supported by Nova. Identified by first checking boot0 to ensure that the GPU is not
  182. // from an earlier (pre-Fermi) era, and then using boot42 to precisely identify the GPU.
  183. // Somewhere in the Rubin timeframe, boot0 will no longer have space to add new GPU IDs.
  184. let boot0 = regs::NV_PMC_BOOT_0::read(bar);
  185. if boot0.is_older_than_fermi() {
  186. return Err(ENODEV);
  187. }
  188. let boot42 = regs::NV_PMC_BOOT_42::read(bar);
  189. Spec::try_from(boot42).inspect_err(|_| {
  190. dev_err!(dev, "Unsupported chipset: {}\n", boot42);
  191. })
  192. }
  193. }
  194. impl TryFrom<regs::NV_PMC_BOOT_42> for Spec {
  195. type Error = Error;
  196. fn try_from(boot42: regs::NV_PMC_BOOT_42) -> Result<Self> {
  197. Ok(Self {
  198. chipset: boot42.chipset()?,
  199. revision: boot42.into(),
  200. })
  201. }
  202. }
  203. impl fmt::Display for Spec {
  204. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  205. f.write_fmt(fmt!(
  206. "Chipset: {}, Architecture: {:?}, Revision: {}",
  207. self.chipset,
  208. self.chipset.arch(),
  209. self.revision
  210. ))
  211. }
  212. }
  213. /// Structure holding the resources required to operate the GPU.
  214. #[pin_data]
  215. pub(crate) struct Gpu {
  216. spec: Spec,
  217. /// MMIO mapping of PCI BAR 0
  218. bar: Arc<Devres<Bar0>>,
  219. /// System memory page required for flushing all pending GPU-side memory writes done through
  220. /// PCIE into system memory, via sysmembar (A GPU-initiated HW memory-barrier operation).
  221. sysmem_flush: SysmemFlush,
  222. /// GSP falcon instance, used for GSP boot up and cleanup.
  223. gsp_falcon: Falcon<GspFalcon>,
  224. /// SEC2 falcon instance, used for GSP boot up and cleanup.
  225. sec2_falcon: Falcon<Sec2Falcon>,
  226. /// GSP runtime data. Temporarily an empty placeholder.
  227. #[pin]
  228. gsp: Gsp,
  229. }
  230. impl Gpu {
  231. pub(crate) fn new<'a>(
  232. pdev: &'a pci::Device<device::Bound>,
  233. devres_bar: Arc<Devres<Bar0>>,
  234. bar: &'a Bar0,
  235. ) -> impl PinInit<Self, Error> + 'a {
  236. try_pin_init!(Self {
  237. spec: Spec::new(pdev.as_ref(), bar).inspect(|spec| {
  238. dev_info!(pdev.as_ref(),"NVIDIA ({})\n", spec);
  239. })?,
  240. // We must wait for GFW_BOOT completion before doing any significant setup on the GPU.
  241. _: {
  242. gfw::wait_gfw_boot_completion(bar)
  243. .inspect_err(|_| dev_err!(pdev.as_ref(), "GFW boot did not complete\n"))?;
  244. },
  245. sysmem_flush: SysmemFlush::register(pdev.as_ref(), bar, spec.chipset)?,
  246. gsp_falcon: Falcon::new(
  247. pdev.as_ref(),
  248. spec.chipset,
  249. )
  250. .inspect(|falcon| falcon.clear_swgen0_intr(bar))?,
  251. sec2_falcon: Falcon::new(pdev.as_ref(), spec.chipset)?,
  252. gsp <- Gsp::new(pdev),
  253. _: { gsp.boot(pdev, bar, spec.chipset, gsp_falcon, sec2_falcon)? },
  254. bar: devres_bar,
  255. })
  256. }
  257. /// Called when the corresponding [`Device`](device::Device) is unbound.
  258. ///
  259. /// Note: This method must only be called from `Driver::unbind`.
  260. pub(crate) fn unbind(&self, dev: &device::Device<device::Core>) {
  261. kernel::warn_on!(self
  262. .bar
  263. .access(dev)
  264. .inspect(|bar| self.sysmem_flush.unregister(bar))
  265. .is_err());
  266. }
  267. }