drm_framebuffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (c) 2016 Intel Corporation
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #ifndef __DRM_FRAMEBUFFER_H__
  23. #define __DRM_FRAMEBUFFER_H__
  24. #include <linux/bits.h>
  25. #include <linux/ctype.h>
  26. #include <linux/list.h>
  27. #include <linux/sched.h>
  28. #include <drm/drm_fourcc.h>
  29. #include <drm/drm_mode_object.h>
  30. struct drm_clip_rect;
  31. struct drm_device;
  32. struct drm_file;
  33. struct drm_framebuffer;
  34. struct drm_gem_object;
  35. /**
  36. * struct drm_framebuffer_funcs - framebuffer hooks
  37. */
  38. struct drm_framebuffer_funcs {
  39. /**
  40. * @destroy:
  41. *
  42. * Clean up framebuffer resources, specifically also unreference the
  43. * backing storage. The core guarantees to call this function for every
  44. * framebuffer successfully created by calling
  45. * &drm_mode_config_funcs.fb_create. Drivers must also call
  46. * drm_framebuffer_cleanup() to release DRM core resources for this
  47. * framebuffer.
  48. */
  49. void (*destroy)(struct drm_framebuffer *framebuffer);
  50. /**
  51. * @create_handle:
  52. *
  53. * Create a buffer handle in the driver-specific buffer manager (either
  54. * GEM or TTM) valid for the passed-in &struct drm_file. This is used by
  55. * the core to implement the GETFB IOCTL, which returns (for
  56. * sufficiently priviledged user) also a native buffer handle. This can
  57. * be used for seamless transitions between modesetting clients by
  58. * copying the current screen contents to a private buffer and blending
  59. * between that and the new contents.
  60. *
  61. * GEM based drivers should call drm_gem_handle_create() to create the
  62. * handle.
  63. *
  64. * RETURNS:
  65. *
  66. * 0 on success or a negative error code on failure.
  67. */
  68. int (*create_handle)(struct drm_framebuffer *fb,
  69. struct drm_file *file_priv,
  70. unsigned int *handle);
  71. /**
  72. * @dirty:
  73. *
  74. * Optional callback for the dirty fb IOCTL.
  75. *
  76. * Userspace can notify the driver via this callback that an area of the
  77. * framebuffer has changed and should be flushed to the display
  78. * hardware. This can also be used internally, e.g. by the fbdev
  79. * emulation, though that's not the case currently.
  80. *
  81. * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd
  82. * for more information as all the semantics and arguments have a one to
  83. * one mapping on this function.
  84. *
  85. * Atomic drivers should use drm_atomic_helper_dirtyfb() to implement
  86. * this hook.
  87. *
  88. * RETURNS:
  89. *
  90. * 0 on success or a negative error code on failure.
  91. */
  92. int (*dirty)(struct drm_framebuffer *framebuffer,
  93. struct drm_file *file_priv, unsigned flags,
  94. unsigned color, struct drm_clip_rect *clips,
  95. unsigned num_clips);
  96. };
  97. #define DRM_FRAMEBUFFER_HAS_HANDLE_REF(_i) BIT(0u + (_i))
  98. /**
  99. * struct drm_framebuffer - frame buffer object
  100. *
  101. * Note that the fb is refcounted for the benefit of driver internals,
  102. * for example some hw, disabling a CRTC/plane is asynchronous, and
  103. * scanout does not actually complete until the next vblank. So some
  104. * cleanup (like releasing the reference(s) on the backing GEM bo(s))
  105. * should be deferred. In cases like this, the driver would like to
  106. * hold a ref to the fb even though it has already been removed from
  107. * userspace perspective. See drm_framebuffer_get() and
  108. * drm_framebuffer_put().
  109. *
  110. * The refcount is stored inside the mode object @base.
  111. */
  112. struct drm_framebuffer {
  113. /**
  114. * @dev: DRM device this framebuffer belongs to
  115. */
  116. struct drm_device *dev;
  117. /**
  118. * @head: Place on the &drm_mode_config.fb_list, access protected by
  119. * &drm_mode_config.fb_lock.
  120. */
  121. struct list_head head;
  122. /**
  123. * @base: base modeset object structure, contains the reference count.
  124. */
  125. struct drm_mode_object base;
  126. /**
  127. * @comm: Name of the process allocating the fb, used for fb dumping.
  128. */
  129. char comm[TASK_COMM_LEN];
  130. /**
  131. * @format: framebuffer format information
  132. */
  133. const struct drm_format_info *format;
  134. /**
  135. * @funcs: framebuffer vfunc table
  136. */
  137. const struct drm_framebuffer_funcs *funcs;
  138. /**
  139. * @pitches: Line stride per buffer. For userspace created object this
  140. * is copied from drm_mode_fb_cmd2.
  141. */
  142. unsigned int pitches[DRM_FORMAT_MAX_PLANES];
  143. /**
  144. * @offsets: Offset from buffer start to the actual pixel data in bytes,
  145. * per buffer. For userspace created object this is copied from
  146. * drm_mode_fb_cmd2.
  147. *
  148. * Note that this is a linear offset and does not take into account
  149. * tiling or buffer layout per @modifier. It is meant to be used when
  150. * the actual pixel data for this framebuffer plane starts at an offset,
  151. * e.g. when multiple planes are allocated within the same backing
  152. * storage buffer object. For tiled layouts this generally means its
  153. * @offsets must at least be tile-size aligned, but hardware often has
  154. * stricter requirements.
  155. *
  156. * This should not be used to specifiy x/y pixel offsets into the buffer
  157. * data (even for linear buffers). Specifying an x/y pixel offset is
  158. * instead done through the source rectangle in &struct drm_plane_state.
  159. */
  160. unsigned int offsets[DRM_FORMAT_MAX_PLANES];
  161. /**
  162. * @modifier: Data layout modifier. This is used to describe
  163. * tiling, or also special layouts (like compression) of auxiliary
  164. * buffers. For userspace created object this is copied from
  165. * drm_mode_fb_cmd2.
  166. */
  167. uint64_t modifier;
  168. /**
  169. * @width: Logical width of the visible area of the framebuffer, in
  170. * pixels.
  171. */
  172. unsigned int width;
  173. /**
  174. * @height: Logical height of the visible area of the framebuffer, in
  175. * pixels.
  176. */
  177. unsigned int height;
  178. /**
  179. * @flags: Framebuffer flags like DRM_MODE_FB_INTERLACED or
  180. * DRM_MODE_FB_MODIFIERS.
  181. */
  182. int flags;
  183. /**
  184. * @internal_flags: Framebuffer flags like DRM_FRAMEBUFFER_HAS_HANDLE_REF.
  185. */
  186. unsigned int internal_flags;
  187. /**
  188. * @filp_head: Placed on &drm_file.fbs, protected by &drm_file.fbs_lock.
  189. */
  190. struct list_head filp_head;
  191. /**
  192. * @obj: GEM objects backing the framebuffer, one per plane (optional).
  193. *
  194. * This is used by the GEM framebuffer helpers, see e.g.
  195. * drm_gem_fb_create().
  196. */
  197. struct drm_gem_object *obj[DRM_FORMAT_MAX_PLANES];
  198. };
  199. #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
  200. int drm_framebuffer_init(struct drm_device *dev,
  201. struct drm_framebuffer *fb,
  202. const struct drm_framebuffer_funcs *funcs);
  203. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  204. struct drm_file *file_priv,
  205. uint32_t id);
  206. void drm_framebuffer_remove(struct drm_framebuffer *fb);
  207. void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
  208. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
  209. /**
  210. * drm_framebuffer_get - acquire a framebuffer reference
  211. * @fb: DRM framebuffer
  212. *
  213. * This function increments the framebuffer's reference count.
  214. */
  215. static inline void drm_framebuffer_get(struct drm_framebuffer *fb)
  216. {
  217. drm_mode_object_get(&fb->base);
  218. }
  219. /**
  220. * drm_framebuffer_put - release a framebuffer reference
  221. * @fb: DRM framebuffer
  222. *
  223. * This function decrements the framebuffer's reference count and frees the
  224. * framebuffer if the reference count drops to zero.
  225. */
  226. static inline void drm_framebuffer_put(struct drm_framebuffer *fb)
  227. {
  228. drm_mode_object_put(&fb->base);
  229. }
  230. /**
  231. * drm_framebuffer_read_refcount - read the framebuffer reference count.
  232. * @fb: framebuffer
  233. *
  234. * This functions returns the framebuffer's reference count.
  235. */
  236. static inline uint32_t drm_framebuffer_read_refcount(const struct drm_framebuffer *fb)
  237. {
  238. return kref_read(&fb->base.refcount);
  239. }
  240. /**
  241. * drm_framebuffer_assign - store a reference to the fb
  242. * @p: location to store framebuffer
  243. * @fb: new framebuffer (maybe NULL)
  244. *
  245. * This functions sets the location to store a reference to the framebuffer,
  246. * unreferencing the framebuffer that was previously stored in that location.
  247. */
  248. static inline void drm_framebuffer_assign(struct drm_framebuffer **p,
  249. struct drm_framebuffer *fb)
  250. {
  251. if (fb)
  252. drm_framebuffer_get(fb);
  253. if (*p)
  254. drm_framebuffer_put(*p);
  255. *p = fb;
  256. }
  257. /*
  258. * drm_for_each_fb - iterate over all framebuffers
  259. * @fb: the loop cursor
  260. * @dev: the DRM device
  261. *
  262. * Iterate over all framebuffers of @dev. User must hold
  263. * &drm_mode_config.fb_lock.
  264. */
  265. #define drm_for_each_fb(fb, dev) \
  266. for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)), \
  267. fb = list_first_entry(&(dev)->mode_config.fb_list, \
  268. struct drm_framebuffer, head); \
  269. &fb->head != (&(dev)->mode_config.fb_list); \
  270. fb = list_next_entry(fb, head))
  271. /**
  272. * struct drm_afbc_framebuffer - a special afbc frame buffer object
  273. *
  274. * A derived class of struct drm_framebuffer, dedicated for afbc use cases.
  275. */
  276. struct drm_afbc_framebuffer {
  277. /**
  278. * @base: base framebuffer structure.
  279. */
  280. struct drm_framebuffer base;
  281. /**
  282. * @block_width: width of a single afbc block
  283. */
  284. u32 block_width;
  285. /**
  286. * @block_height: height of a single afbc block
  287. */
  288. u32 block_height;
  289. /**
  290. * @aligned_width: aligned frame buffer width
  291. */
  292. u32 aligned_width;
  293. /**
  294. * @aligned_height: aligned frame buffer height
  295. */
  296. u32 aligned_height;
  297. /**
  298. * @offset: offset of the first afbc header
  299. */
  300. u32 offset;
  301. /**
  302. * @afbc_size: minimum size of afbc buffer
  303. */
  304. u32 afbc_size;
  305. };
  306. #define fb_to_afbc_fb(x) container_of(x, struct drm_afbc_framebuffer, base)
  307. #endif