vkms_drv.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #ifndef _VKMS_DRV_H_
  3. #define _VKMS_DRV_H_
  4. #include <linux/hrtimer.h>
  5. #include <drm/drm.h>
  6. #include <drm/drm_framebuffer.h>
  7. #include <drm/drm_gem.h>
  8. #include <drm/drm_gem_atomic_helper.h>
  9. #include <drm/drm_encoder.h>
  10. #include <drm/drm_writeback.h>
  11. #define DEFAULT_DEVICE_NAME "vkms"
  12. #define XRES_MIN 10
  13. #define YRES_MIN 10
  14. #define XRES_DEF 1024
  15. #define YRES_DEF 768
  16. #define XRES_MAX 8192
  17. #define YRES_MAX 8192
  18. #define NUM_OVERLAY_PLANES 8
  19. #define VKMS_LUT_SIZE 256
  20. /**
  21. * struct vkms_frame_info - Structure to store the state of a frame
  22. *
  23. * @fb: backing drm framebuffer
  24. * @src: source rectangle of this frame in the source framebuffer, stored in 16.16 fixed-point form
  25. * @dst: destination rectangle in the crtc buffer, stored in whole pixel units
  26. * @map: see @drm_shadow_plane_state.data
  27. * @rotation: rotation applied to the source.
  28. *
  29. * @src and @dst should have the same size modulo the rotation.
  30. */
  31. struct vkms_frame_info {
  32. struct drm_framebuffer *fb;
  33. struct drm_rect src, dst;
  34. struct iosys_map map[DRM_FORMAT_MAX_PLANES];
  35. unsigned int rotation;
  36. };
  37. struct pixel_argb_s32 {
  38. s32 a, r, g, b;
  39. };
  40. /**
  41. * struct pixel_argb_u16 - Internal representation of a pixel color.
  42. * @a: Alpha component value, stored in 16 bits, without padding, using
  43. * machine endianness
  44. * @r: Red component value, stored in 16 bits, without padding, using
  45. * machine endianness
  46. * @g: Green component value, stored in 16 bits, without padding, using
  47. * machine endianness
  48. * @b: Blue component value, stored in 16 bits, without padding, using
  49. * machine endianness
  50. *
  51. * The goal of this structure is to keep enough precision to ensure
  52. * correct composition results in VKMS and simplifying color
  53. * manipulation by splitting each component into its own field.
  54. * Caution: the byte ordering of this structure is machine-dependent,
  55. * you can't cast it directly to AR48 or xR48.
  56. */
  57. struct pixel_argb_u16 {
  58. u16 a, r, g, b;
  59. };
  60. struct line_buffer {
  61. size_t n_pixels;
  62. struct pixel_argb_u16 *pixels;
  63. };
  64. /**
  65. * typedef pixel_write_t - These functions are used to read a pixel from a
  66. * &struct pixel_argb_u16, convert it in a specific format and write it in the @out_pixel
  67. * buffer.
  68. *
  69. * @out_pixel: destination address to write the pixel
  70. * @in_pixel: pixel to write
  71. */
  72. typedef void (*pixel_write_t)(u8 *out_pixel, const struct pixel_argb_u16 *in_pixel);
  73. struct vkms_writeback_job {
  74. struct iosys_map data[DRM_FORMAT_MAX_PLANES];
  75. struct vkms_frame_info wb_frame_info;
  76. pixel_write_t pixel_write;
  77. };
  78. /**
  79. * enum pixel_read_direction - Enum used internally by VKMS to represent a reading direction in a
  80. * plane.
  81. */
  82. enum pixel_read_direction {
  83. READ_BOTTOM_TO_TOP,
  84. READ_TOP_TO_BOTTOM,
  85. READ_RIGHT_TO_LEFT,
  86. READ_LEFT_TO_RIGHT
  87. };
  88. struct vkms_plane_state;
  89. /**
  90. * typedef pixel_read_line_t - These functions are used to read a pixel line in the source frame,
  91. * convert it to `struct pixel_argb_u16` and write it to @out_pixel.
  92. *
  93. * @plane: plane used as source for the pixel value
  94. * @x_start: X (width) coordinate of the first pixel to copy. The caller must ensure that x_start
  95. * is non-negative and smaller than @plane->frame_info->fb->width.
  96. * @y_start: Y (height) coordinate of the first pixel to copy. The caller must ensure that y_start
  97. * is non-negative and smaller than @plane->frame_info->fb->height.
  98. * @direction: direction to use for the copy, starting at @x_start/@y_start
  99. * @count: number of pixels to copy
  100. * @out_pixel: pointer where to write the pixel values. They will be written from @out_pixel[0]
  101. * (included) to @out_pixel[@count] (excluded). The caller must ensure that out_pixel have a
  102. * length of at least @count.
  103. */
  104. typedef void (*pixel_read_line_t)(const struct vkms_plane_state *plane, int x_start,
  105. int y_start, enum pixel_read_direction direction, int count,
  106. struct pixel_argb_u16 out_pixel[]);
  107. /**
  108. * struct conversion_matrix - Matrix to use for a specific encoding and range
  109. *
  110. * @matrix: Conversion matrix from yuv to rgb. The matrix is stored in a row-major manner and is
  111. * used to compute rgb values from yuv values:
  112. * [[r],[g],[b]] = @matrix * [[y],[u],[v]]
  113. * OR for yvu formats:
  114. * [[r],[g],[b]] = @matrix * [[y],[v],[u]]
  115. * The values of the matrix are signed fixed-point values with 32 bits fractional part.
  116. * @y_offset: Offset to apply on the y value.
  117. */
  118. struct conversion_matrix {
  119. s64 matrix[3][3];
  120. int y_offset;
  121. };
  122. /**
  123. * struct vkms_plane_state - Driver specific plane state
  124. * @base: base plane state
  125. * @frame_info: data required for composing computation
  126. * @pixel_read_line: function to read a pixel line in this plane. The creator of a
  127. * struct vkms_plane_state must ensure that this pointer is valid
  128. * @conversion_matrix: matrix used for yuv formats to convert to rgb
  129. */
  130. struct vkms_plane_state {
  131. struct drm_shadow_plane_state base;
  132. struct vkms_frame_info *frame_info;
  133. pixel_read_line_t pixel_read_line;
  134. struct conversion_matrix conversion_matrix;
  135. };
  136. struct vkms_plane {
  137. struct drm_plane base;
  138. };
  139. struct vkms_color_lut {
  140. struct drm_color_lut *base;
  141. size_t lut_length;
  142. s64 channel_value2index_ratio;
  143. };
  144. /**
  145. * struct vkms_crtc_state - Driver specific CRTC state
  146. *
  147. * @base: base CRTC state
  148. * @composer_work: work struct to compose and add CRC entries
  149. *
  150. * @num_active_planes: Number of active planes
  151. * @active_planes: List containing all the active planes (counted by
  152. * @num_active_planes). They should be stored in z-order.
  153. * @active_writeback: Current active writeback job
  154. * @gamma_lut: Look up table for gamma used in this CRTC
  155. * @crc_pending: Protected by @vkms_output.composer_lock, true when the frame CRC is not computed
  156. * yet. Used by vblank to detect if the composer is too slow.
  157. * @wb_pending: Protected by @vkms_output.composer_lock, true when a writeback frame is requested.
  158. * @frame_start: Protected by @vkms_output.composer_lock, saves the frame number before the start
  159. * of the composition process.
  160. * @frame_end: Protected by @vkms_output.composer_lock, saves the last requested frame number.
  161. * This is used to generate enough CRC entries when the composition worker is too slow.
  162. */
  163. struct vkms_crtc_state {
  164. struct drm_crtc_state base;
  165. struct work_struct composer_work;
  166. int num_active_planes;
  167. struct vkms_plane_state **active_planes;
  168. struct vkms_writeback_job *active_writeback;
  169. struct vkms_color_lut gamma_lut;
  170. bool crc_pending;
  171. bool wb_pending;
  172. u64 frame_start;
  173. u64 frame_end;
  174. };
  175. /**
  176. * struct vkms_output - Internal representation of all output components in VKMS
  177. *
  178. * @crtc: Base CRTC in DRM
  179. * @encoder: DRM encoder used for this output
  180. * @connector: DRM connector used for this output
  181. * @wb_connecter: DRM writeback connector used for this output
  182. * @vblank_hrtimer: Timer used to trigger the vblank
  183. * @period_ns: vblank period, in nanoseconds, used to configure @vblank_hrtimer and to compute
  184. * vblank timestamps
  185. * @composer_workq: Ordered workqueue for @composer_state.composer_work.
  186. * @lock: Lock used to protect concurrent access to the composer
  187. * @composer_enabled: Protected by @lock, true when the VKMS composer is active (crc needed or
  188. * writeback)
  189. * @composer_state: Protected by @lock, current state of this VKMS output
  190. * @composer_lock: Lock used internally to protect @composer_state members
  191. */
  192. struct vkms_output {
  193. struct drm_crtc crtc;
  194. struct drm_writeback_connector wb_connector;
  195. struct drm_encoder wb_encoder;
  196. struct workqueue_struct *composer_workq;
  197. spinlock_t lock;
  198. bool composer_enabled;
  199. struct vkms_crtc_state *composer_state;
  200. spinlock_t composer_lock;
  201. };
  202. struct vkms_config;
  203. struct vkms_config_plane;
  204. /**
  205. * struct vkms_device - Description of a VKMS device
  206. *
  207. * @drm - Base device in DRM
  208. * @faux_dev - Associated faux device
  209. * @output - Configuration and sub-components of the VKMS device
  210. * @config: Configuration used in this VKMS device
  211. */
  212. struct vkms_device {
  213. struct drm_device drm;
  214. struct faux_device *faux_dev;
  215. const struct vkms_config *config;
  216. };
  217. /*
  218. * The following helpers are used to convert a member of a struct into its parent.
  219. */
  220. #define drm_crtc_to_vkms_output(target) \
  221. container_of(target, struct vkms_output, crtc)
  222. #define drm_device_to_vkms_device(target) \
  223. container_of(target, struct vkms_device, drm)
  224. #define to_vkms_crtc_state(target)\
  225. container_of(target, struct vkms_crtc_state, base)
  226. #define to_vkms_plane_state(target)\
  227. container_of(target, struct vkms_plane_state, base.base)
  228. /**
  229. * vkms_create() - Create a device from a configuration
  230. * @config: Config used to configure the new device
  231. *
  232. * A pointer to the created vkms_device is stored in @config
  233. *
  234. * Returns:
  235. * 0 on success or an error.
  236. */
  237. int vkms_create(struct vkms_config *config);
  238. /**
  239. * vkms_destroy() - Destroy a device
  240. * @config: Config from which the device was created
  241. *
  242. * The device is completely removed, but the @config is not freed. It can be
  243. * reused or destroyed with vkms_config_destroy().
  244. */
  245. void vkms_destroy(struct vkms_config *config);
  246. /**
  247. * vkms_crtc_init() - Initialize a CRTC for VKMS
  248. * @dev: DRM device associated with the VKMS buffer
  249. * @crtc: uninitialized CRTC device
  250. * @primary: primary plane to attach to the CRTC
  251. * @cursor: plane to attach to the CRTC
  252. */
  253. struct vkms_output *vkms_crtc_init(struct drm_device *dev,
  254. struct drm_plane *primary,
  255. struct drm_plane *cursor);
  256. /**
  257. * vkms_output_init() - Initialize all sub-components needed for a VKMS device.
  258. *
  259. * @vkmsdev: VKMS device to initialize
  260. */
  261. int vkms_output_init(struct vkms_device *vkmsdev);
  262. /**
  263. * vkms_plane_init() - Initialize a plane
  264. *
  265. * @vkmsdev: VKMS device containing the plane
  266. * @plane_cfg: plane configuration
  267. */
  268. struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,
  269. struct vkms_config_plane *plane_cfg);
  270. /* CRC Support */
  271. const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
  272. size_t *count);
  273. int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name);
  274. int vkms_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
  275. size_t *values_cnt);
  276. /* Composer Support */
  277. void vkms_composer_worker(struct work_struct *work);
  278. void vkms_set_composer(struct vkms_output *out, bool enabled);
  279. void vkms_writeback_row(struct vkms_writeback_job *wb, const struct line_buffer *src_buffer, int y);
  280. /* Writeback */
  281. int vkms_enable_writeback_connector(struct vkms_device *vkmsdev, struct vkms_output *vkms_out);
  282. /* Colorops */
  283. int vkms_initialize_colorops(struct drm_plane *plane);
  284. #endif /* _VKMS_DRV_H_ */