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_damage_helper.h>
9 #include <drm/drm_drv.h>
10 #include <drm/drm_edid.h>
11 #include <drm/drm_fb_helper.h>
12 #include <drm/drm_format_helper.h>
13 #include <drm/drm_fourcc.h>
14 #include <drm/drm_framebuffer.h>
15 #include <drm/drm_gem_atomic_helper.h>
16 #include <drm/drm_gem_framebuffer_helper.h>
17 #include <drm/drm_gem_shmem_helper.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_simple_kms_helper.h>
20 
21 #include "hyperv_drm.h"
22 
23 static int hyperv_blit_to_vram_rect(struct drm_framebuffer *fb,
24 				    const struct iosys_map *vmap,
25 				    struct drm_rect *rect)
26 {
27 	struct hyperv_drm_device *hv = to_hv(fb->dev);
28 	struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(hv->vram);
29 	int idx;
30 
31 	if (!drm_dev_enter(&hv->dev, &idx))
32 		return -ENODEV;
33 
34 	iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, rect));
35 	drm_fb_memcpy(&dst, fb->pitches, vmap, fb, rect);
36 
37 	drm_dev_exit(idx);
38 
39 	return 0;
40 }
41 
42 static int hyperv_blit_to_vram_fullscreen(struct drm_framebuffer *fb,
43 					  const struct iosys_map *map)
44 {
45 	struct drm_rect fullscreen = {
46 		.x1 = 0,
47 		.x2 = fb->width,
48 		.y1 = 0,
49 		.y2 = fb->height,
50 	};
51 	return hyperv_blit_to_vram_rect(fb, map, &fullscreen);
52 }
53 
54 static int hyperv_connector_get_modes(struct drm_connector *connector)
55 {
56 	struct hyperv_drm_device *hv = to_hv(connector->dev);
57 	int count;
58 
59 	count = drm_add_modes_noedid(connector,
60 				     connector->dev->mode_config.max_width,
61 				     connector->dev->mode_config.max_height);
62 	drm_set_preferred_mode(connector, hv->preferred_width,
63 			       hv->preferred_height);
64 
65 	return count;
66 }
67 
68 static const struct drm_connector_helper_funcs hyperv_connector_helper_funcs = {
69 	.get_modes = hyperv_connector_get_modes,
70 };
71 
72 static const struct drm_connector_funcs hyperv_connector_funcs = {
73 	.fill_modes = drm_helper_probe_single_connector_modes,
74 	.destroy = drm_connector_cleanup,
75 	.reset = drm_atomic_helper_connector_reset,
76 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
77 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
78 };
79 
80 static inline int hyperv_conn_init(struct hyperv_drm_device *hv)
81 {
82 	drm_connector_helper_add(&hv->connector, &hyperv_connector_helper_funcs);
83 	return drm_connector_init(&hv->dev, &hv->connector,
84 				  &hyperv_connector_funcs,
85 				  DRM_MODE_CONNECTOR_VIRTUAL);
86 }
87 
88 static int hyperv_check_size(struct hyperv_drm_device *hv, int w, int h,
89 			     struct drm_framebuffer *fb)
90 {
91 	u32 pitch = w * (hv->screen_depth / 8);
92 
93 	if (fb)
94 		pitch = fb->pitches[0];
95 
96 	if (pitch * h > hv->fb_size)
97 		return -EINVAL;
98 
99 	return 0;
100 }
101 
102 static void hyperv_pipe_enable(struct drm_simple_display_pipe *pipe,
103 			       struct drm_crtc_state *crtc_state,
104 			       struct drm_plane_state *plane_state)
105 {
106 	struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
107 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
108 
109 	hyperv_hide_hw_ptr(hv->hdev);
110 	hyperv_update_situation(hv->hdev, 1,  hv->screen_depth,
111 				crtc_state->mode.hdisplay,
112 				crtc_state->mode.vdisplay,
113 				plane_state->fb->pitches[0]);
114 	hyperv_blit_to_vram_fullscreen(plane_state->fb, &shadow_plane_state->data[0]);
115 }
116 
117 static int hyperv_pipe_check(struct drm_simple_display_pipe *pipe,
118 			     struct drm_plane_state *plane_state,
119 			     struct drm_crtc_state *crtc_state)
120 {
121 	struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
122 	struct drm_framebuffer *fb = plane_state->fb;
123 
124 	if (fb->format->format != DRM_FORMAT_XRGB8888)
125 		return -EINVAL;
126 
127 	if (fb->pitches[0] * fb->height > hv->fb_size) {
128 		drm_err(&hv->dev, "fb size requested by %s for %dX%d (pitch %d) greater than %ld\n",
129 			current->comm, fb->width, fb->height, fb->pitches[0], hv->fb_size);
130 		return -EINVAL;
131 	}
132 
133 	return 0;
134 }
135 
136 static void hyperv_pipe_update(struct drm_simple_display_pipe *pipe,
137 			       struct drm_plane_state *old_state)
138 {
139 	struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
140 	struct drm_plane_state *state = pipe->plane.state;
141 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
142 	struct drm_rect rect;
143 
144 	if (drm_atomic_helper_damage_merged(old_state, state, &rect)) {
145 		hyperv_blit_to_vram_rect(state->fb, &shadow_plane_state->data[0], &rect);
146 		hyperv_update_dirt(hv->hdev, &rect);
147 	}
148 }
149 
150 static const struct drm_simple_display_pipe_funcs hyperv_pipe_funcs = {
151 	.enable	= hyperv_pipe_enable,
152 	.check = hyperv_pipe_check,
153 	.update	= hyperv_pipe_update,
154 	DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
155 };
156 
157 static const uint32_t hyperv_formats[] = {
158 	DRM_FORMAT_XRGB8888,
159 };
160 
161 static const uint64_t hyperv_modifiers[] = {
162 	DRM_FORMAT_MOD_LINEAR,
163 	DRM_FORMAT_MOD_INVALID
164 };
165 
166 static inline int hyperv_pipe_init(struct hyperv_drm_device *hv)
167 {
168 	int ret;
169 
170 	ret = drm_simple_display_pipe_init(&hv->dev,
171 					   &hv->pipe,
172 					   &hyperv_pipe_funcs,
173 					   hyperv_formats,
174 					   ARRAY_SIZE(hyperv_formats),
175 					   hyperv_modifiers,
176 					   &hv->connector);
177 	if (ret)
178 		return ret;
179 
180 	drm_plane_enable_fb_damage_clips(&hv->pipe.plane);
181 
182 	return 0;
183 }
184 
185 static enum drm_mode_status
186 hyperv_mode_valid(struct drm_device *dev,
187 		  const struct drm_display_mode *mode)
188 {
189 	struct hyperv_drm_device *hv = to_hv(dev);
190 
191 	if (hyperv_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
192 		return MODE_BAD;
193 
194 	return MODE_OK;
195 }
196 
197 static const struct drm_mode_config_funcs hyperv_mode_config_funcs = {
198 	.fb_create = drm_gem_fb_create_with_dirty,
199 	.mode_valid = hyperv_mode_valid,
200 	.atomic_check = drm_atomic_helper_check,
201 	.atomic_commit = drm_atomic_helper_commit,
202 };
203 
204 int hyperv_mode_config_init(struct hyperv_drm_device *hv)
205 {
206 	struct drm_device *dev = &hv->dev;
207 	int ret;
208 
209 	ret = drmm_mode_config_init(dev);
210 	if (ret) {
211 		drm_err(dev, "Failed to initialized mode setting.\n");
212 		return ret;
213 	}
214 
215 	dev->mode_config.min_width = 0;
216 	dev->mode_config.min_height = 0;
217 	dev->mode_config.max_width = hv->screen_width_max;
218 	dev->mode_config.max_height = hv->screen_height_max;
219 
220 	dev->mode_config.preferred_depth = hv->screen_depth;
221 	dev->mode_config.prefer_shadow = 0;
222 
223 	dev->mode_config.funcs = &hyperv_mode_config_funcs;
224 
225 	ret = hyperv_conn_init(hv);
226 	if (ret) {
227 		drm_err(dev, "Failed to initialized connector.\n");
228 		return ret;
229 	}
230 
231 	ret = hyperv_pipe_init(hv);
232 	if (ret) {
233 		drm_err(dev, "Failed to initialized pipe.\n");
234 		return ret;
235 	}
236 
237 	drm_mode_config_reset(dev);
238 
239 	return 0;
240 }
241