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