summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/nova/driver.rs
blob: 8f5753f04dbae746bd9c1feb7863e0240819de99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: GPL-2.0

use kernel::{
    bindings, c_str, drm,
    drm::{drv, ioctl},
    pci,
    pci::define_pci_id_table,
    prelude::*,
    sync::Arc,
};

use crate::{file::File, gpu::Gpu};

pub(crate) struct NovaDriver;

/// Convienence type alias for the DRM device type for this driver
pub(crate) type NovaDevice = drm::device::Device<NovaDriver>;

#[allow(dead_code)]
pub(crate) struct NovaData {
    pub(crate) gpu: Arc<Gpu>,
    pub(crate) pdev: pci::Device,
}

const BAR0_SIZE: usize = 0x1000000;
pub(crate) type Bar0 = pci::Bar<BAR0_SIZE>;

const INFO: drm::drv::DriverInfo = drm::drv::DriverInfo {
    major: 0,
    minor: 0,
    patchlevel: 0,
    name: c_str!("nova"),
    desc: c_str!("Nvidia Graphics"),
    date: c_str!("20240227"),
};

impl pci::Driver for NovaDriver {
    type Data = Arc<NovaData>;

    define_pci_id_table! {
        (),
        [ (pci::DeviceId::new(bindings::PCI_VENDOR_ID_NVIDIA, bindings::PCI_ANY_ID as u32), None) ]
    }

    fn probe(pdev: &mut pci::Device, _id_info: Option<&Self::IdInfo>) -> Result<Self::Data> {
        dev_dbg!(pdev.as_ref(), "Probe Nova GPU driver.\n");

        pdev.enable_device_mem()?;
        pdev.set_master();

        let bar = pdev.iomap_region_sized::<BAR0_SIZE>(0, c_str!("nova"))?;

        let gpu = Gpu::new(pdev, bar)?;
        gpu.init()?;

        let data = Arc::new(
            NovaData {
                gpu,
                pdev: pdev.clone(),
            },
            GFP_KERNEL,
        )?;

        let drm = drm::device::Device::<NovaDriver>::new(pdev.as_ref(), data.clone())?;
        drm::drv::Registration::new_foreign_owned(drm, 0)?;

        Ok(data)
    }

    fn remove(data: &Self::Data) {
        dev_dbg!(data.pdev.as_ref(), "Remove Nova GPU driver.\n");
    }
}

#[vtable]
impl drm::drv::Driver for NovaDriver {
    type Data = Arc<NovaData>;
    type File = File;
    type Object = crate::gem::Object;

    const INFO: drm::drv::DriverInfo = INFO;
    const FEATURES: u32 = drv::FEAT_GEM;

    kernel::declare_drm_ioctls! {
        (NOVA_GETPARAM, drm_nova_getparam, ioctl::RENDER_ALLOW, File::get_param),
        (NOVA_GEM_CREATE, drm_nova_gem_create, ioctl::AUTH | ioctl::RENDER_ALLOW, File::gem_create),
        (NOVA_GEM_INFO, drm_nova_gem_info, ioctl::AUTH | ioctl::RENDER_ALLOW, File::gem_info),
    }
}