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