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