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