// SPDX-License-Identifier: GPL-2.0 use kernel::{ auxiliary, device::Core, dma::Device, dma::DmaMask, pci, pci::{ Class, ClassMask, Vendor, // }, prelude::*, sizes::SZ_16M, sync::{ atomic::{ Atomic, Relaxed, // }, Arc, }, types::ForLt, }; use crate::gpu::Gpu; /// Counter for generating unique auxiliary device IDs. static AUXILIARY_ID_COUNTER: Atomic = Atomic::new(0); #[pin_data] pub(crate) struct NovaCore<'bound> { #[pin] pub(crate) gpu: Gpu, #[allow(clippy::type_complexity)] _reg: auxiliary::Registration<'bound, ForLt!(())>, } pub(crate) struct NovaCoreDriver; const BAR0_SIZE: usize = SZ_16M; // For now we only support Ampere which can use up to 47-bit DMA addresses. // // TODO: Add an abstraction for this to support newer GPUs which may support // larger DMA addresses. Limiting these GPUs to smaller address widths won't // have any adverse affects, unless installed on systems which require larger // DMA addresses. These systems should be quite rare. const GPU_DMA_BITS: u32 = 47; pub(crate) type Bar0 = pci::Bar<'static, BAR0_SIZE>; kernel::pci_device_table!( PCI_TABLE, MODULE_PCI_TABLE, ::IdInfo, [ // Modern NVIDIA GPUs will show up as either VGA or 3D controllers. ( pci::DeviceId::from_class_and_vendor( Class::DISPLAY_VGA, ClassMask::ClassSubclass, Vendor::NVIDIA ), () ), ( pci::DeviceId::from_class_and_vendor( Class::DISPLAY_3D, ClassMask::ClassSubclass, Vendor::NVIDIA ), () ), ] ); impl pci::Driver for NovaCoreDriver { type IdInfo = (); type Data<'bound> = NovaCore<'bound>; const ID_TABLE: pci::IdTable = &PCI_TABLE; fn probe<'bound>( pdev: &'bound pci::Device>, _info: &'bound Self::IdInfo, ) -> impl PinInit, Error> + 'bound { pin_init::pin_init_scope(move || { dev_dbg!(pdev, "Probe Nova Core GPU driver.\n"); pdev.enable_device_mem()?; pdev.set_master(); // SAFETY: No concurrent DMA allocations or mappings can be made because // the device is still being probed and therefore isn't being used by // other threads of execution. unsafe { pdev.dma_set_mask_and_coherent(DmaMask::new::())? }; let bar = Arc::new( pdev.iomap_region_sized::(0, c"nova-core/bar0")? .into_devres()?, GFP_KERNEL, )?; Ok(try_pin_init!(NovaCore { gpu <- Gpu::new(pdev, bar.clone(), bar.access(pdev.as_ref())?), _reg: auxiliary::Registration::new( pdev.as_ref(), c"nova-drm", // TODO[XARR]: Use XArray or perhaps IDA for proper ID allocation/recycling. For // now, use a simple atomic counter that never recycles IDs. AUXILIARY_ID_COUNTER.fetch_add(1, Relaxed), crate::MODULE_NAME, (), )?, })) }) } fn unbind<'bound>(pdev: &'bound pci::Device>, this: Pin<&Self::Data<'bound>>) { this.gpu.unbind(pdev.as_ref()); } }