xref: /openbmc/linux/drivers/gpu/drm/vkms/vkms_drv.h (revision 373beef0)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 
3 #ifndef _VKMS_DRV_H_
4 #define _VKMS_DRV_H_
5 
6 #include <linux/hrtimer.h>
7 
8 #include <drm/drm.h>
9 #include <drm/drm_framebuffer.h>
10 #include <drm/drm_gem.h>
11 #include <drm/drm_gem_atomic_helper.h>
12 #include <drm/drm_encoder.h>
13 #include <drm/drm_writeback.h>
14 
15 #define XRES_MIN    10
16 #define YRES_MIN    10
17 
18 #define XRES_DEF  1024
19 #define YRES_DEF   768
20 
21 #define XRES_MAX  8192
22 #define YRES_MAX  8192
23 
24 #define NUM_OVERLAY_PLANES 8
25 
26 #define VKMS_LUT_SIZE 256
27 
28 struct vkms_frame_info {
29 	struct drm_framebuffer *fb;
30 	struct drm_rect src, dst;
31 	struct drm_rect rotated;
32 	struct iosys_map map[DRM_FORMAT_MAX_PLANES];
33 	unsigned int rotation;
34 	unsigned int offset;
35 	unsigned int pitch;
36 	unsigned int cpp;
37 };
38 
39 struct pixel_argb_u16 {
40 	u16 a, r, g, b;
41 };
42 
43 struct line_buffer {
44 	size_t n_pixels;
45 	struct pixel_argb_u16 *pixels;
46 };
47 
48 struct vkms_writeback_job {
49 	struct iosys_map data[DRM_FORMAT_MAX_PLANES];
50 	struct vkms_frame_info wb_frame_info;
51 	void (*pixel_write)(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel);
52 };
53 
54 /**
55  * vkms_plane_state - Driver specific plane state
56  * @base: base plane state
57  * @frame_info: data required for composing computation
58  */
59 struct vkms_plane_state {
60 	struct drm_shadow_plane_state base;
61 	struct vkms_frame_info *frame_info;
62 	void (*pixel_read)(u8 *src_buffer, struct pixel_argb_u16 *out_pixel);
63 };
64 
65 struct vkms_plane {
66 	struct drm_plane base;
67 };
68 
69 struct vkms_color_lut {
70 	struct drm_color_lut *base;
71 	size_t lut_length;
72 	s64 channel_value2index_ratio;
73 };
74 
75 /**
76  * vkms_crtc_state - Driver specific CRTC state
77  * @base: base CRTC state
78  * @composer_work: work struct to compose and add CRC entries
79  * @n_frame_start: start frame number for computed CRC
80  * @n_frame_end: end frame number for computed CRC
81  */
82 struct vkms_crtc_state {
83 	struct drm_crtc_state base;
84 	struct work_struct composer_work;
85 
86 	int num_active_planes;
87 	/* stack of active planes for crc computation, should be in z order */
88 	struct vkms_plane_state **active_planes;
89 	struct vkms_writeback_job *active_writeback;
90 	struct vkms_color_lut gamma_lut;
91 
92 	/* below four are protected by vkms_output.composer_lock */
93 	bool crc_pending;
94 	bool wb_pending;
95 	u64 frame_start;
96 	u64 frame_end;
97 };
98 
99 struct vkms_output {
100 	struct drm_crtc crtc;
101 	struct drm_encoder encoder;
102 	struct drm_connector connector;
103 	struct drm_writeback_connector wb_connector;
104 	struct hrtimer vblank_hrtimer;
105 	ktime_t period_ns;
106 	struct drm_pending_vblank_event *event;
107 	/* ordered wq for composer_work */
108 	struct workqueue_struct *composer_workq;
109 	/* protects concurrent access to composer */
110 	spinlock_t lock;
111 	/* guarantees that if the composer is enabled, a job will be queued */
112 	struct mutex enabled_lock;
113 
114 	/* protected by @enabled_lock */
115 	bool composer_enabled;
116 	struct vkms_crtc_state *composer_state;
117 
118 	spinlock_t composer_lock;
119 };
120 
121 struct vkms_device;
122 
123 struct vkms_config {
124 	bool writeback;
125 	bool cursor;
126 	bool overlay;
127 	/* only set when instantiated */
128 	struct vkms_device *dev;
129 };
130 
131 struct vkms_device {
132 	struct drm_device drm;
133 	struct platform_device *platform;
134 	struct vkms_output output;
135 	const struct vkms_config *config;
136 };
137 
138 #define drm_crtc_to_vkms_output(target) \
139 	container_of(target, struct vkms_output, crtc)
140 
141 #define drm_device_to_vkms_device(target) \
142 	container_of(target, struct vkms_device, drm)
143 
144 #define to_vkms_crtc_state(target)\
145 	container_of(target, struct vkms_crtc_state, base)
146 
147 #define to_vkms_plane_state(target)\
148 	container_of(target, struct vkms_plane_state, base.base)
149 
150 /* CRTC */
151 int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
152 		   struct drm_plane *primary, struct drm_plane *cursor);
153 
154 int vkms_output_init(struct vkms_device *vkmsdev, int index);
155 
156 struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,
157 				   enum drm_plane_type type, int index);
158 
159 /* CRC Support */
160 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
161 					size_t *count);
162 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name);
163 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
164 			   size_t *values_cnt);
165 
166 /* Composer Support */
167 void vkms_composer_worker(struct work_struct *work);
168 void vkms_set_composer(struct vkms_output *out, bool enabled);
169 void vkms_compose_row(struct line_buffer *stage_buffer, struct vkms_plane_state *plane, int y);
170 void vkms_writeback_row(struct vkms_writeback_job *wb, const struct line_buffer *src_buffer, int y);
171 
172 /* Writeback */
173 int vkms_enable_writeback_connector(struct vkms_device *vkmsdev);
174 
175 #endif /* _VKMS_DRV_H_ */
176