| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::{
|
| 8 | alloc::allocator::Kmalloc,
|
| 9 | bindings,
|
| 10 | device,
|
| 11 | drm::{
|
| 12 | self,
|
| 13 | driver::AllocImpl,
|
| 14 | private::Sealed, |
| 15 | },
|
| 16 | error::from_err_ptr,
|
| 17 | prelude::*,
|
| 18 | sync::aref::{
|
| 19 | ARef,
|
| 20 | AlwaysRefCounted, |
| 21 | },
|
| 22 | types::{
|
| 23 | NotThreadSafe,
|
| 24 | Opaque, |
| 25 | },
|
| 26 | workqueue::{
|
| 27 | HasDelayedWork,
|
| 28 | HasWork,
|
| 29 | Work,
|
| 30 | WorkItem, |
| 31 | }, |
| 32 | };
|
| 33 | use core::{
|
| 34 | alloc::Layout,
|
| 35 | cell::UnsafeCell,
|
| 36 | marker::PhantomData,
|
| 37 | mem,
|
| 38 | ops::Deref,
|
| 39 | ptr::{
|
| 40 | self,
|
| 41 | NonNull, |
| 42 | },
|
| 43 | };
|
| 44 |
|
| 45 | #[cfg(CONFIG_DRM_LEGACY)]
|
| 46 | macro_rules! drm_legacy_fields {
|
| 47 | ( $($field:ident: $val:expr),* $(,)? ) => {
|
| 48 | bindings::drm_driver {
|
| 49 | $( $field: $val ),*,
|
| 50 | firstopen: None,
|
| 51 | preclose: None,
|
| 52 | dma_ioctl: None,
|
| 53 | dma_quiescent: None,
|
| 54 | context_dtor: None,
|
| 55 | irq_handler: None,
|
| 56 | irq_preinstall: None,
|
| 57 | irq_postinstall: None,
|
| 58 | irq_uninstall: None,
|
| 59 | get_vblank_counter: None,
|
| 60 | enable_vblank: None,
|
| 61 | disable_vblank: None,
|
| 62 | dev_priv_size: 0,
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | #[cfg(not(CONFIG_DRM_LEGACY))]
|
| 68 | macro_rules! drm_legacy_fields {
|
| 69 | ( $($field:ident: $val:expr),* $(,)? ) => {
|
| 70 | bindings::drm_driver {
|
| 71 | $( $field: $val ),*
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | pub trait DeviceContext: Sealed + Send + Sync + 'static {}
|
| 90 |
|
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | pub struct Normal;
|
| 99 |
|
| 100 | impl Sealed for Normal {}
|
| 101 | impl DeviceContext for Normal {}
|
| 102 |
|
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | pub struct Registered;
|
| 113 |
|
| 114 | impl Sealed for Registered {}
|
| 115 | impl DeviceContext for Registered {}
|
| 116 |
|
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | pub struct Ioctl;
|
| 128 |
|
| 129 | impl Sealed for Ioctl {}
|
| 130 | impl DeviceContext for Ioctl {}
|
| 131 |
|
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | pub struct UnregisteredDevice<T: drm::Driver>(ARef<Device<T, Normal>>, NotThreadSafe);
|
| 145 |
|
| 146 | impl<T: drm::Driver> Deref for UnregisteredDevice<T> {
|
| 147 | type Target = Device<T, Normal>;
|
| 148 |
|
| 149 | fn deref(&self) -> &Self::Target {
|
| 150 | &self.0
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | impl<T: drm::Driver> UnregisteredDevice<T> {
|
| 155 | const fn compute_features() -> u32 {
|
| 156 | let mut features = drm::driver::FEAT_GEM;
|
| 157 |
|
| 158 | if T::FEAT_RENDER {
|
| 159 | features |= drm::driver::FEAT_RENDER;
|
| 160 | }
|
| 161 |
|
| 162 | features
|
| 163 | }
|
| 164 |
|
| 165 | const VTABLE: bindings::drm_driver = drm_legacy_fields! {
|
| 166 | load: None,
|
| 167 | open: Some(drm::File::<T::File>::open_callback),
|
| 168 | postclose: Some(drm::File::<T::File>::postclose_callback),
|
| 169 | unload: None,
|
| 170 | release: Some(Device::<T>::release),
|
| 171 | master_set: None,
|
| 172 | master_drop: None,
|
| 173 | debugfs_init: None,
|
| 174 |
|
| 175 | gem_create_object: T::Object::ALLOC_OPS.gem_create_object,
|
| 176 | prime_handle_to_fd: T::Object::ALLOC_OPS.prime_handle_to_fd,
|
| 177 | prime_fd_to_handle: T::Object::ALLOC_OPS.prime_fd_to_handle,
|
| 178 | gem_prime_import: T::Object::ALLOC_OPS.gem_prime_import,
|
| 179 | gem_prime_import_sg_table: T::Object::ALLOC_OPS.gem_prime_import_sg_table,
|
| 180 | dumb_create: T::Object::ALLOC_OPS.dumb_create,
|
| 181 | dumb_map_offset: T::Object::ALLOC_OPS.dumb_map_offset,
|
| 182 |
|
| 183 | show_fdinfo: None,
|
| 184 | fbdev_probe: None,
|
| 185 |
|
| 186 | major: T::INFO.major,
|
| 187 | minor: T::INFO.minor,
|
| 188 | patchlevel: T::INFO.patchlevel,
|
| 189 | name: crate::str::as_char_ptr_in_const_context(T::INFO.name).cast_mut(),
|
| 190 | desc: crate::str::as_char_ptr_in_const_context(T::INFO.desc).cast_mut(),
|
| 191 |
|
| 192 | driver_features: Self::compute_features(),
|
| 193 | ioctls: T::IOCTLS.as_ptr(),
|
| 194 | num_ioctls: T::IOCTLS.len() as i32,
|
| 195 | fops: &Self::GEM_FOPS,
|
| 196 | };
|
| 197 |
|
| 198 | const GEM_FOPS: bindings::file_operations =
|
| 199 | drm::gem::create_fops(crate::module::this_module::<T::OwnerModule>().as_ptr());
|
| 200 |
|
| 201 | |
| 202 | |
| 203 | |
| 204 | pub fn new(
|
| 205 | dev: &T::ParentDevice<device::Bound>,
|
| 206 | data: impl PinInit<T::Data, Error>,
|
| 207 | ) -> Result<Self> {
|
| 208 | |
| 209 | |
| 210 | let layout = Kmalloc::aligned_layout(Layout::new::<Device<T, Normal>>());
|
| 211 |
|
| 212 | |
| 213 | |
| 214 | let alloc_vtable = bindings::drm_driver {
|
| 215 | release: None,
|
| 216 | ..Self::VTABLE
|
| 217 | };
|
| 218 |
|
| 219 | |
| 220 | |
| 221 | |
| 222 | let raw_drm: *mut Device<T, Normal> = unsafe {
|
| 223 | bindings::__drm_dev_alloc(
|
| 224 | dev.as_ref().as_raw(),
|
| 225 | &alloc_vtable,
|
| 226 | layout.size(),
|
| 227 | mem::offset_of!(Device<T, Normal>, dev),
|
| 228 | )
|
| 229 | }
|
| 230 | .cast();
|
| 231 | let raw_drm = NonNull::new(from_err_ptr(raw_drm)?).ok_or(ENOMEM)?;
|
| 232 |
|
| 233 | |
| 234 | |
| 235 | let drm_dev = unsafe { Device::into_drm_device(raw_drm) };
|
| 236 |
|
| 237 | |
| 238 | let raw_data = unsafe { ptr::addr_of_mut!((*raw_drm.as_ptr()).data) };
|
| 239 |
|
| 240 | |
| 241 | |
| 242 | |
| 243 | unsafe { pin_init::raw_try_init(raw_data, data) }.inspect_err(|_| {
|
| 244 | |
| 245 | |
| 246 | unsafe { bindings::drm_dev_put(drm_dev) };
|
| 247 | })?;
|
| 248 |
|
| 249 | |
| 250 | unsafe { (*drm_dev).driver = const { &Self::VTABLE } };
|
| 251 |
|
| 252 | |
| 253 | unsafe { (*raw_drm.as_ptr()).registration_data = UnsafeCell::new(NonNull::dangling()) };
|
| 254 |
|
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | Ok(Self(unsafe { ARef::from_raw(raw_drm) }, NotThreadSafe))
|
| 261 | }
|
| 262 | }
|
| 263 |
|
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | #[repr(C)]
|
| 275 | pub struct Device<T: drm::Driver, C: DeviceContext = Normal> {
|
| 276 | dev: Opaque<bindings::drm_device>,
|
| 277 | data: T::Data,
|
| 278 | pub(super) registration_data: UnsafeCell<NonNull<T::RegistrationData<'static>>>,
|
| 279 | _ctx: PhantomData<C>,
|
| 280 | }
|
| 281 |
|
| 282 | impl<T: drm::Driver, C: DeviceContext> Device<T, C> {
|
| 283 | pub(crate) fn as_raw(&self) -> *mut bindings::drm_device {
|
| 284 | self.dev.get()
|
| 285 | }
|
| 286 |
|
| 287 | |
| 288 | |
| 289 | |
| 290 | unsafe fn from_drm_device(ptr: *const bindings::drm_device) -> *mut Self {
|
| 291 | |
| 292 | |
| 293 | unsafe { crate::container_of!(Opaque::cast_from(ptr), Self, dev) }.cast_mut()
|
| 294 | }
|
| 295 |
|
| 296 | |
| 297 | |
| 298 | |
| 299 | unsafe fn into_drm_device(ptr: NonNull<Self>) -> *mut bindings::drm_device {
|
| 300 | |
| 301 | unsafe { &raw mut (*ptr.as_ptr()).dev }.cast()
|
| 302 | }
|
| 303 |
|
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | #[doc(hidden)]
|
| 316 | pub unsafe fn from_raw<'a>(ptr: *const bindings::drm_device) -> &'a Self {
|
| 317 | |
| 318 | |
| 319 | let ptr = unsafe { Self::from_drm_device(ptr) };
|
| 320 |
|
| 321 | |
| 322 | unsafe { &*ptr.cast() }
|
| 323 | }
|
| 324 |
|
| 325 | extern "C" fn release(ptr: *mut bindings::drm_device) {
|
| 326 | |
| 327 | let this = unsafe { Self::from_drm_device(ptr) };
|
| 328 |
|
| 329 | |
| 330 | |
| 331 | |
| 332 | unsafe { core::ptr::drop_in_place(this) };
|
| 333 | }
|
| 334 |
|
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | pub(crate) unsafe fn assume_ctx<NewCtx: DeviceContext>(&self) -> &Device<T, NewCtx> {
|
| 342 | |
| 343 | unsafe { mem::transmute(self) }
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | impl<T: drm::Driver> Device<T, Ioctl> {
|
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | #[must_use]
|
| 354 | pub fn registration_guard(&self) -> Option<RegistrationGuard<'_, T>> {
|
| 355 | let mut idx: i32 = 0;
|
| 356 | |
| 357 | if unsafe { bindings::drm_dev_enter(self.as_raw(), &mut idx) } {
|
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | Some(RegistrationGuard {
|
| 364 | |
| 365 | dev: unsafe { self.assume_ctx() },
|
| 366 | idx,
|
| 367 | _not_send: NotThreadSafe,
|
| 368 | })
|
| 369 | } else {
|
| 370 | None
|
| 371 | }
|
| 372 | }
|
| 373 | }
|
| 374 |
|
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | #[must_use]
|
| 388 | pub struct RegistrationGuard<'a, T: drm::Driver> {
|
| 389 | dev: &'a Device<T, Registered>,
|
| 390 | idx: i32,
|
| 391 | _not_send: NotThreadSafe,
|
| 392 | }
|
| 393 |
|
| 394 | impl<T: drm::Driver> Device<T, Registered> {
|
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | #[inline]
|
| 403 | unsafe fn registration_data_unchecked(&self) -> &T::RegistrationData<'_> {
|
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | unsafe { (*self.registration_data.get()).cast::<_>().as_ref() }
|
| 411 | }
|
| 412 |
|
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | #[inline]
|
| 421 | pub fn registration_data_with<R, F>(&self, f: F) -> R
|
| 422 | where
|
| 423 | F: for<'a> FnOnce(&'a T::RegistrationData<'a>) -> R,
|
| 424 | {
|
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | f(unsafe { self.registration_data_unchecked() })
|
| 430 | }
|
| 431 | }
|
| 432 |
|
| 433 | impl<T: drm::Driver> Deref for RegistrationGuard<'_, T> {
|
| 434 | type Target = Device<T, Registered>;
|
| 435 |
|
| 436 | #[inline]
|
| 437 | fn deref(&self) -> &Self::Target {
|
| 438 | self.dev
|
| 439 | }
|
| 440 | }
|
| 441 |
|
| 442 | impl<T: drm::Driver> Drop for RegistrationGuard<'_, T> {
|
| 443 | #[inline]
|
| 444 | fn drop(&mut self) {
|
| 445 | |
| 446 | |
| 447 | unsafe { bindings::drm_dev_exit(self.idx) };
|
| 448 | }
|
| 449 | }
|
| 450 |
|
| 451 | impl<T: drm::Driver> Deref for Device<T> {
|
| 452 | type Target = T::Data;
|
| 453 |
|
| 454 | fn deref(&self) -> &Self::Target {
|
| 455 | &self.data
|
| 456 | }
|
| 457 | }
|
| 458 |
|
| 459 | impl<T: drm::Driver> Deref for Device<T, Registered> {
|
| 460 | type Target = Device<T>;
|
| 461 |
|
| 462 | #[inline]
|
| 463 | fn deref(&self) -> &Self::Target {
|
| 464 | |
| 465 | |
| 466 | unsafe { self.assume_ctx() }
|
| 467 | }
|
| 468 | }
|
| 469 |
|
| 470 | impl<T: drm::Driver> Deref for Device<T, Ioctl> {
|
| 471 | type Target = Device<T>;
|
| 472 |
|
| 473 | #[inline]
|
| 474 | fn deref(&self) -> &Self::Target {
|
| 475 | |
| 476 | |
| 477 | unsafe { self.assume_ctx() }
|
| 478 | }
|
| 479 | }
|
| 480 |
|
| 481 | |
| 482 | |
| 483 | unsafe impl<T: drm::Driver> AlwaysRefCounted for Device<T> {
|
| 484 | fn inc_ref(&self) {
|
| 485 | |
| 486 | unsafe { bindings::drm_dev_get(self.as_raw()) };
|
| 487 | }
|
| 488 |
|
| 489 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 490 | |
| 491 | let drm_dev = unsafe { Self::into_drm_device(obj) };
|
| 492 |
|
| 493 | |
| 494 | unsafe { bindings::drm_dev_put(drm_dev) };
|
| 495 | }
|
| 496 | }
|
| 497 |
|
| 498 | impl<T: drm::Driver> AsRef<T::ParentDevice<device::Normal>> for Device<T> {
|
| 499 | fn as_ref(&self) -> &T::ParentDevice<device::Normal> {
|
| 500 | |
| 501 | |
| 502 | let dev = unsafe { device::Device::from_raw((*self.as_raw()).dev) };
|
| 503 |
|
| 504 | |
| 505 | |
| 506 | unsafe { device::AsBusDevice::from_device(dev) }
|
| 507 | }
|
| 508 | }
|
| 509 |
|
| 510 | impl<T: drm::Driver> AsRef<T::ParentDevice<device::Bound>> for Device<T, Registered> {
|
| 511 | #[inline]
|
| 512 | fn as_ref(&self) -> &T::ParentDevice<device::Bound> {
|
| 513 | let dev = (**self).as_ref().as_ref();
|
| 514 |
|
| 515 | |
| 516 | let dev = unsafe { dev.as_bound() };
|
| 517 |
|
| 518 | |
| 519 | |
| 520 | unsafe { device::AsBusDevice::from_device(dev) }
|
| 521 | }
|
| 522 | }
|
| 523 |
|
| 524 | |
| 525 | unsafe impl<T: drm::Driver, C: DeviceContext> Send for Device<T, C> {}
|
| 526 |
|
| 527 | |
| 528 | |
| 529 | unsafe impl<T: drm::Driver, C: DeviceContext> Sync for Device<T, C> {}
|
| 530 |
|
| 531 | impl<T: drm::Driver, const ID: u64> WorkItem<ID> for Device<T>
|
| 532 | where
|
| 533 | T::Data: WorkItem<ID, Pointer = ARef<Self>>,
|
| 534 | T::Data: HasWork<Self, ID>,
|
| 535 | {
|
| 536 | type Pointer = ARef<Self>;
|
| 537 |
|
| 538 | fn run(ptr: ARef<Self>) {
|
| 539 | T::Data::run(ptr);
|
| 540 | }
|
| 541 | }
|
| 542 |
|
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | unsafe impl<T, C, const ID: u64> HasWork<Self, ID> for Device<T, C>
|
| 554 | where
|
| 555 | T: drm::Driver,
|
| 556 | T::Data: HasWork<Self, ID>,
|
| 557 | C: DeviceContext,
|
| 558 | {
|
| 559 | unsafe fn raw_get_work(ptr: *mut Self) -> *mut Work<Self, ID> {
|
| 560 | |
| 561 | let data_ptr = unsafe { &raw mut (*ptr).data };
|
| 562 |
|
| 563 | |
| 564 | unsafe { T::Data::raw_get_work(data_ptr) }
|
| 565 | }
|
| 566 |
|
| 567 | unsafe fn work_container_of(ptr: *mut Work<Self, ID>) -> *mut Self {
|
| 568 | |
| 569 | |
| 570 | let data_ptr = unsafe { T::Data::work_container_of(ptr) };
|
| 571 |
|
| 572 | |
| 573 | unsafe { crate::container_of!(data_ptr, Self, data) }
|
| 574 | }
|
| 575 | }
|
| 576 |
|
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | unsafe impl<T, C, const ID: u64> HasDelayedWork<Self, ID> for Device<T, C>
|
| 583 | where
|
| 584 | T: drm::Driver,
|
| 585 | T::Data: HasDelayedWork<Self, ID>,
|
| 586 | C: DeviceContext,
|
| 587 | {
|
| 588 | }
|