xref: /linux/drivers/gpu/drm/nova/driver.rs (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use kernel::{
4     auxiliary,
5     device::{
6         Core,
7         DeviceContext, //
8     },
9     drm::{
10         self,
11         gem,
12         ioctl, //
13     },
14     prelude::*,
15     sync::aref::ARef, //
16 };
17 
18 use crate::file::File;
19 use crate::gem::NovaObject;
20 
21 pub(crate) struct NovaDriver;
22 
23 pub(crate) struct Nova<'bound> {
24     #[expect(unused)]
25     drm: ARef<drm::Device<NovaDriver>>,
26     _reg: drm::Registration<'bound, NovaDriver>,
27 }
28 
29 /// Convienence type alias for the DRM device type for this driver
30 pub(crate) type NovaDevice<Ctx = drm::Normal> = drm::Device<NovaDriver, Ctx>;
31 
32 const INFO: drm::DriverInfo = drm::DriverInfo {
33     major: 0,
34     minor: 0,
35     patchlevel: 0,
36     name: c"nova-drm",
37     desc: c"NVIDIA Graphics and Compute",
38 };
39 
40 const NOVA_CORE_MODULE_NAME: &CStr = c"nova-core";
41 const AUXILIARY_NAME: &CStr = c"nova-drm";
42 
43 kernel::auxiliary_device_table!(
44     AUX_TABLE,
45     <NovaDriver as auxiliary::Driver>::IdInfo,
46     [(
47         auxiliary::DeviceId::new(NOVA_CORE_MODULE_NAME, AUXILIARY_NAME),
48         ()
49     )]
50 );
51 
52 impl auxiliary::Driver for NovaDriver {
53     type IdInfo = ();
54     type Data<'bound> = Nova<'bound>;
55     const ID_TABLE: auxiliary::IdTable<Self::IdInfo> = &AUX_TABLE;
56 
57     fn probe<'bound>(
58         adev: &'bound auxiliary::Device<Core<'_>>,
59         _info: &'bound Self::IdInfo,
60     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
61         let drm = drm::UnregisteredDevice::<Self>::new(adev, Ok(()))?;
62         // SAFETY: `reg` is stored in `Nova` and dropped when the driver is unbound; it is
63         // never forgotten.
64         let reg = unsafe { drm::Registration::new(adev.as_ref(), drm, (), 0)? };
65 
66         Ok(Nova {
67             drm: reg.device().into(),
68             _reg: reg,
69         })
70     }
71 }
72 
73 #[vtable]
74 impl drm::Driver for NovaDriver {
75     type Data = ();
76     type RegistrationData<'a> = ();
77     type File = File;
78     type Object = gem::Object<NovaObject>;
79     type ParentDevice<Ctx: DeviceContext> = auxiliary::Device<Ctx>;
80 
81     const INFO: drm::DriverInfo = INFO;
82 
83     kernel::declare_drm_ioctls! {
84         (NOVA_GETPARAM, drm_nova_getparam, ioctl::RENDER_ALLOW, File::get_param),
85         (NOVA_GEM_CREATE, drm_nova_gem_create, ioctl::AUTH | ioctl::RENDER_ALLOW, File::gem_create),
86         (NOVA_GEM_INFO, drm_nova_gem_info, ioctl::AUTH | ioctl::RENDER_ALLOW, File::gem_info),
87     }
88 }
89