1 /* 2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 4 * Copyright (c) 2009-2010, Code Aurora Forum. 5 * Copyright 2016 Intel Corp. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the next 15 * paragraph) shall be included in all copies or substantial portions of the 16 * Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 * OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 27 #ifndef _DRM_DRV_H_ 28 #define _DRM_DRV_H_ 29 30 #include <linux/list.h> 31 #include <linux/irqreturn.h> 32 33 #include <linux/device.h> 34 35 #include <sys/sysctl.h> 36 37 #include <drm/drm_device.h> 38 39 struct drm_file; 40 struct drm_gem_object; 41 struct drm_master; 42 struct drm_minor; 43 struct dma_buf_attachment; 44 struct drm_display_mode; 45 struct drm_mode_create_dumb; 46 47 /* driver capabilities and requirements mask */ 48 #define DRIVER_USE_AGP 0x1 49 #define DRIVER_LEGACY 0x2 50 #define DRIVER_PCI_DMA 0x8 51 #define DRIVER_SG 0x10 52 #define DRIVER_HAVE_DMA 0x20 53 #define DRIVER_HAVE_IRQ 0x40 54 #define DRIVER_IRQ_SHARED 0x80 55 #define DRIVER_GEM 0x1000 56 #define DRIVER_MODESET 0x2000 57 #define DRIVER_PRIME 0x4000 58 #define DRIVER_RENDER 0x8000 59 #define DRIVER_ATOMIC 0x10000 60 #define DRIVER_KMS_LEGACY_CONTEXT 0x20000 61 #define DRIVER_SYNCOBJ 0x40000 62 #define DRIVER_PREFER_XBGR_30BPP 0x80000 63 64 /** 65 * struct drm_driver - DRM driver structure 66 * 67 * This structure represent the common code for a family of cards. There will 68 * one drm_device for each card present in this family. It contains lots of 69 * vfunc entries, and a pile of those probably should be moved to more 70 * appropriate places like &drm_mode_config_funcs or into a new operations 71 * structure for GEM drivers. 72 */ 73 struct drm_driver { 74 /** 75 * @load: 76 * 77 * Backward-compatible driver callback to complete 78 * initialization steps after the driver is registered. For 79 * this reason, may suffer from race conditions and its use is 80 * deprecated for new drivers. It is therefore only supported 81 * for existing drivers not yet converted to the new scheme. 82 * See drm_dev_init() and drm_dev_register() for proper and 83 * race-free way to set up a &struct drm_device. 84 * 85 * This is deprecated, do not use! 86 * 87 * Returns: 88 * 89 * Zero on success, non-zero value on failure. 90 */ 91 int (*load) (struct drm_device *, unsigned long flags); 92 93 /** 94 * @open: 95 * 96 * Driver callback when a new &struct drm_file is opened. Useful for 97 * setting up driver-private data structures like buffer allocators, 98 * execution contexts or similar things. Such driver-private resources 99 * must be released again in @postclose. 100 * 101 * Since the display/modeset side of DRM can only be owned by exactly 102 * one &struct drm_file (see &drm_file.is_master and &drm_device.master) 103 * there should never be a need to set up any modeset related resources 104 * in this callback. Doing so would be a driver design bug. 105 * 106 * Returns: 107 * 108 * 0 on success, a negative error code on failure, which will be 109 * promoted to userspace as the result of the open() system call. 110 */ 111 int (*open) (struct drm_device *, struct drm_file *); 112 113 /** 114 * @postclose: 115 * 116 * One of the driver callbacks when a new &struct drm_file is closed. 117 * Useful for tearing down driver-private data structures allocated in 118 * @open like buffer allocators, execution contexts or similar things. 119 * 120 * Since the display/modeset side of DRM can only be owned by exactly 121 * one &struct drm_file (see &drm_file.is_master and &drm_device.master) 122 * there should never be a need to tear down any modeset related 123 * resources in this callback. Doing so would be a driver design bug. 124 */ 125 void (*postclose) (struct drm_device *, struct drm_file *); 126 127 /** 128 * @lastclose: 129 * 130 * Called when the last &struct drm_file has been closed and there's 131 * currently no userspace client for the &struct drm_device. 132 * 133 * Modern drivers should only use this to force-restore the fbdev 134 * framebuffer using drm_fb_helper_restore_fbdev_mode_unlocked(). 135 * Anything else would indicate there's something seriously wrong. 136 * Modern drivers can also use this to execute delayed power switching 137 * state changes, e.g. in conjunction with the :ref:`vga_switcheroo` 138 * infrastructure. 139 * 140 * This is called after @postclose hook has been called. 141 * 142 * NOTE: 143 * 144 * All legacy drivers use this callback to de-initialize the hardware. 145 * This is purely because of the shadow-attach model, where the DRM 146 * kernel driver does not really own the hardware. Instead ownershipe is 147 * handled with the help of userspace through an inheritedly racy dance 148 * to set/unset the VT into raw mode. 149 * 150 * Legacy drivers initialize the hardware in the @firstopen callback, 151 * which isn't even called for modern drivers. 152 */ 153 void (*lastclose) (struct drm_device *); 154 155 /** 156 * @unload: 157 * 158 * Reverse the effects of the driver load callback. Ideally, 159 * the clean up performed by the driver should happen in the 160 * reverse order of the initialization. Similarly to the load 161 * hook, this handler is deprecated and its usage should be 162 * dropped in favor of an open-coded teardown function at the 163 * driver layer. See drm_dev_unregister() and drm_dev_put() 164 * for the proper way to remove a &struct drm_device. 165 * 166 * The unload() hook is called right after unregistering 167 * the device. 168 * 169 */ 170 void (*unload) (struct drm_device *); 171 172 /** 173 * @release: 174 * 175 * Optional callback for destroying device data after the final 176 * reference is released, i.e. the device is being destroyed. Drivers 177 * using this callback are responsible for calling drm_dev_fini() 178 * to finalize the device and then freeing the struct themselves. 179 */ 180 void (*release) (struct drm_device *); 181 182 /** 183 * @get_vblank_counter: 184 * 185 * Driver callback for fetching a raw hardware vblank counter for the 186 * CRTC specified with the pipe argument. If a device doesn't have a 187 * hardware counter, the driver can simply leave the hook as NULL. 188 * The DRM core will account for missed vblank events while interrupts 189 * where disabled based on system timestamps. 190 * 191 * Wraparound handling and loss of events due to modesetting is dealt 192 * with in the DRM core code, as long as drivers call 193 * drm_crtc_vblank_off() and drm_crtc_vblank_on() when disabling or 194 * enabling a CRTC. 195 * 196 * This is deprecated and should not be used by new drivers. 197 * Use &drm_crtc_funcs.get_vblank_counter instead. 198 * 199 * Returns: 200 * 201 * Raw vblank counter value. 202 */ 203 u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe); 204 205 /** 206 * @enable_vblank: 207 * 208 * Enable vblank interrupts for the CRTC specified with the pipe 209 * argument. 210 * 211 * This is deprecated and should not be used by new drivers. 212 * Use &drm_crtc_funcs.enable_vblank instead. 213 * 214 * Returns: 215 * 216 * Zero on success, appropriate errno if the given @crtc's vblank 217 * interrupt cannot be enabled. 218 */ 219 int (*enable_vblank) (struct drm_device *dev, unsigned int pipe); 220 221 /** 222 * @disable_vblank: 223 * 224 * Disable vblank interrupts for the CRTC specified with the pipe 225 * argument. 226 * 227 * This is deprecated and should not be used by new drivers. 228 * Use &drm_crtc_funcs.disable_vblank instead. 229 */ 230 void (*disable_vblank) (struct drm_device *dev, unsigned int pipe); 231 232 /** 233 * @get_scanout_position: 234 * 235 * Called by vblank timestamping code. 236 * 237 * Returns the current display scanout position from a crtc, and an 238 * optional accurate ktime_get() timestamp of when position was 239 * measured. Note that this is a helper callback which is only used if a 240 * driver uses drm_calc_vbltimestamp_from_scanoutpos() for the 241 * @get_vblank_timestamp callback. 242 * 243 * Parameters: 244 * 245 * dev: 246 * DRM device. 247 * pipe: 248 * Id of the crtc to query. 249 * in_vblank_irq: 250 * True when called from drm_crtc_handle_vblank(). Some drivers 251 * need to apply some workarounds for gpu-specific vblank irq quirks 252 * if flag is set. 253 * vpos: 254 * Target location for current vertical scanout position. 255 * hpos: 256 * Target location for current horizontal scanout position. 257 * stime: 258 * Target location for timestamp taken immediately before 259 * scanout position query. Can be NULL to skip timestamp. 260 * etime: 261 * Target location for timestamp taken immediately after 262 * scanout position query. Can be NULL to skip timestamp. 263 * mode: 264 * Current display timings. 265 * 266 * Returns vpos as a positive number while in active scanout area. 267 * Returns vpos as a negative number inside vblank, counting the number 268 * of scanlines to go until end of vblank, e.g., -1 means "one scanline 269 * until start of active scanout / end of vblank." 270 * 271 * Returns: 272 * 273 * True on success, false if a reliable scanout position counter could 274 * not be read out. 275 * 276 * FIXME: 277 * 278 * Since this is a helper to implement @get_vblank_timestamp, we should 279 * move it to &struct drm_crtc_helper_funcs, like all the other 280 * helper-internal hooks. 281 */ 282 bool (*get_scanout_position) (struct drm_device *dev, unsigned int pipe, 283 bool in_vblank_irq, int *vpos, int *hpos, 284 ktime_t *stime, ktime_t *etime, 285 const struct drm_display_mode *mode); 286 287 /** 288 * @get_vblank_timestamp: 289 * 290 * Called by drm_get_last_vbltimestamp(). Should return a precise 291 * timestamp when the most recent VBLANK interval ended or will end. 292 * 293 * Specifically, the timestamp in @vblank_time should correspond as 294 * closely as possible to the time when the first video scanline of 295 * the video frame after the end of VBLANK will start scanning out, 296 * the time immediately after end of the VBLANK interval. If the 297 * @crtc is currently inside VBLANK, this will be a time in the future. 298 * If the @crtc is currently scanning out a frame, this will be the 299 * past start time of the current scanout. This is meant to adhere 300 * to the OpenML OML_sync_control extension specification. 301 * 302 * Paramters: 303 * 304 * dev: 305 * dev DRM device handle. 306 * pipe: 307 * crtc for which timestamp should be returned. 308 * max_error: 309 * Maximum allowable timestamp error in nanoseconds. 310 * Implementation should strive to provide timestamp 311 * with an error of at most max_error nanoseconds. 312 * Returns true upper bound on error for timestamp. 313 * vblank_time: 314 * Target location for returned vblank timestamp. 315 * in_vblank_irq: 316 * True when called from drm_crtc_handle_vblank(). Some drivers 317 * need to apply some workarounds for gpu-specific vblank irq quirks 318 * if flag is set. 319 * 320 * Returns: 321 * 322 * True on success, false on failure, which means the core should 323 * fallback to a simple timestamp taken in drm_crtc_handle_vblank(). 324 * 325 * FIXME: 326 * 327 * We should move this hook to &struct drm_crtc_funcs like all the other 328 * vblank hooks. 329 */ 330 bool (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe, 331 int *max_error, 332 ktime_t *vblank_time, 333 bool in_vblank_irq); 334 335 /** 336 * @irq_handler: 337 * 338 * Interrupt handler called when using drm_irq_install(). Not used by 339 * drivers which implement their own interrupt handling. 340 */ 341 irqreturn_t(*irq_handler) (int irq, void *arg); 342 343 /** 344 * @irq_preinstall: 345 * 346 * Optional callback used by drm_irq_install() which is called before 347 * the interrupt handler is registered. This should be used to clear out 348 * any pending interrupts (from e.g. firmware based drives) and reset 349 * the interrupt handling registers. 350 */ 351 void (*irq_preinstall) (struct drm_device *dev); 352 353 /** 354 * @irq_postinstall: 355 * 356 * Optional callback used by drm_irq_install() which is called after 357 * the interrupt handler is registered. This should be used to enable 358 * interrupt generation in the hardware. 359 */ 360 int (*irq_postinstall) (struct drm_device *dev); 361 362 /** 363 * @irq_uninstall: 364 * 365 * Optional callback used by drm_irq_uninstall() which is called before 366 * the interrupt handler is unregistered. This should be used to disable 367 * interrupt generation in the hardware. 368 */ 369 void (*irq_uninstall) (struct drm_device *dev); 370 371 /** 372 * @master_create: 373 * 374 * Called whenever a new master is created. Only used by vmwgfx. 375 */ 376 int (*master_create)(struct drm_device *dev, struct drm_master *master); 377 378 /** 379 * @master_destroy: 380 * 381 * Called whenever a master is destroyed. Only used by vmwgfx. 382 */ 383 void (*master_destroy)(struct drm_device *dev, struct drm_master *master); 384 385 /** 386 * @master_set: 387 * 388 * Called whenever the minor master is set. Only used by vmwgfx. 389 */ 390 int (*master_set)(struct drm_device *dev, struct drm_file *file_priv, 391 bool from_open); 392 /** 393 * @master_drop: 394 * 395 * Called whenever the minor master is dropped. Only used by vmwgfx. 396 */ 397 void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv); 398 399 /** 400 * @debugfs_init: 401 * 402 * Allows drivers to create driver-specific debugfs files. 403 */ 404 int (*debugfs_init)(struct drm_minor *minor); 405 406 /** 407 * @gem_free_object: deconstructor for drm_gem_objects 408 * 409 * This is deprecated and should not be used by new drivers. Use 410 * @gem_free_object_unlocked instead. 411 */ 412 void (*gem_free_object) (struct drm_gem_object *obj); 413 414 /** 415 * @gem_free_object_unlocked: deconstructor for drm_gem_objects 416 * 417 * This is for drivers which are not encumbered with &drm_device.struct_mutex 418 * legacy locking schemes. Use this hook instead of @gem_free_object. 419 */ 420 void (*gem_free_object_unlocked) (struct drm_gem_object *obj); 421 422 /** 423 * @gem_open_object: 424 * 425 * Driver hook called upon gem handle creation 426 */ 427 int (*gem_open_object) (struct drm_gem_object *, struct drm_file *); 428 429 /** 430 * @gem_close_object: 431 * 432 * Driver hook called upon gem handle release 433 */ 434 void (*gem_close_object) (struct drm_gem_object *, struct drm_file *); 435 436 /** 437 * @gem_create_object: constructor for gem objects 438 * 439 * Hook for allocating the GEM object struct, for use by core 440 * helpers. 441 */ 442 struct drm_gem_object *(*gem_create_object)(struct drm_device *dev, 443 size_t size); 444 445 /* prime: */ 446 /** 447 * @prime_handle_to_fd: 448 * 449 * export handle -> fd (see drm_gem_prime_handle_to_fd() helper) 450 */ 451 int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv, 452 uint32_t handle, uint32_t flags, int *prime_fd); 453 /** 454 * @prime_fd_to_handle: 455 * 456 * import fd -> handle (see drm_gem_prime_fd_to_handle() helper) 457 */ 458 int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv, 459 int prime_fd, uint32_t *handle); 460 /** 461 * @gem_prime_export: 462 * 463 * export GEM -> dmabuf 464 */ 465 struct dma_buf * (*gem_prime_export)(struct drm_device *dev, 466 struct drm_gem_object *obj, int flags); 467 /** 468 * @gem_prime_import: 469 * 470 * import dmabuf -> GEM 471 */ 472 struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev, 473 struct dma_buf *dma_buf); 474 int (*gem_prime_pin)(struct drm_gem_object *obj); 475 void (*gem_prime_unpin)(struct drm_gem_object *obj); 476 struct reservation_object * (*gem_prime_res_obj)( 477 struct drm_gem_object *obj); 478 struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj); 479 struct drm_gem_object *(*gem_prime_import_sg_table)( 480 struct drm_device *dev, 481 struct dma_buf_attachment *attach, 482 struct sg_table *sgt); 483 void *(*gem_prime_vmap)(struct drm_gem_object *obj); 484 void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr); 485 int (*gem_prime_mmap)(struct drm_gem_object *obj, 486 struct vm_area_struct *vma); 487 488 /** 489 * @dumb_create: 490 * 491 * This creates a new dumb buffer in the driver's backing storage manager (GEM, 492 * TTM or something else entirely) and returns the resulting buffer handle. This 493 * handle can then be wrapped up into a framebuffer modeset object. 494 * 495 * Note that userspace is not allowed to use such objects for render 496 * acceleration - drivers must create their own private ioctls for such a use 497 * case. 498 * 499 * Width, height and depth are specified in the &drm_mode_create_dumb 500 * argument. The callback needs to fill the handle, pitch and size for 501 * the created buffer. 502 * 503 * Called by the user via ioctl. 504 * 505 * Returns: 506 * 507 * Zero on success, negative errno on failure. 508 */ 509 int (*dumb_create)(struct drm_file *file_priv, 510 struct drm_device *dev, 511 struct drm_mode_create_dumb *args); 512 /** 513 * @dumb_map_offset: 514 * 515 * Allocate an offset in the drm device node's address space to be able to 516 * memory map a dumb buffer. GEM-based drivers must use 517 * drm_gem_create_mmap_offset() to implement this. 518 * 519 * Called by the user via ioctl. 520 * 521 * Returns: 522 * 523 * Zero on success, negative errno on failure. 524 */ 525 int (*dumb_map_offset)(struct drm_file *file_priv, 526 struct drm_device *dev, uint32_t handle, 527 uint64_t *offset); 528 /** 529 * @dumb_destroy: 530 * 531 * This destroys the userspace handle for the given dumb backing storage buffer. 532 * Since buffer objects must be reference counted in the kernel a buffer object 533 * won't be immediately freed if a framebuffer modeset object still uses it. 534 * 535 * Called by the user via ioctl. 536 * 537 * Returns: 538 * 539 * Zero on success, negative errno on failure. 540 */ 541 int (*dumb_destroy)(struct drm_file *file_priv, 542 struct drm_device *dev, 543 uint32_t handle); 544 545 /** 546 * @gem_vm_ops: Driver private ops for this object 547 */ 548 struct cdev_pager_ops *gem_vm_ops; 549 550 /** @major: driver major number */ 551 int major; 552 /** @minor: driver minor number */ 553 int minor; 554 /** @patchlevel: driver patch level */ 555 int patchlevel; 556 /** @name: driver name */ 557 char *name; 558 /** @desc: driver description */ 559 char *desc; 560 /** @date: driver date */ 561 char *date; 562 563 /** @driver_features: driver features */ 564 u32 driver_features; 565 566 /** 567 * @ioctls: 568 * 569 * Array of driver-private IOCTL description entries. See the chapter on 570 * :ref:`IOCTL support in the userland interfaces 571 * chapter<drm_driver_ioctl>` for the full details. 572 */ 573 574 const struct drm_ioctl_desc *ioctls; 575 /** @num_ioctls: Number of entries in @ioctls. */ 576 int num_ioctls; 577 578 /** 579 * @fops: 580 * 581 * File operations for the DRM device node. See the discussion in 582 * :ref:`file operations<drm_driver_fops>` for in-depth coverage and 583 * some examples. 584 */ 585 const struct file_operations *fops; 586 587 /* Everything below here is for legacy driver, never use! */ 588 /* private: */ 589 590 /* List of devices hanging off this driver with stealth attach. */ 591 struct list_head legacy_dev_list; 592 int (*firstopen) (struct drm_device *); 593 void (*preclose) (struct drm_device *, struct drm_file *file_priv); 594 int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv); 595 int (*dma_quiescent) (struct drm_device *); 596 int (*context_dtor) (struct drm_device *dev, int context); 597 int dev_priv_size; 598 #ifdef __DragonFly__ 599 int (*sysctl_init) (struct drm_device *dev, 600 struct sysctl_ctx_list *ctx, struct sysctl_oid *top); 601 void (*sysctl_cleanup) (struct drm_device *dev); 602 #endif /* __DragonFly__ */ 603 }; 604 605 void drm_dev_printk(const struct device *dev, const char *level, 606 unsigned int category, const char *function_name, 607 const char *prefix, const char *format, ...); 608 void drm_printk(const char *level, unsigned int category, 609 const char *format, ...); 610 extern unsigned int drm_debug; 611 612 int drm_dev_init(struct drm_device *dev, 613 struct drm_driver *driver, 614 struct device *parent); 615 void drm_dev_fini(struct drm_device *dev); 616 617 struct drm_device *drm_dev_alloc(struct drm_driver *driver, 618 struct device *parent); 619 int drm_dev_register(struct drm_device *dev, unsigned long flags); 620 void drm_dev_unregister(struct drm_device *dev); 621 622 void drm_dev_get(struct drm_device *dev); 623 void drm_dev_put(struct drm_device *dev); 624 void drm_dev_unref(struct drm_device *dev); 625 void drm_put_dev(struct drm_device *dev); 626 void drm_dev_unplug(struct drm_device *dev); 627 628 /** 629 * drm_dev_is_unplugged - is a DRM device unplugged 630 * @dev: DRM device 631 * 632 * This function can be called to check whether a hotpluggable is unplugged. 633 * Unplugging itself is singalled through drm_dev_unplug(). If a device is 634 * unplugged, these two functions guarantee that any store before calling 635 * drm_dev_unplug() is visible to callers of this function after it completes 636 */ 637 static inline int drm_dev_is_unplugged(struct drm_device *dev) 638 { 639 int ret = atomic_read(&dev->unplugged); 640 smp_rmb(); 641 return ret; 642 } 643 644 645 int drm_dev_set_unique(struct drm_device *dev, const char *name); 646 647 648 #endif 649