drm_drv.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. #ifndef _DRM_DRV_H_
  27. #define _DRM_DRV_H_
  28. #include <linux/list.h>
  29. #include <linux/irqreturn.h>
  30. #include <drm/drm_device.h>
  31. struct drm_file;
  32. struct drm_gem_object;
  33. struct drm_master;
  34. struct drm_minor;
  35. struct dma_buf;
  36. struct dma_buf_attachment;
  37. struct drm_display_mode;
  38. struct drm_mode_create_dumb;
  39. struct drm_printer;
  40. struct sg_table;
  41. /**
  42. * enum drm_driver_feature - feature flags
  43. *
  44. * See &drm_driver.driver_features, drm_device.driver_features and
  45. * drm_core_check_feature().
  46. */
  47. enum drm_driver_feature {
  48. /**
  49. * @DRIVER_GEM:
  50. *
  51. * Driver use the GEM memory manager. This should be set for all modern
  52. * drivers.
  53. */
  54. DRIVER_GEM = BIT(0),
  55. /**
  56. * @DRIVER_MODESET:
  57. *
  58. * Driver supports mode setting interfaces (KMS).
  59. */
  60. DRIVER_MODESET = BIT(1),
  61. /**
  62. * @DRIVER_RENDER:
  63. *
  64. * Driver supports dedicated render nodes. See also the :ref:`section on
  65. * render nodes <drm_render_node>` for details.
  66. */
  67. DRIVER_RENDER = BIT(3),
  68. /**
  69. * @DRIVER_ATOMIC:
  70. *
  71. * Driver supports the full atomic modesetting userspace API. Drivers
  72. * which only use atomic internally, but do not support the full
  73. * userspace API (e.g. not all properties converted to atomic, or
  74. * multi-plane updates are not guaranteed to be tear-free) should not
  75. * set this flag.
  76. */
  77. DRIVER_ATOMIC = BIT(4),
  78. /**
  79. * @DRIVER_SYNCOBJ:
  80. *
  81. * Driver supports &drm_syncobj for explicit synchronization of command
  82. * submission.
  83. */
  84. DRIVER_SYNCOBJ = BIT(5),
  85. /**
  86. * @DRIVER_SYNCOBJ_TIMELINE:
  87. *
  88. * Driver supports the timeline flavor of &drm_syncobj for explicit
  89. * synchronization of command submission.
  90. */
  91. DRIVER_SYNCOBJ_TIMELINE = BIT(6),
  92. /* IMPORTANT: Below are all the legacy flags, add new ones above. */
  93. /**
  94. * @DRIVER_USE_AGP:
  95. *
  96. * Set up DRM AGP support, see drm_agp_init(), the DRM core will manage
  97. * AGP resources. New drivers don't need this.
  98. */
  99. DRIVER_USE_AGP = BIT(25),
  100. /**
  101. * @DRIVER_LEGACY:
  102. *
  103. * Denote a legacy driver using shadow attach. Do not use.
  104. */
  105. DRIVER_LEGACY = BIT(26),
  106. /**
  107. * @DRIVER_PCI_DMA:
  108. *
  109. * Driver is capable of PCI DMA, mapping of PCI DMA buffers to userspace
  110. * will be enabled. Only for legacy drivers. Do not use.
  111. */
  112. DRIVER_PCI_DMA = BIT(27),
  113. /**
  114. * @DRIVER_SG:
  115. *
  116. * Driver can perform scatter/gather DMA, allocation and mapping of
  117. * scatter/gather buffers will be enabled. Only for legacy drivers. Do
  118. * not use.
  119. */
  120. DRIVER_SG = BIT(28),
  121. /**
  122. * @DRIVER_HAVE_DMA:
  123. *
  124. * Driver supports DMA, the userspace DMA API will be supported. Only
  125. * for legacy drivers. Do not use.
  126. */
  127. DRIVER_HAVE_DMA = BIT(29),
  128. /**
  129. * @DRIVER_HAVE_IRQ:
  130. *
  131. * Legacy irq support. Only for legacy drivers. Do not use.
  132. */
  133. DRIVER_HAVE_IRQ = BIT(30),
  134. /**
  135. * @DRIVER_KMS_LEGACY_CONTEXT:
  136. *
  137. * Used only by nouveau for backwards compatibility with existing
  138. * userspace. Do not use.
  139. */
  140. DRIVER_KMS_LEGACY_CONTEXT = BIT(31),
  141. };
  142. /**
  143. * struct drm_driver - DRM driver structure
  144. *
  145. * This structure represent the common code for a family of cards. There will be
  146. * one &struct drm_device for each card present in this family. It contains lots
  147. * of vfunc entries, and a pile of those probably should be moved to more
  148. * appropriate places like &drm_mode_config_funcs or into a new operations
  149. * structure for GEM drivers.
  150. */
  151. struct drm_driver {
  152. /**
  153. * @load:
  154. *
  155. * Backward-compatible driver callback to complete initialization steps
  156. * after the driver is registered. For this reason, may suffer from
  157. * race conditions and its use is deprecated for new drivers. It is
  158. * therefore only supported for existing drivers not yet converted to
  159. * the new scheme. See devm_drm_dev_alloc() and drm_dev_register() for
  160. * proper and race-free way to set up a &struct drm_device.
  161. *
  162. * This is deprecated, do not use!
  163. *
  164. * Returns:
  165. *
  166. * Zero on success, non-zero value on failure.
  167. */
  168. int (*load) (struct drm_device *, unsigned long flags);
  169. /**
  170. * @open:
  171. *
  172. * Driver callback when a new &struct drm_file is opened. Useful for
  173. * setting up driver-private data structures like buffer allocators,
  174. * execution contexts or similar things. Such driver-private resources
  175. * must be released again in @postclose.
  176. *
  177. * Since the display/modeset side of DRM can only be owned by exactly
  178. * one &struct drm_file (see &drm_file.is_master and &drm_device.master)
  179. * there should never be a need to set up any modeset related resources
  180. * in this callback. Doing so would be a driver design bug.
  181. *
  182. * Returns:
  183. *
  184. * 0 on success, a negative error code on failure, which will be
  185. * promoted to userspace as the result of the open() system call.
  186. */
  187. int (*open) (struct drm_device *, struct drm_file *);
  188. /**
  189. * @postclose:
  190. *
  191. * One of the driver callbacks when a new &struct drm_file is closed.
  192. * Useful for tearing down driver-private data structures allocated in
  193. * @open like buffer allocators, execution contexts or similar things.
  194. *
  195. * Since the display/modeset side of DRM can only be owned by exactly
  196. * one &struct drm_file (see &drm_file.is_master and &drm_device.master)
  197. * there should never be a need to tear down any modeset related
  198. * resources in this callback. Doing so would be a driver design bug.
  199. */
  200. void (*postclose) (struct drm_device *, struct drm_file *);
  201. /**
  202. * @lastclose:
  203. *
  204. * Called when the last &struct drm_file has been closed and there's
  205. * currently no userspace client for the &struct drm_device.
  206. *
  207. * Modern drivers should only use this to force-restore the fbdev
  208. * framebuffer using drm_fb_helper_restore_fbdev_mode_unlocked().
  209. * Anything else would indicate there's something seriously wrong.
  210. * Modern drivers can also use this to execute delayed power switching
  211. * state changes, e.g. in conjunction with the :ref:`vga_switcheroo`
  212. * infrastructure.
  213. *
  214. * This is called after @postclose hook has been called.
  215. *
  216. * NOTE:
  217. *
  218. * All legacy drivers use this callback to de-initialize the hardware.
  219. * This is purely because of the shadow-attach model, where the DRM
  220. * kernel driver does not really own the hardware. Instead ownershipe is
  221. * handled with the help of userspace through an inheritedly racy dance
  222. * to set/unset the VT into raw mode.
  223. *
  224. * Legacy drivers initialize the hardware in the @firstopen callback,
  225. * which isn't even called for modern drivers.
  226. */
  227. void (*lastclose) (struct drm_device *);
  228. /**
  229. * @unload:
  230. *
  231. * Reverse the effects of the driver load callback. Ideally,
  232. * the clean up performed by the driver should happen in the
  233. * reverse order of the initialization. Similarly to the load
  234. * hook, this handler is deprecated and its usage should be
  235. * dropped in favor of an open-coded teardown function at the
  236. * driver layer. See drm_dev_unregister() and drm_dev_put()
  237. * for the proper way to remove a &struct drm_device.
  238. *
  239. * The unload() hook is called right after unregistering
  240. * the device.
  241. *
  242. */
  243. void (*unload) (struct drm_device *);
  244. /**
  245. * @release:
  246. *
  247. * Optional callback for destroying device data after the final
  248. * reference is released, i.e. the device is being destroyed.
  249. *
  250. * This is deprecated, clean up all memory allocations associated with a
  251. * &drm_device using drmm_add_action(), drmm_kmalloc() and related
  252. * managed resources functions.
  253. */
  254. void (*release) (struct drm_device *);
  255. /**
  256. * @master_set:
  257. *
  258. * Called whenever the minor master is set. Only used by vmwgfx.
  259. */
  260. void (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
  261. bool from_open);
  262. /**
  263. * @master_drop:
  264. *
  265. * Called whenever the minor master is dropped. Only used by vmwgfx.
  266. */
  267. void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv);
  268. /**
  269. * @debugfs_init:
  270. *
  271. * Allows drivers to create driver-specific debugfs files.
  272. */
  273. void (*debugfs_init)(struct drm_minor *minor);
  274. /**
  275. * @gem_create_object: constructor for gem objects
  276. *
  277. * Hook for allocating the GEM object struct, for use by the CMA
  278. * and SHMEM GEM helpers. Returns a GEM object on success, or an
  279. * ERR_PTR()-encoded error code otherwise.
  280. */
  281. struct drm_gem_object *(*gem_create_object)(struct drm_device *dev,
  282. size_t size);
  283. /**
  284. * @prime_handle_to_fd:
  285. *
  286. * Main PRIME export function. Should be implemented with
  287. * drm_gem_prime_handle_to_fd() for GEM based drivers.
  288. *
  289. * For an in-depth discussion see :ref:`PRIME buffer sharing
  290. * documentation <prime_buffer_sharing>`.
  291. */
  292. int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
  293. uint32_t handle, uint32_t flags, int *prime_fd);
  294. /**
  295. * @prime_fd_to_handle:
  296. *
  297. * Main PRIME import function. Should be implemented with
  298. * drm_gem_prime_fd_to_handle() for GEM based drivers.
  299. *
  300. * For an in-depth discussion see :ref:`PRIME buffer sharing
  301. * documentation <prime_buffer_sharing>`.
  302. */
  303. int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
  304. int prime_fd, uint32_t *handle);
  305. /**
  306. * @gem_prime_import:
  307. *
  308. * Import hook for GEM drivers.
  309. *
  310. * This defaults to drm_gem_prime_import() if not set.
  311. */
  312. struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
  313. struct dma_buf *dma_buf);
  314. /**
  315. * @gem_prime_import_sg_table:
  316. *
  317. * Optional hook used by the PRIME helper functions
  318. * drm_gem_prime_import() respectively drm_gem_prime_import_dev().
  319. */
  320. struct drm_gem_object *(*gem_prime_import_sg_table)(
  321. struct drm_device *dev,
  322. struct dma_buf_attachment *attach,
  323. struct sg_table *sgt);
  324. /**
  325. * @gem_prime_mmap:
  326. *
  327. * mmap hook for GEM drivers, used to implement dma-buf mmap in the
  328. * PRIME helpers.
  329. *
  330. * This hook only exists for historical reasons. Drivers must use
  331. * drm_gem_prime_mmap() to implement it.
  332. *
  333. * FIXME: Convert all drivers to implement mmap in struct
  334. * &drm_gem_object_funcs and inline drm_gem_prime_mmap() into
  335. * its callers. This hook should be removed afterwards.
  336. */
  337. int (*gem_prime_mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
  338. /**
  339. * @dumb_create:
  340. *
  341. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  342. * TTM or something else entirely) and returns the resulting buffer handle. This
  343. * handle can then be wrapped up into a framebuffer modeset object.
  344. *
  345. * Note that userspace is not allowed to use such objects for render
  346. * acceleration - drivers must create their own private ioctls for such a use
  347. * case.
  348. *
  349. * Width, height and depth are specified in the &drm_mode_create_dumb
  350. * argument. The callback needs to fill the handle, pitch and size for
  351. * the created buffer.
  352. *
  353. * Called by the user via ioctl.
  354. *
  355. * Returns:
  356. *
  357. * Zero on success, negative errno on failure.
  358. */
  359. int (*dumb_create)(struct drm_file *file_priv,
  360. struct drm_device *dev,
  361. struct drm_mode_create_dumb *args);
  362. /**
  363. * @dumb_map_offset:
  364. *
  365. * Allocate an offset in the drm device node's address space to be able to
  366. * memory map a dumb buffer.
  367. *
  368. * The default implementation is drm_gem_create_mmap_offset(). GEM based
  369. * drivers must not overwrite this.
  370. *
  371. * Called by the user via ioctl.
  372. *
  373. * Returns:
  374. *
  375. * Zero on success, negative errno on failure.
  376. */
  377. int (*dumb_map_offset)(struct drm_file *file_priv,
  378. struct drm_device *dev, uint32_t handle,
  379. uint64_t *offset);
  380. /**
  381. * @dumb_destroy:
  382. *
  383. * This destroys the userspace handle for the given dumb backing storage buffer.
  384. * Since buffer objects must be reference counted in the kernel a buffer object
  385. * won't be immediately freed if a framebuffer modeset object still uses it.
  386. *
  387. * Called by the user via ioctl.
  388. *
  389. * The default implementation is drm_gem_dumb_destroy(). GEM based drivers
  390. * must not overwrite this.
  391. *
  392. * Returns:
  393. *
  394. * Zero on success, negative errno on failure.
  395. */
  396. int (*dumb_destroy)(struct drm_file *file_priv,
  397. struct drm_device *dev,
  398. uint32_t handle);
  399. /** @major: driver major number */
  400. int major;
  401. /** @minor: driver minor number */
  402. int minor;
  403. /** @patchlevel: driver patch level */
  404. int patchlevel;
  405. /** @name: driver name */
  406. char *name;
  407. /** @desc: driver description */
  408. char *desc;
  409. /** @date: driver date */
  410. char *date;
  411. /**
  412. * @driver_features:
  413. * Driver features, see &enum drm_driver_feature. Drivers can disable
  414. * some features on a per-instance basis using
  415. * &drm_device.driver_features.
  416. */
  417. u32 driver_features;
  418. /**
  419. * @ioctls:
  420. *
  421. * Array of driver-private IOCTL description entries. See the chapter on
  422. * :ref:`IOCTL support in the userland interfaces
  423. * chapter<drm_driver_ioctl>` for the full details.
  424. */
  425. const struct drm_ioctl_desc *ioctls;
  426. /** @num_ioctls: Number of entries in @ioctls. */
  427. int num_ioctls;
  428. /**
  429. * @fops:
  430. *
  431. * File operations for the DRM device node. See the discussion in
  432. * :ref:`file operations<drm_driver_fops>` for in-depth coverage and
  433. * some examples.
  434. */
  435. const struct file_operations *fops;
  436. #ifdef CONFIG_DRM_LEGACY
  437. /* Everything below here is for legacy driver, never use! */
  438. /* private: */
  439. int (*firstopen) (struct drm_device *);
  440. void (*preclose) (struct drm_device *, struct drm_file *file_priv);
  441. int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
  442. int (*dma_quiescent) (struct drm_device *);
  443. int (*context_dtor) (struct drm_device *dev, int context);
  444. irqreturn_t (*irq_handler)(int irq, void *arg);
  445. void (*irq_preinstall)(struct drm_device *dev);
  446. int (*irq_postinstall)(struct drm_device *dev);
  447. void (*irq_uninstall)(struct drm_device *dev);
  448. u32 (*get_vblank_counter)(struct drm_device *dev, unsigned int pipe);
  449. int (*enable_vblank)(struct drm_device *dev, unsigned int pipe);
  450. void (*disable_vblank)(struct drm_device *dev, unsigned int pipe);
  451. int dev_priv_size;
  452. #endif
  453. };
  454. void *__devm_drm_dev_alloc(struct device *parent,
  455. const struct drm_driver *driver,
  456. size_t size, size_t offset);
  457. /**
  458. * devm_drm_dev_alloc - Resource managed allocation of a &drm_device instance
  459. * @parent: Parent device object
  460. * @driver: DRM driver
  461. * @type: the type of the struct which contains struct &drm_device
  462. * @member: the name of the &drm_device within @type.
  463. *
  464. * This allocates and initialize a new DRM device. No device registration is done.
  465. * Call drm_dev_register() to advertice the device to user space and register it
  466. * with other core subsystems. This should be done last in the device
  467. * initialization sequence to make sure userspace can't access an inconsistent
  468. * state.
  469. *
  470. * The initial ref-count of the object is 1. Use drm_dev_get() and
  471. * drm_dev_put() to take and drop further ref-counts.
  472. *
  473. * It is recommended that drivers embed &struct drm_device into their own device
  474. * structure.
  475. *
  476. * Note that this manages the lifetime of the resulting &drm_device
  477. * automatically using devres. The DRM device initialized with this function is
  478. * automatically put on driver detach using drm_dev_put().
  479. *
  480. * RETURNS:
  481. * Pointer to new DRM device, or ERR_PTR on failure.
  482. */
  483. #define devm_drm_dev_alloc(parent, driver, type, member) \
  484. ((type *) __devm_drm_dev_alloc(parent, driver, sizeof(type), \
  485. offsetof(type, member)))
  486. struct drm_device *drm_dev_alloc(const struct drm_driver *driver,
  487. struct device *parent);
  488. int drm_dev_register(struct drm_device *dev, unsigned long flags);
  489. void drm_dev_unregister(struct drm_device *dev);
  490. void drm_dev_get(struct drm_device *dev);
  491. void drm_dev_put(struct drm_device *dev);
  492. void drm_put_dev(struct drm_device *dev);
  493. bool drm_dev_enter(struct drm_device *dev, int *idx);
  494. void drm_dev_exit(int idx);
  495. void drm_dev_unplug(struct drm_device *dev);
  496. /**
  497. * drm_dev_is_unplugged - is a DRM device unplugged
  498. * @dev: DRM device
  499. *
  500. * This function can be called to check whether a hotpluggable is unplugged.
  501. * Unplugging itself is singalled through drm_dev_unplug(). If a device is
  502. * unplugged, these two functions guarantee that any store before calling
  503. * drm_dev_unplug() is visible to callers of this function after it completes
  504. *
  505. * WARNING: This function fundamentally races against drm_dev_unplug(). It is
  506. * recommended that drivers instead use the underlying drm_dev_enter() and
  507. * drm_dev_exit() function pairs.
  508. */
  509. static inline bool drm_dev_is_unplugged(struct drm_device *dev)
  510. {
  511. int idx;
  512. if (drm_dev_enter(dev, &idx)) {
  513. drm_dev_exit(idx);
  514. return false;
  515. }
  516. return true;
  517. }
  518. /**
  519. * drm_core_check_all_features - check driver feature flags mask
  520. * @dev: DRM device to check
  521. * @features: feature flag(s) mask
  522. *
  523. * This checks @dev for driver features, see &drm_driver.driver_features,
  524. * &drm_device.driver_features, and the various &enum drm_driver_feature flags.
  525. *
  526. * Returns true if all features in the @features mask are supported, false
  527. * otherwise.
  528. */
  529. static inline bool drm_core_check_all_features(const struct drm_device *dev,
  530. u32 features)
  531. {
  532. u32 supported = dev->driver->driver_features & dev->driver_features;
  533. return features && (supported & features) == features;
  534. }
  535. /**
  536. * drm_core_check_feature - check driver feature flags
  537. * @dev: DRM device to check
  538. * @feature: feature flag
  539. *
  540. * This checks @dev for driver features, see &drm_driver.driver_features,
  541. * &drm_device.driver_features, and the various &enum drm_driver_feature flags.
  542. *
  543. * Returns true if the @feature is supported, false otherwise.
  544. */
  545. static inline bool drm_core_check_feature(const struct drm_device *dev,
  546. enum drm_driver_feature feature)
  547. {
  548. return drm_core_check_all_features(dev, feature);
  549. }
  550. /**
  551. * drm_drv_uses_atomic_modeset - check if the driver implements
  552. * atomic_commit()
  553. * @dev: DRM device
  554. *
  555. * This check is useful if drivers do not have DRIVER_ATOMIC set but
  556. * have atomic modesetting internally implemented.
  557. */
  558. static inline bool drm_drv_uses_atomic_modeset(struct drm_device *dev)
  559. {
  560. return drm_core_check_feature(dev, DRIVER_ATOMIC) ||
  561. (dev->mode_config.funcs && dev->mode_config.funcs->atomic_commit != NULL);
  562. }
  563. int drm_dev_set_unique(struct drm_device *dev, const char *name);
  564. extern bool drm_firmware_drivers_only(void);
  565. #endif