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 use nova_core::api::{ 22 NovaCoreApi, 23 NovaCoreApiHandle, // 24 }; 25 26 pub(crate) struct NovaDriver; 27 28 pub(crate) struct Nova<'bound> { 29 #[expect(unused)] 30 drm: ARef<drm::Device<NovaDriver>>, 31 _reg: drm::Registration<'bound, NovaDriver>, 32 } 33 34 /// DRM registration data, accessible from ioctl handlers via the registration guard. 35 pub(crate) struct DrmRegData<'bound> { 36 pub(crate) api: NovaCoreApiHandle<'bound>, 37 } 38 39 /// Convienence type alias for the DRM device type for this driver 40 pub(crate) type NovaDevice<Ctx = drm::Normal> = drm::Device<NovaDriver, Ctx>; 41 42 const INFO: drm::DriverInfo = drm::DriverInfo { 43 major: 0, 44 minor: 0, 45 patchlevel: 0, 46 name: c"nova-drm", 47 desc: c"NVIDIA Graphics and Compute", 48 }; 49 50 const NOVA_CORE_MODULE_NAME: &CStr = c"nova-core"; 51 const AUXILIARY_NAME: &CStr = c"nova-drm"; 52 53 kernel::auxiliary_device_table!( 54 AUX_TABLE, 55 <NovaDriver as auxiliary::Driver>::IdInfo, 56 [( 57 auxiliary::DeviceId::new(NOVA_CORE_MODULE_NAME, AUXILIARY_NAME), 58 () 59 )] 60 ); 61 62 impl auxiliary::Driver for NovaDriver { 63 type IdInfo = (); 64 type Data<'bound> = Nova<'bound>; 65 const ID_TABLE: auxiliary::IdTable<Self::IdInfo> = &AUX_TABLE; 66 67 fn probe<'bound>( 68 adev: &'bound auxiliary::Device<Core<'_>>, 69 _info: &'bound Self::IdInfo, 70 ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound { 71 let drm = drm::UnregisteredDevice::<Self>::new(adev, Ok(()))?; 72 let reg_data = DrmRegData { 73 api: NovaCoreApi::of(adev)?, 74 }; 75 // SAFETY: `reg` is stored in `Nova` and dropped when the driver is unbound; it is 76 // never forgotten. 77 let reg = unsafe { drm::Registration::new(adev.as_ref(), drm, reg_data, 0)? }; 78 79 Ok(Nova { 80 drm: reg.device().into(), 81 _reg: reg, 82 }) 83 } 84 } 85 86 #[vtable] 87 impl drm::Driver for NovaDriver { 88 type Data = (); 89 type RegistrationData<'a> = DrmRegData<'a>; 90 type File = File; 91 type Object = gem::Object<NovaObject>; 92 type ParentDevice<Ctx: DeviceContext> = auxiliary::Device<Ctx>; 93 94 const INFO: drm::DriverInfo = INFO; 95 const FEAT_RENDER: bool = true; 96 97 kernel::declare_drm_ioctls! { 98 (NOVA_GETPARAM, drm_nova_getparam, ioctl::RENDER_ALLOW, File::get_param), 99 (NOVA_GEM_CREATE, drm_nova_gem_create, ioctl::AUTH | ioctl::RENDER_ALLOW, File::gem_create), 100 (NOVA_GEM_INFO, drm_nova_gem_info, ioctl::AUTH | ioctl::RENDER_ALLOW, File::gem_info), 101 (NOVA_INFO, drm_nova_info, ioctl::RENDER_ALLOW, File::info), 102 } 103 } 104