xref: /linux/drivers/gpu/nova-core/driver.rs (revision e727d1c382a0cb2e10fd7aabef9217d1a37c80c7)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use kernel::{
4     auxiliary,
5     device::{
6         Bound,
7         Core, //
8     },
9     io::resource,
10     pci,
11     pci::{
12         Class,
13         ClassMask,
14         Vendor, //
15     },
16     prelude::*,
17     sizes::SZ_16M,
18     sync::atomic::{
19         Atomic,
20         Relaxed, //
21     },
22     types::ForLt,
23 };
24 
25 use crate::{
26     api::NovaCoreApi,
27     gpu::{
28         Gpu,
29         Spec, //
30     }, //
31 };
32 
33 /// Counter for generating unique auxiliary device IDs.
34 static AUXILIARY_ID_COUNTER: Atomic<u32> = Atomic::new(0);
35 
36 #[pin_data]
37 pub(crate) struct NovaCore<'bound> {
38     #[allow(clippy::type_complexity)]
39     _reg: auxiliary::Registration<'bound, ForLt!(NovaCoreApi<'_>)>,
40     #[pin]
41     pub(crate) gpu: Gpu<'bound>,
42     bar: pci::Bar<'bound, BAR0_SIZE>,
43     bar1: pci::Bar<'bound>,
44 }
45 
46 pub(crate) struct NovaCoreDriver;
47 
48 const BAR0_SIZE: usize = SZ_16M;
49 
50 pub(crate) type Bar0<'a> = &'a pci::Bar<'a, BAR0_SIZE>;
51 pub(crate) type NovaRegisters = kernel::io::Region<BAR0_SIZE>;
52 pub(crate) type Bar1<'a> = &'a pci::Bar<'a>;
53 
54 /// Returns the Linux PCI resource index that holds BAR1 for an NVIDIA GPU.
55 ///
56 /// On Maxwell through Ada, BAR0 is a 32-bit memory BAR occupying a single
57 /// Linux PCI resource slot, so BAR1 lives at index 1. Starting with Blackwell
58 /// (and on some Ampere GA100 / Hopper SKUs) BAR0 is a 64-bit memory BAR that
59 /// consumes two consecutive resource slots: index 0 holds the low 32 bits and
60 /// index 1 holds the high 32 bits (with no `flags` / or size of its own),
61 /// shifting BAR1 to index 2.
62 pub(crate) fn bar1_resource_index(pdev: &pci::Device<Bound>) -> Result<u32> {
63     // Probe the `IORESOURCE_MEM_64` flag of BAR0 as a robust way of exposing
64     // if BAR0 and hence BAR1 is 64-bit.
65     let flags0 = pdev.resource_flags(0)?;
66     if flags0.contains(resource::Flags::IORESOURCE_MEM_64) {
67         Ok(2)
68     } else {
69         Ok(1)
70     }
71 }
72 
73 kernel::pci_device_table!(
74     PCI_TABLE,
75     <NovaCoreDriver as pci::Driver>::IdInfo,
76     [
77         // Modern NVIDIA GPUs will show up as either VGA or 3D controllers.
78         (
79             pci::DeviceId::from_class_and_vendor(
80                 Class::DISPLAY_VGA,
81                 ClassMask::ClassSubclass,
82                 Vendor::NVIDIA
83             ),
84             ()
85         ),
86         (
87             pci::DeviceId::from_class_and_vendor(
88                 Class::DISPLAY_3D,
89                 ClassMask::ClassSubclass,
90                 Vendor::NVIDIA
91             ),
92             ()
93         ),
94     ]
95 );
96 
97 impl pci::Driver for NovaCoreDriver {
98     type IdInfo = ();
99     type Data<'bound> = NovaCore<'bound>;
100     const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
101 
102     fn probe<'bound>(
103         pdev: &'bound pci::Device<Core<'_>>,
104         _info: Option<&'bound Self::IdInfo>,
105     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
106         dev_dbg!(pdev, "Probe Nova Core GPU driver.\n");
107 
108         try_pin_init!(NovaCore {
109             _: {
110                 pdev.enable_device_mem()?;
111                 pdev.set_master();
112             },
113 
114             bar: pdev.iomap_region_sized::<BAR0_SIZE>(0, c"nova-core/bar0")?,
115 
116             bar1: {
117                 let bar1_idx = bar1_resource_index(pdev)?;
118                 pdev.iomap_region(bar1_idx, c"nova-core/bar1")?
119             },
120 
121             // Run self-tests that do not depend on the `Gpu` instance.
122             #[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
123             _: {
124                 let spec = Spec::new(pdev.as_ref(), bar)?;
125 
126                 // We must wait for GFW_BOOT completion before doing any significant setup on
127                 // the GPU.
128                 Gpu::wait_gfw_boot_completion(pdev.as_ref(), bar, spec.chipset)?;
129 
130                 // The self-test disables and drains the whole tree, so it has to run before
131                 // `Gpu::new` boots the GSP.
132                 crate::irq::doorbell_test::run_selftest(pdev, bar, spec.chipset)?;
133             },
134 
135             // TODO: Use self-referential pin-init syntax once available.
136             gpu <- Gpu::new(
137                 pdev,
138                 // SAFETY: `bar` is initialized above, pinned, and outlives `gpu`.
139                 unsafe { &*core::ptr::from_ref(bar) },
140                 // SAFETY: `bar1` is initialized above, pinned, and outlives `gpu`.
141                 unsafe { &*core::ptr::from_ref(bar1) },
142             ).pin_chain(|_gpu| {
143                 #[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
144                 _gpu.run_selftests(pdev);
145                 Ok(())
146             }),
147 
148             _reg: {
149                 // TODO: Use `&gpu` self-referential pin-init syntax once available.
150                 //
151                 // SAFETY: `gpu` is initialized before this expression is evaluated
152                 // (`try_pin_init!()` initializes fields in initializer order), lives at
153                 // a pinned stable address, and is dropped after `_reg` (struct field
154                 // drop order).
155                 let gpu = unsafe {
156                     Pin::new_unchecked(&*core::ptr::from_ref(gpu.as_ref().get_ref()))
157                 };
158 
159                 // SAFETY: `NovaCore` is dropped when the device is unbound;
160                 // i.e. `mem::forget()` is never called on it.
161                 unsafe {
162                     auxiliary::Registration::new_with_lt(
163                         pdev.as_ref(),
164                         c"nova-drm",
165                         // TODO[XARR]: Use XArray or perhaps IDA for proper ID
166                         // allocation/recycling. For now, use a simple atomic counter that
167                         // never recycles IDs.
168                         AUXILIARY_ID_COUNTER.fetch_add(1, Relaxed),
169                         crate::MODULE_NAME,
170                         NovaCoreApi { gpu, pdev },
171                     )?
172                 }
173             },
174         })
175     }
176 }
177