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_gem.h>
10 #include <drm/drm_gem_atomic_helper.h>
11 #include <drm/drm_encoder.h>
12 #include <drm/drm_writeback.h>
13 
14 #define XRES_MIN    20
15 #define YRES_MIN    20
16 
17 #define XRES_DEF  1024
18 #define YRES_DEF   768
19 
20 #define XRES_MAX  8192
21 #define YRES_MAX  8192
22 
23 #define NUM_OVERLAY_PLANES 8
24 
25 struct vkms_writeback_job {
26 	struct iosys_map map[DRM_FORMAT_MAX_PLANES];
27 	struct iosys_map data[DRM_FORMAT_MAX_PLANES];
28 };
29 
30 struct vkms_composer {
31 	struct drm_framebuffer fb;
32 	struct drm_rect src, dst;
33 	struct iosys_map map[4];
34 	unsigned int offset;
35 	unsigned int pitch;
36 	unsigned int cpp;
37 };
38 
39 /**
40  * vkms_plane_state - Driver specific plane state
41  * @base: base plane state
42  * @composer: data required for composing computation
43  */
44 struct vkms_plane_state {
45 	struct drm_shadow_plane_state base;
46 	struct vkms_composer *composer;
47 };
48 
49 struct vkms_plane {
50 	struct drm_plane base;
51 };
52 
53 /**
54  * vkms_crtc_state - Driver specific CRTC state
55  * @base: base CRTC state
56  * @composer_work: work struct to compose and add CRC entries
57  * @n_frame_start: start frame number for computed CRC
58  * @n_frame_end: end frame number for computed CRC
59  */
60 struct vkms_crtc_state {
61 	struct drm_crtc_state base;
62 	struct work_struct composer_work;
63 
64 	int num_active_planes;
65 	/* stack of active planes for crc computation, should be in z order */
66 	struct vkms_plane_state **active_planes;
67 	struct vkms_writeback_job *active_writeback;
68 
69 	/* below four are protected by vkms_output.composer_lock */
70 	bool crc_pending;
71 	bool wb_pending;
72 	u64 frame_start;
73 	u64 frame_end;
74 };
75 
76 struct vkms_output {
77 	struct drm_crtc crtc;
78 	struct drm_encoder encoder;
79 	struct drm_connector connector;
80 	struct drm_writeback_connector wb_connector;
81 	struct hrtimer vblank_hrtimer;
82 	ktime_t period_ns;
83 	struct drm_pending_vblank_event *event;
84 	/* ordered wq for composer_work */
85 	struct workqueue_struct *composer_workq;
86 	/* protects concurrent access to composer */
87 	spinlock_t lock;
88 
89 	/* protected by @lock */
90 	bool composer_enabled;
91 	struct vkms_crtc_state *composer_state;
92 
93 	spinlock_t composer_lock;
94 };
95 
96 struct vkms_device;
97 
98 struct vkms_config {
99 	bool writeback;
100 	bool cursor;
101 	bool overlay;
102 	/* only set when instantiated */
103 	struct vkms_device *dev;
104 };
105 
106 struct vkms_device {
107 	struct drm_device drm;
108 	struct platform_device *platform;
109 	struct vkms_output output;
110 	const struct vkms_config *config;
111 };
112 
113 #define drm_crtc_to_vkms_output(target) \
114 	container_of(target, struct vkms_output, crtc)
115 
116 #define drm_device_to_vkms_device(target) \
117 	container_of(target, struct vkms_device, drm)
118 
119 #define to_vkms_crtc_state(target)\
120 	container_of(target, struct vkms_crtc_state, base)
121 
122 #define to_vkms_plane_state(target)\
123 	container_of(target, struct vkms_plane_state, base.base)
124 
125 /* CRTC */
126 int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
127 		   struct drm_plane *primary, struct drm_plane *cursor);
128 
129 int vkms_output_init(struct vkms_device *vkmsdev, int index);
130 
131 struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,
132 				   enum drm_plane_type type, int index);
133 
134 /* CRC Support */
135 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
136 					size_t *count);
137 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name);
138 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
139 			   size_t *values_cnt);
140 
141 /* Composer Support */
142 void vkms_composer_worker(struct work_struct *work);
143 void vkms_set_composer(struct vkms_output *out, bool enabled);
144 
145 /* Writeback */
146 int vkms_enable_writeback_connector(struct vkms_device *vkmsdev);
147 
148 #endif /* _VKMS_DRV_H_ */
149