From b6214aa6152cc7adf23f14d3c12bbd03ae2829d4 Mon Sep 17 00:00:00 2001 From: Danilo Krummrich Date: Fri, 12 Jan 2024 00:22:26 +0100 Subject: nova: move driver code to separate source file Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nova/Makefile | 2 +- drivers/gpu/drm/nova/bios.rs | 2 +- drivers/gpu/drm/nova/driver.rs | 86 ++++++++++++++++++++++++++++++++++ drivers/gpu/drm/nova/gpu.rs | 2 +- drivers/gpu/drm/nova/nova.rs | 20 ++++++++ drivers/gpu/drm/nova/nova_drv.rs | 99 ---------------------------------------- 6 files changed, 109 insertions(+), 102 deletions(-) create mode 100644 drivers/gpu/drm/nova/driver.rs create mode 100644 drivers/gpu/drm/nova/nova.rs delete mode 100644 drivers/gpu/drm/nova/nova_drv.rs diff --git a/drivers/gpu/drm/nova/Makefile b/drivers/gpu/drm/nova/Makefile index 6fbb32893ecc..42019bff3173 100644 --- a/drivers/gpu/drm/nova/Makefile +++ b/drivers/gpu/drm/nova/Makefile @@ -1,3 +1,3 @@ -nova-y += nova_drv.o +# SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_DRM_NOVA) += nova.o diff --git a/drivers/gpu/drm/nova/bios.rs b/drivers/gpu/drm/nova/bios.rs index f5b9db2bc9fc..5a6f5df08c4d 100644 --- a/drivers/gpu/drm/nova/bios.rs +++ b/drivers/gpu/drm/nova/bios.rs @@ -3,7 +3,7 @@ use kernel::io_mem::IoMem; use kernel::prelude::*; use alloc::vec::Vec; -use crate::BAR_SIZE; +use crate::driver::BAR_SIZE; use crate::fwsec::FalconUCodeDescV3; diff --git a/drivers/gpu/drm/nova/driver.rs b/drivers/gpu/drm/nova/driver.rs new file mode 100644 index 000000000000..33abf2545994 --- /dev/null +++ b/drivers/gpu/drm/nova/driver.rs @@ -0,0 +1,86 @@ + +use alloc::boxed::Box; +use core::{ + format_args, + pin::Pin, +}; +use kernel::{ + bindings, + c_str, + driver, + error::code::*, + io_mem::IoMem, + pci, pci::define_pci_id_table, + prelude::*, +}; + +use crate::gpu::Gpu; +use crate::gpu::CardType; + +struct NovaDevice; + +/// BAR Size +pub(crate) const BAR_SIZE: usize = 16777216; + +impl pci::Driver for NovaDevice { + + define_pci_id_table! { + (), + [ (pci::DeviceId::new(bindings::PCI_VENDOR_ID_NVIDIA, bindings::PCI_ANY_ID as u32), None) ] + } + + fn probe(dev: &mut pci::Device, _id_info: Option<&Self::IdInfo>) -> Result { + pr_info!("probe()\n"); + + dev.enable_device_mem()?; + dev.set_master(); + + let _bars = dev.select_bars(bindings::IORESOURCE_MEM.into()); + + let res = dev.take_resource(0).ok_or(ENXIO)?; + let bar = unsafe { IoMem::::try_new(res) }?; + + let boot0 = u64::from_le(bar.readq(0)); + + if boot0 & 0x1f000000 == 0 { + return Err(ENODEV); + } + pr_info!("nvidia hw rev 0x{:#x}", boot0); + let chipset = (boot0 & 0x1ff00000) >> 20; + let _chiprev = boot0 & 0xff; + + let card_type = match chipset & 0x1f0 { + 0x160 => { pr_info!("TU100"); CardType::TU100 }, + 0x170 => { pr_info!("GA100"); CardType::GA100 }, + 0x190 => { pr_info!("AD100"); CardType::AD100 }, + _ => return Err(ENODEV) + }; + + let mut gpu = Gpu::new(card_type, bar); + let _ = gpu.init(dev)?; + + Ok(()) + } + + fn remove(_data: &Self::Data) { + pr_info!("remove()\n"); + } +} + +pub(crate) struct NovaModule { + _registration: Pin>>>, +} + +impl kernel::Module for NovaModule { + fn init(_name: &'static CStr, module: &'static ThisModule) -> Result { + pr_info!("Module loaded!\n"); + + let registration = driver::Registration::new_pinned(c_str!("nova"), module)?; + pr_info!("Registerd PCI driver.\n"); + + Ok(Self { + _registration: registration, + }) + } +} + diff --git a/drivers/gpu/drm/nova/gpu.rs b/drivers/gpu/drm/nova/gpu.rs index 382303866cd3..d3f1890953b1 100644 --- a/drivers/gpu/drm/nova/gpu.rs +++ b/drivers/gpu/drm/nova/gpu.rs @@ -9,7 +9,7 @@ use kernel::{ use crate::bios::*; -use crate::BAR_SIZE; +use crate::driver::BAR_SIZE; pub(crate) enum CardType { TU100 = 0x160, diff --git a/drivers/gpu/drm/nova/nova.rs b/drivers/gpu/drm/nova/nova.rs new file mode 100644 index 000000000000..0289fce7d2d5 --- /dev/null +++ b/drivers/gpu/drm/nova/nova.rs @@ -0,0 +1,20 @@ +//! Nova GPU Driver + +mod driver; +mod bios; +mod fwsec; +mod gpu; + +use kernel::prelude::module; + +use driver::NovaModule; + +module! { + type: NovaModule, + name: "Nova", + author: "Danilo Krummrich", + description: "Nova GPU driver", + license: "GPL v2", + params: { + }, +} diff --git a/drivers/gpu/drm/nova/nova_drv.rs b/drivers/gpu/drm/nova/nova_drv.rs deleted file mode 100644 index c2bf7be4dd0b..000000000000 --- a/drivers/gpu/drm/nova/nova_drv.rs +++ /dev/null @@ -1,99 +0,0 @@ -//! Nova GPU Driver - -mod gpu; -mod bios; -mod fwsec; - -use crate::gpu::CardType; -use alloc::boxed::Box; -use core::{ - format_args, - pin::Pin, -}; -use kernel::{ - bindings, - c_str, - driver, - error::code::*, - io_mem::IoMem, - pci, pci::define_pci_id_table, - prelude::*, -}; - -use gpu::Gpu; -struct NovaDevice; - -/// BAR Size -pub(crate) const BAR_SIZE: usize = 16777216; - -impl pci::Driver for NovaDevice { - - define_pci_id_table! { - (), - [ (pci::DeviceId::new(bindings::PCI_VENDOR_ID_NVIDIA, bindings::PCI_ANY_ID as u32), None) ] - } - - fn probe(dev: &mut pci::Device, _id_info: Option<&Self::IdInfo>) -> Result { - pr_info!("probe()\n"); - - dev.enable_device_mem()?; - dev.set_master(); - - let _bars = dev.select_bars(bindings::IORESOURCE_MEM.into()); - - let res = dev.take_resource(0).ok_or(ENXIO)?; - let bar = unsafe { IoMem::::try_new(res) }?; - - let boot0 = u64::from_le(bar.readq(0)); - - if boot0 & 0x1f000000 == 0 { - return Err(ENODEV); - } - pr_info!("nvidia hw rev 0x{:#x}", boot0); - let chipset = (boot0 & 0x1ff00000) >> 20; - let _chiprev = boot0 & 0xff; - - let card_type = match chipset & 0x1f0 { - 0x160 => { pr_info!("TU100"); CardType::TU100 }, - 0x170 => { pr_info!("GA100"); CardType::GA100 }, - 0x190 => { pr_info!("AD100"); CardType::AD100 }, - _ => return Err(ENODEV) - }; - - let mut gpu = Gpu::new(card_type, bar); - let _ = gpu.init(dev)?; - - Ok(()) - } - - fn remove(_data: &Self::Data) { - pr_info!("remove()\n"); - } -} - -struct NovaModule { - _registration: Pin>>>, -} - -impl kernel::Module for NovaModule { - fn init(_name: &'static CStr, module: &'static ThisModule) -> Result { - pr_info!("Module loaded!\n"); - - let registration = driver::Registration::new_pinned(c_str!("nova"), module)?; - pr_info!("Registerd PCI driver.\n"); - - Ok(Self { - _registration: registration, - }) - } -} - -module! { - type: NovaModule, - name: "Nova", - author: "Danilo Krummrich", - description: "Nova GPU driver", - license: "GPL v2", - params: { - }, -} -- cgit v1.2.3-58-ga151