drm_drv.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. struct drm_device;
  31. struct drm_file;
  32. struct drm_gem_object;
  33. struct drm_master;
  34. struct drm_minor;
  35. struct dma_buf_attachment;
  36. struct drm_display_mode;
  37. struct drm_mode_create_dumb;
  38. /* driver capabilities and requirements mask */
  39. #define DRIVER_USE_AGP 0x1
  40. #define DRIVER_LEGACY 0x2
  41. #define DRIVER_PCI_DMA 0x8
  42. #define DRIVER_SG 0x10
  43. #define DRIVER_HAVE_DMA 0x20
  44. #define DRIVER_HAVE_IRQ 0x40
  45. #define DRIVER_IRQ_SHARED 0x80
  46. #define DRIVER_GEM 0x1000
  47. #define DRIVER_MODESET 0x2000
  48. #define DRIVER_PRIME 0x4000
  49. #define DRIVER_RENDER 0x8000
  50. #define DRIVER_ATOMIC 0x10000
  51. #define DRIVER_KMS_LEGACY_CONTEXT 0x20000
  52. #define DRIVER_SYNCOBJ 0x40000
  53. /**
  54. * struct drm_driver - DRM driver structure
  55. *
  56. * This structure represent the common code for a family of cards. There will
  57. * one drm_device for each card present in this family. It contains lots of
  58. * vfunc entries, and a pile of those probably should be moved to more
  59. * appropriate places like &drm_mode_config_funcs or into a new operations
  60. * structure for GEM drivers.
  61. */
  62. struct drm_driver {
  63. /**
  64. * @load:
  65. *
  66. * Backward-compatible driver callback to complete
  67. * initialization steps after the driver is registered. For
  68. * this reason, may suffer from race conditions and its use is
  69. * deprecated for new drivers. It is therefore only supported
  70. * for existing drivers not yet converted to the new scheme.
  71. * See drm_dev_init() and drm_dev_register() for proper and
  72. * race-free way to set up a &struct drm_device.
  73. *
  74. * This is deprecated, do not use!
  75. *
  76. * Returns:
  77. *
  78. * Zero on success, non-zero value on failure.
  79. */
  80. int (*load) (struct drm_device *, unsigned long flags);
  81. /**
  82. * @open:
  83. *
  84. * Driver callback when a new &struct drm_file is opened. Useful for
  85. * setting up driver-private data structures like buffer allocators,
  86. * execution contexts or similar things. Such driver-private resources
  87. * must be released again in @postclose.
  88. *
  89. * Since the display/modeset side of DRM can only be owned by exactly
  90. * one &struct drm_file (see &drm_file.is_master and &drm_device.master)
  91. * there should never be a need to set up any modeset related resources
  92. * in this callback. Doing so would be a driver design bug.
  93. *
  94. * Returns:
  95. *
  96. * 0 on success, a negative error code on failure, which will be
  97. * promoted to userspace as the result of the open() system call.
  98. */
  99. int (*open) (struct drm_device *, struct drm_file *);
  100. /**
  101. * @postclose:
  102. *
  103. * One of the driver callbacks when a new &struct drm_file is closed.
  104. * Useful for tearing down driver-private data structures allocated in
  105. * @open like buffer allocators, execution contexts or similar things.
  106. *
  107. * Since the display/modeset side of DRM can only be owned by exactly
  108. * one &struct drm_file (see &drm_file.is_master and &drm_device.master)
  109. * there should never be a need to tear down any modeset related
  110. * resources in this callback. Doing so would be a driver design bug.
  111. */
  112. void (*postclose) (struct drm_device *, struct drm_file *);
  113. /**
  114. * @lastclose:
  115. *
  116. * Called when the last &struct drm_file has been closed and there's
  117. * currently no userspace client for the &struct drm_device.
  118. *
  119. * Modern drivers should only use this to force-restore the fbdev
  120. * framebuffer using drm_fb_helper_restore_fbdev_mode_unlocked().
  121. * Anything else would indicate there's something seriously wrong.
  122. * Modern drivers can also use this to execute delayed power switching
  123. * state changes, e.g. in conjunction with the :ref:`vga_switcheroo`
  124. * infrastructure.
  125. *
  126. * This is called after @postclose hook has been called.
  127. *
  128. * NOTE:
  129. *
  130. * All legacy drivers use this callback to de-initialize the hardware.
  131. * This is purely because of the shadow-attach model, where the DRM
  132. * kernel driver does not really own the hardware. Instead ownershipe is
  133. * handled with the help of userspace through an inheritedly racy dance
  134. * to set/unset the VT into raw mode.
  135. *
  136. * Legacy drivers initialize the hardware in the @firstopen callback,
  137. * which isn't even called for modern drivers.
  138. */
  139. void (*lastclose) (struct drm_device *);
  140. /**
  141. * @unload:
  142. *
  143. * Reverse the effects of the driver load callback. Ideally,
  144. * the clean up performed by the driver should happen in the
  145. * reverse order of the initialization. Similarly to the load
  146. * hook, this handler is deprecated and its usage should be
  147. * dropped in favor of an open-coded teardown function at the
  148. * driver layer. See drm_dev_unregister() and drm_dev_unref()
  149. * for the proper way to remove a &struct drm_device.
  150. *
  151. * The unload() hook is called right after unregistering
  152. * the device.
  153. *
  154. */
  155. void (*unload) (struct drm_device *);
  156. /**
  157. * @release:
  158. *
  159. * Optional callback for destroying device data after the final
  160. * reference is released, i.e. the device is being destroyed. Drivers
  161. * using this callback are responsible for calling drm_dev_fini()
  162. * to finalize the device and then freeing the struct themselves.
  163. */
  164. void (*release) (struct drm_device *);
  165. int (*set_busid)(struct drm_device *dev, struct drm_master *master);
  166. /**
  167. * @get_vblank_counter:
  168. *
  169. * Driver callback for fetching a raw hardware vblank counter for the
  170. * CRTC specified with the pipe argument. If a device doesn't have a
  171. * hardware counter, the driver can simply leave the hook as NULL.
  172. * The DRM core will account for missed vblank events while interrupts
  173. * where disabled based on system timestamps.
  174. *
  175. * Wraparound handling and loss of events due to modesetting is dealt
  176. * with in the DRM core code, as long as drivers call
  177. * drm_crtc_vblank_off() and drm_crtc_vblank_on() when disabling or
  178. * enabling a CRTC.
  179. *
  180. * This is deprecated and should not be used by new drivers.
  181. * Use &drm_crtc_funcs.get_vblank_counter instead.
  182. *
  183. * Returns:
  184. *
  185. * Raw vblank counter value.
  186. */
  187. u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
  188. /**
  189. * @enable_vblank:
  190. *
  191. * Enable vblank interrupts for the CRTC specified with the pipe
  192. * argument.
  193. *
  194. * This is deprecated and should not be used by new drivers.
  195. * Use &drm_crtc_funcs.enable_vblank instead.
  196. *
  197. * Returns:
  198. *
  199. * Zero on success, appropriate errno if the given @crtc's vblank
  200. * interrupt cannot be enabled.
  201. */
  202. int (*enable_vblank) (struct drm_device *dev, unsigned int pipe);
  203. /**
  204. * @disable_vblank:
  205. *
  206. * Disable vblank interrupts for the CRTC specified with the pipe
  207. * argument.
  208. *
  209. * This is deprecated and should not be used by new drivers.
  210. * Use &drm_crtc_funcs.disable_vblank instead.
  211. */
  212. void (*disable_vblank) (struct drm_device *dev, unsigned int pipe);
  213. /**
  214. * @get_scanout_position:
  215. *
  216. * Called by vblank timestamping code.
  217. *
  218. * Returns the current display scanout position from a crtc, and an
  219. * optional accurate ktime_get() timestamp of when position was
  220. * measured. Note that this is a helper callback which is only used if a
  221. * driver uses drm_calc_vbltimestamp_from_scanoutpos() for the
  222. * @get_vblank_timestamp callback.
  223. *
  224. * Parameters:
  225. *
  226. * dev:
  227. * DRM device.
  228. * pipe:
  229. * Id of the crtc to query.
  230. * in_vblank_irq:
  231. * True when called from drm_crtc_handle_vblank(). Some drivers
  232. * need to apply some workarounds for gpu-specific vblank irq quirks
  233. * if flag is set.
  234. * vpos:
  235. * Target location for current vertical scanout position.
  236. * hpos:
  237. * Target location for current horizontal scanout position.
  238. * stime:
  239. * Target location for timestamp taken immediately before
  240. * scanout position query. Can be NULL to skip timestamp.
  241. * etime:
  242. * Target location for timestamp taken immediately after
  243. * scanout position query. Can be NULL to skip timestamp.
  244. * mode:
  245. * Current display timings.
  246. *
  247. * Returns vpos as a positive number while in active scanout area.
  248. * Returns vpos as a negative number inside vblank, counting the number
  249. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  250. * until start of active scanout / end of vblank."
  251. *
  252. * Returns:
  253. *
  254. * True on success, false if a reliable scanout position counter could
  255. * not be read out.
  256. *
  257. * FIXME:
  258. *
  259. * Since this is a helper to implement @get_vblank_timestamp, we should
  260. * move it to &struct drm_crtc_helper_funcs, like all the other
  261. * helper-internal hooks.
  262. */
  263. bool (*get_scanout_position) (struct drm_device *dev, unsigned int pipe,
  264. bool in_vblank_irq, int *vpos, int *hpos,
  265. ktime_t *stime, ktime_t *etime,
  266. const struct drm_display_mode *mode);
  267. /**
  268. * @get_vblank_timestamp:
  269. *
  270. * Called by drm_get_last_vbltimestamp(). Should return a precise
  271. * timestamp when the most recent VBLANK interval ended or will end.
  272. *
  273. * Specifically, the timestamp in @vblank_time should correspond as
  274. * closely as possible to the time when the first video scanline of
  275. * the video frame after the end of VBLANK will start scanning out,
  276. * the time immediately after end of the VBLANK interval. If the
  277. * @crtc is currently inside VBLANK, this will be a time in the future.
  278. * If the @crtc is currently scanning out a frame, this will be the
  279. * past start time of the current scanout. This is meant to adhere
  280. * to the OpenML OML_sync_control extension specification.
  281. *
  282. * Paramters:
  283. *
  284. * dev:
  285. * dev DRM device handle.
  286. * pipe:
  287. * crtc for which timestamp should be returned.
  288. * max_error:
  289. * Maximum allowable timestamp error in nanoseconds.
  290. * Implementation should strive to provide timestamp
  291. * with an error of at most max_error nanoseconds.
  292. * Returns true upper bound on error for timestamp.
  293. * vblank_time:
  294. * Target location for returned vblank timestamp.
  295. * in_vblank_irq:
  296. * True when called from drm_crtc_handle_vblank(). Some drivers
  297. * need to apply some workarounds for gpu-specific vblank irq quirks
  298. * if flag is set.
  299. *
  300. * Returns:
  301. *
  302. * True on success, false on failure, which means the core should
  303. * fallback to a simple timestamp taken in drm_crtc_handle_vblank().
  304. *
  305. * FIXME:
  306. *
  307. * We should move this hook to &struct drm_crtc_funcs like all the other
  308. * vblank hooks.
  309. */
  310. bool (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe,
  311. int *max_error,
  312. struct timeval *vblank_time,
  313. bool in_vblank_irq);
  314. /**
  315. * @irq_handler:
  316. *
  317. * Interrupt handler called when using drm_irq_install(). Not used by
  318. * drivers which implement their own interrupt handling.
  319. */
  320. irqreturn_t(*irq_handler) (int irq, void *arg);
  321. /**
  322. * @irq_preinstall:
  323. *
  324. * Optional callback used by drm_irq_install() which is called before
  325. * the interrupt handler is registered. This should be used to clear out
  326. * any pending interrupts (from e.g. firmware based drives) and reset
  327. * the interrupt handling registers.
  328. */
  329. void (*irq_preinstall) (struct drm_device *dev);
  330. /**
  331. * @irq_postinstall:
  332. *
  333. * Optional callback used by drm_irq_install() which is called after
  334. * the interrupt handler is registered. This should be used to enable
  335. * interrupt generation in the hardware.
  336. */
  337. int (*irq_postinstall) (struct drm_device *dev);
  338. /**
  339. * @irq_uninstall:
  340. *
  341. * Optional callback used by drm_irq_uninstall() which is called before
  342. * the interrupt handler is unregistered. This should be used to disable
  343. * interrupt generation in the hardware.
  344. */
  345. void (*irq_uninstall) (struct drm_device *dev);
  346. /**
  347. * @master_create:
  348. *
  349. * Called whenever a new master is created. Only used by vmwgfx.
  350. */
  351. int (*master_create)(struct drm_device *dev, struct drm_master *master);
  352. /**
  353. * @master_destroy:
  354. *
  355. * Called whenever a master is destroyed. Only used by vmwgfx.
  356. */
  357. void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
  358. /**
  359. * @master_set:
  360. *
  361. * Called whenever the minor master is set. Only used by vmwgfx.
  362. */
  363. int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
  364. bool from_open);
  365. /**
  366. * @master_drop:
  367. *
  368. * Called whenever the minor master is dropped. Only used by vmwgfx.
  369. */
  370. void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv);
  371. int (*debugfs_init)(struct drm_minor *minor);
  372. /**
  373. * @gem_free_object: deconstructor for drm_gem_objects
  374. *
  375. * This is deprecated and should not be used by new drivers. Use
  376. * @gem_free_object_unlocked instead.
  377. */
  378. void (*gem_free_object) (struct drm_gem_object *obj);
  379. /**
  380. * @gem_free_object_unlocked: deconstructor for drm_gem_objects
  381. *
  382. * This is for drivers which are not encumbered with &drm_device.struct_mutex
  383. * legacy locking schemes. Use this hook instead of @gem_free_object.
  384. */
  385. void (*gem_free_object_unlocked) (struct drm_gem_object *obj);
  386. int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
  387. void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
  388. /**
  389. * @gem_create_object: constructor for gem objects
  390. *
  391. * Hook for allocating the GEM object struct, for use by core
  392. * helpers.
  393. */
  394. struct drm_gem_object *(*gem_create_object)(struct drm_device *dev,
  395. size_t size);
  396. /* prime: */
  397. /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
  398. int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
  399. uint32_t handle, uint32_t flags, int *prime_fd);
  400. /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
  401. int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
  402. int prime_fd, uint32_t *handle);
  403. /* export GEM -> dmabuf */
  404. struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
  405. struct drm_gem_object *obj, int flags);
  406. /* import dmabuf -> GEM */
  407. struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
  408. struct dma_buf *dma_buf);
  409. /* low-level interface used by drm_gem_prime_{import,export} */
  410. int (*gem_prime_pin)(struct drm_gem_object *obj);
  411. void (*gem_prime_unpin)(struct drm_gem_object *obj);
  412. struct reservation_object * (*gem_prime_res_obj)(
  413. struct drm_gem_object *obj);
  414. struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
  415. struct drm_gem_object *(*gem_prime_import_sg_table)(
  416. struct drm_device *dev,
  417. struct dma_buf_attachment *attach,
  418. struct sg_table *sgt);
  419. void *(*gem_prime_vmap)(struct drm_gem_object *obj);
  420. void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
  421. int (*gem_prime_mmap)(struct drm_gem_object *obj,
  422. struct vm_area_struct *vma);
  423. /**
  424. * @dumb_create:
  425. *
  426. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  427. * TTM or something else entirely) and returns the resulting buffer handle. This
  428. * handle can then be wrapped up into a framebuffer modeset object.
  429. *
  430. * Note that userspace is not allowed to use such objects for render
  431. * acceleration - drivers must create their own private ioctls for such a use
  432. * case.
  433. *
  434. * Width, height and depth are specified in the &drm_mode_create_dumb
  435. * argument. The callback needs to fill the handle, pitch and size for
  436. * the created buffer.
  437. *
  438. * Called by the user via ioctl.
  439. *
  440. * Returns:
  441. *
  442. * Zero on success, negative errno on failure.
  443. */
  444. int (*dumb_create)(struct drm_file *file_priv,
  445. struct drm_device *dev,
  446. struct drm_mode_create_dumb *args);
  447. /**
  448. * @dumb_map_offset:
  449. *
  450. * Allocate an offset in the drm device node's address space to be able to
  451. * memory map a dumb buffer. GEM-based drivers must use
  452. * drm_gem_create_mmap_offset() to implement this.
  453. *
  454. * Called by the user via ioctl.
  455. *
  456. * Returns:
  457. *
  458. * Zero on success, negative errno on failure.
  459. */
  460. int (*dumb_map_offset)(struct drm_file *file_priv,
  461. struct drm_device *dev, uint32_t handle,
  462. uint64_t *offset);
  463. /**
  464. * @dumb_destroy:
  465. *
  466. * This destroys the userspace handle for the given dumb backing storage buffer.
  467. * Since buffer objects must be reference counted in the kernel a buffer object
  468. * won't be immediately freed if a framebuffer modeset object still uses it.
  469. *
  470. * Called by the user via ioctl.
  471. *
  472. * Returns:
  473. *
  474. * Zero on success, negative errno on failure.
  475. */
  476. int (*dumb_destroy)(struct drm_file *file_priv,
  477. struct drm_device *dev,
  478. uint32_t handle);
  479. /* Driver private ops for this object */
  480. const struct vm_operations_struct *gem_vm_ops;
  481. int major;
  482. int minor;
  483. int patchlevel;
  484. char *name;
  485. char *desc;
  486. char *date;
  487. u32 driver_features;
  488. const struct drm_ioctl_desc *ioctls;
  489. int num_ioctls;
  490. const struct file_operations *fops;
  491. /* Everything below here is for legacy driver, never use! */
  492. /* private: */
  493. /* List of devices hanging off this driver with stealth attach. */
  494. struct list_head legacy_dev_list;
  495. int (*firstopen) (struct drm_device *);
  496. void (*preclose) (struct drm_device *, struct drm_file *file_priv);
  497. int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
  498. int (*dma_quiescent) (struct drm_device *);
  499. int (*context_dtor) (struct drm_device *dev, int context);
  500. int dev_priv_size;
  501. };
  502. __printf(6, 7)
  503. void drm_dev_printk(const struct device *dev, const char *level,
  504. unsigned int category, const char *function_name,
  505. const char *prefix, const char *format, ...);
  506. __printf(3, 4)
  507. void drm_printk(const char *level, unsigned int category,
  508. const char *format, ...);
  509. extern unsigned int drm_debug;
  510. int drm_dev_init(struct drm_device *dev,
  511. struct drm_driver *driver,
  512. struct device *parent);
  513. void drm_dev_fini(struct drm_device *dev);
  514. struct drm_device *drm_dev_alloc(struct drm_driver *driver,
  515. struct device *parent);
  516. int drm_dev_register(struct drm_device *dev, unsigned long flags);
  517. void drm_dev_unregister(struct drm_device *dev);
  518. void drm_dev_ref(struct drm_device *dev);
  519. void drm_dev_unref(struct drm_device *dev);
  520. void drm_put_dev(struct drm_device *dev);
  521. void drm_unplug_dev(struct drm_device *dev);
  522. int drm_dev_set_unique(struct drm_device *dev, const char *name);
  523. #endif