xref: /linux/drivers/gpu/drm/hyperv/hyperv_drm_modeset.c (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2021 Microsoft
4  */
5 
6 #include <linux/hyperv.h>
7 
8 #include <drm/drm_atomic.h>
9 #include <drm/drm_crtc_helper.h>
10 #include <drm/drm_damage_helper.h>
11 #include <drm/drm_drv.h>
12 #include <drm/drm_edid.h>
13 #include <drm/drm_format_helper.h>
14 #include <drm/drm_fourcc.h>
15 #include <drm/drm_framebuffer.h>
16 #include <drm/drm_gem_atomic_helper.h>
17 #include <drm/drm_gem_framebuffer_helper.h>
18 #include <drm/drm_gem_shmem_helper.h>
19 #include <drm/drm_probe_helper.h>
20 #include <drm/drm_panic.h>
21 #include <drm/drm_panic_helper.h>
22 #include <drm/drm_plane.h>
23 #include <drm/drm_print.h>
24 #include <drm/drm_vblank.h>
25 #include <drm/drm_vblank_helper.h>
26 
27 #include "hyperv_drm.h"
28 
29 static int hv_drm_blit_to_vram_rect(struct drm_framebuffer *fb,
30 				    const struct iosys_map *vmap,
31 				    struct drm_rect *rect)
32 {
33 	struct hv_drm_device *hv = to_hv_drm(fb->dev);
34 	struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(hv->vram);
35 	int idx;
36 
37 	if (!drm_dev_enter(&hv->dev, &idx))
38 		return -ENODEV;
39 
40 	iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, rect));
41 	drm_fb_memcpy(&dst, fb->pitches, vmap, fb, rect);
42 
43 	drm_dev_exit(idx);
44 
45 	return 0;
46 }
47 
48 static int hv_drm_connector_get_modes(struct drm_connector *connector)
49 {
50 	struct hv_drm_device *hv = to_hv_drm(connector->dev);
51 	int count;
52 
53 	count = drm_add_modes_noedid(connector,
54 				     connector->dev->mode_config.max_width,
55 				     connector->dev->mode_config.max_height);
56 	drm_set_preferred_mode(connector, hv->preferred_width,
57 			       hv->preferred_height);
58 
59 	return count;
60 }
61 
62 static const struct drm_connector_helper_funcs hv_drm_connector_helper_funcs = {
63 	.get_modes = hv_drm_connector_get_modes,
64 };
65 
66 static const struct drm_connector_funcs hv_drm_connector_funcs = {
67 	.fill_modes = drm_helper_probe_single_connector_modes,
68 	.destroy = drm_connector_cleanup,
69 	.atomic_create_state = drm_atomic_helper_connector_create_state,
70 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
71 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
72 };
73 
74 static inline int hv_drm_conn_init(struct hv_drm_device *hv)
75 {
76 	drm_connector_helper_add(&hv->connector, &hv_drm_connector_helper_funcs);
77 	return drm_connector_init(&hv->dev, &hv->connector,
78 				  &hv_drm_connector_funcs,
79 				  DRM_MODE_CONNECTOR_VIRTUAL);
80 }
81 
82 static int hv_drm_check_size(struct hv_drm_device *hv, int w, int h,
83 			     struct drm_framebuffer *fb)
84 {
85 	u32 pitch = w * (hv->screen_depth / 8);
86 
87 	if (fb)
88 		pitch = fb->pitches[0];
89 
90 	if (pitch * h > hv->fb_size)
91 		return -EINVAL;
92 
93 	return 0;
94 }
95 
96 static const uint32_t hv_drm_formats[] = {
97 	DRM_FORMAT_XRGB8888,
98 };
99 
100 static const uint64_t hv_drm_modifiers[] = {
101 	DRM_FORMAT_MOD_LINEAR,
102 	DRM_FORMAT_MOD_INVALID
103 };
104 
105 static void hv_drm_crtc_helper_atomic_enable(struct drm_crtc *crtc,
106 					     struct drm_atomic_commit *state)
107 {
108 	struct hv_drm_device *hv = to_hv_drm(crtc->dev);
109 	struct drm_plane *plane = &hv->plane;
110 	struct drm_plane_state *plane_state = plane->state;
111 	struct drm_crtc_state *crtc_state = crtc->state;
112 
113 	hv_drm_hide_hw_ptr(hv->hdev);
114 	hv_drm_update_situation(hv->hdev, 1,  hv->screen_depth,
115 				crtc_state->mode.hdisplay,
116 				crtc_state->mode.vdisplay,
117 				plane_state->fb->pitches[0]);
118 
119 	drm_crtc_vblank_on(crtc);
120 }
121 
122 static const struct drm_crtc_helper_funcs hv_drm_crtc_helper_funcs = {
123 	.atomic_check = drm_crtc_helper_atomic_check,
124 	.atomic_flush = drm_crtc_vblank_atomic_flush,
125 	.atomic_enable = hv_drm_crtc_helper_atomic_enable,
126 	.atomic_disable = drm_crtc_vblank_atomic_disable,
127 };
128 
129 static const struct drm_crtc_funcs hv_drm_crtc_funcs = {
130 	.atomic_create_state = drm_atomic_helper_crtc_create_state,
131 	.destroy = drm_crtc_cleanup,
132 	.set_config = drm_atomic_helper_set_config,
133 	.page_flip = drm_atomic_helper_page_flip,
134 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
135 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
136 	DRM_CRTC_VBLANK_TIMER_FUNCS,
137 };
138 
139 static int hv_drm_plane_atomic_check(struct drm_plane *plane,
140 				     struct drm_atomic_commit *state)
141 {
142 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
143 	struct hv_drm_device *hv = to_hv_drm(plane->dev);
144 	struct drm_framebuffer *fb = plane_state->fb;
145 	struct drm_crtc *crtc = plane_state->crtc;
146 	struct drm_crtc_state *crtc_state = NULL;
147 	int ret;
148 
149 	if (crtc)
150 		crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
151 
152 	ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
153 						  DRM_PLANE_NO_SCALING,
154 						  DRM_PLANE_NO_SCALING,
155 						  false, false);
156 	if (ret)
157 		return ret;
158 
159 	if (!plane_state->visible)
160 		return 0;
161 
162 	if (fb->pitches[0] * fb->height > hv->fb_size) {
163 		drm_err(&hv->dev, "fb size requested by %s for %dX%d (pitch %d) greater than %ld\n",
164 			current->comm, fb->width, fb->height, fb->pitches[0], hv->fb_size);
165 		return -EINVAL;
166 	}
167 
168 	return 0;
169 }
170 
171 static void hv_drm_plane_atomic_update(struct drm_plane *plane,
172 				       struct drm_atomic_commit *state)
173 {
174 	struct hv_drm_device *hv = to_hv_drm(plane->dev);
175 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
176 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
177 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(new_state);
178 	struct drm_rect damage;
179 	struct drm_rect dst_clip;
180 	struct drm_atomic_helper_damage_iter iter;
181 
182 	drm_atomic_helper_damage_iter_init(&iter, old_state, new_state);
183 	drm_atomic_for_each_plane_damage(&iter, &damage) {
184 		dst_clip = new_state->dst;
185 
186 		if (!drm_rect_intersect(&dst_clip, &damage))
187 			continue;
188 
189 		hv_drm_blit_to_vram_rect(new_state->fb, &shadow_plane_state->data[0], &damage);
190 		hv_drm_update_dirt(hv->hdev, &damage);
191 	}
192 }
193 
194 static int hv_drm_plane_get_scanout_buffer(struct drm_plane *plane,
195 					   struct drm_scanout_buffer *sb)
196 {
197 	struct hv_drm_device *hv = to_hv_drm(plane->dev);
198 	struct iosys_map map = IOSYS_MAP_INIT_VADDR_IOMEM(hv->vram);
199 
200 	if (plane->state && plane->state->fb) {
201 		sb->format = plane->state->fb->format;
202 		sb->width = plane->state->fb->width;
203 		sb->height = plane->state->fb->height;
204 		sb->pitch[0] = plane->state->fb->pitches[0];
205 		sb->map[0] = map;
206 		return 0;
207 	}
208 	return -ENODEV;
209 }
210 
211 static void hv_drm_plane_panic_flush(struct drm_plane *plane)
212 {
213 	struct hv_drm_device *hv = to_hv_drm(plane->dev);
214 	struct drm_rect rect;
215 
216 	if (plane->state && plane->state->fb) {
217 		rect.x1 = 0;
218 		rect.y1 = 0;
219 		rect.x2 = plane->state->fb->width;
220 		rect.y2 = plane->state->fb->height;
221 
222 		hv_drm_update_dirt(hv->hdev, &rect);
223 	}
224 
225 	vmbus_initiate_unload(true);
226 }
227 
228 static const struct drm_plane_helper_funcs hv_drm_plane_helper_funcs = {
229 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
230 	.atomic_check = hv_drm_plane_atomic_check,
231 	.atomic_update = hv_drm_plane_atomic_update,
232 	.get_scanout_buffer = hv_drm_plane_get_scanout_buffer,
233 	.panic_flush = hv_drm_plane_panic_flush,
234 };
235 
236 static const struct drm_plane_funcs hv_drm_plane_funcs = {
237 	.update_plane		= drm_atomic_helper_update_plane,
238 	.disable_plane		= drm_atomic_helper_disable_plane,
239 	.destroy		= drm_plane_cleanup,
240 	DRM_GEM_SHADOW_PLANE_FUNCS,
241 	DRM_PANIC_PLANE_FUNCS,
242 };
243 
244 static const struct drm_encoder_funcs hv_drm_simple_encoder_funcs_cleanup = {
245 	.destroy = drm_encoder_cleanup,
246 };
247 
248 static inline int hv_drm_pipe_init(struct hv_drm_device *hv)
249 {
250 	struct drm_device *dev = &hv->dev;
251 	struct drm_encoder *encoder = &hv->encoder;
252 	struct drm_plane *plane = &hv->plane;
253 	struct drm_crtc *crtc = &hv->crtc;
254 	struct drm_connector *connector = &hv->connector;
255 	int ret;
256 
257 	ret = drm_universal_plane_init(dev, plane, 0,
258 				       &hv_drm_plane_funcs,
259 				       hv_drm_formats, ARRAY_SIZE(hv_drm_formats),
260 				       hv_drm_modifiers,
261 				       DRM_PLANE_TYPE_PRIMARY, NULL);
262 	if (ret)
263 		return ret;
264 	drm_plane_helper_add(plane, &hv_drm_plane_helper_funcs);
265 	drm_plane_enable_fb_damage_clips(plane);
266 
267 	ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
268 					&hv_drm_crtc_funcs, NULL);
269 	if (ret)
270 		return ret;
271 	drm_crtc_helper_add(crtc, &hv_drm_crtc_helper_funcs);
272 
273 	encoder->possible_crtcs = drm_crtc_mask(crtc);
274 	ret = drm_encoder_init(dev, encoder,
275 			       &hv_drm_simple_encoder_funcs_cleanup,
276 			       DRM_MODE_ENCODER_NONE, NULL);
277 	if (ret)
278 		return ret;
279 
280 	ret = hv_drm_conn_init(hv);
281 	if (ret) {
282 		drm_err(dev, "Failed to initialized connector.\n");
283 		return ret;
284 	}
285 
286 	return drm_connector_attach_encoder(connector, encoder);
287 }
288 
289 static enum drm_mode_status
290 hv_drm_mode_valid(struct drm_device *dev,
291 		  const struct drm_display_mode *mode)
292 {
293 	struct hv_drm_device *hv = to_hv_drm(dev);
294 
295 	if (hv_drm_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
296 		return MODE_BAD;
297 
298 	return MODE_OK;
299 }
300 
301 static const struct drm_mode_config_funcs hv_drm_mode_config_funcs = {
302 	.fb_create = drm_gem_fb_create_with_dirty,
303 	.mode_valid = hv_drm_mode_valid,
304 	.atomic_check = drm_atomic_helper_check,
305 	.atomic_commit = drm_atomic_helper_commit,
306 };
307 
308 int hv_drm_mode_config_init(struct hv_drm_device *hv)
309 {
310 	struct drm_device *dev = &hv->dev;
311 	int ret;
312 
313 	ret = drmm_mode_config_init(dev);
314 	if (ret) {
315 		drm_err(dev, "Failed to initialized mode setting.\n");
316 		return ret;
317 	}
318 
319 	dev->mode_config.min_width = 0;
320 	dev->mode_config.min_height = 0;
321 	dev->mode_config.max_width = hv->screen_width_max;
322 	dev->mode_config.max_height = hv->screen_height_max;
323 
324 	dev->mode_config.preferred_depth = hv->screen_depth;
325 	dev->mode_config.prefer_shadow = 0;
326 
327 	dev->mode_config.funcs = &hv_drm_mode_config_funcs;
328 
329 	ret = hv_drm_pipe_init(hv);
330 	if (ret) {
331 		drm_err(dev, "Failed to initialized pipe.\n");
332 		return ret;
333 	}
334 
335 	ret = drm_vblank_init(dev, 1);
336 	if (ret)
337 		return ret;
338 
339 	drm_mode_config_reset(dev);
340 
341 	return 0;
342 }
343