drm_drv.h 17 KB

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