1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2021 Microsoft
6 #include <linux/hyperv.h>
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>
19 #include "hyperv_drm.h"
21 static int hyperv_blit_to_vram_rect(struct drm_framebuffer *fb,
22 const struct dma_buf_map *map,
23 struct drm_rect *rect)
25 struct hyperv_drm_device *hv = to_hv(fb->dev);
26 void *vmap = map->vaddr; /* TODO: Use mapping abstraction properly */
29 if (!drm_dev_enter(&hv->dev, &idx))
32 drm_fb_memcpy_dstclip(hv->vram, fb->pitches[0], vmap, fb, rect);
38 static int hyperv_blit_to_vram_fullscreen(struct drm_framebuffer *fb, const struct dma_buf_map *map)
40 struct drm_rect fullscreen = {
46 return hyperv_blit_to_vram_rect(fb, map, &fullscreen);
49 static int hyperv_connector_get_modes(struct drm_connector *connector)
51 struct hyperv_drm_device *hv = to_hv(connector->dev);
54 count = drm_add_modes_noedid(connector,
55 connector->dev->mode_config.max_width,
56 connector->dev->mode_config.max_height);
57 drm_set_preferred_mode(connector, hv->preferred_width,
58 hv->preferred_height);
63 static const struct drm_connector_helper_funcs hyperv_connector_helper_funcs = {
64 .get_modes = hyperv_connector_get_modes,
67 static const struct drm_connector_funcs hyperv_connector_funcs = {
68 .fill_modes = drm_helper_probe_single_connector_modes,
69 .destroy = drm_connector_cleanup,
70 .reset = drm_atomic_helper_connector_reset,
71 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
72 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
75 static inline int hyperv_conn_init(struct hyperv_drm_device *hv)
77 drm_connector_helper_add(&hv->connector, &hyperv_connector_helper_funcs);
78 return drm_connector_init(&hv->dev, &hv->connector,
79 &hyperv_connector_funcs,
80 DRM_MODE_CONNECTOR_VIRTUAL);
83 static int hyperv_check_size(struct hyperv_drm_device *hv, int w, int h,
84 struct drm_framebuffer *fb)
86 u32 pitch = w * (hv->screen_depth / 8);
89 pitch = fb->pitches[0];
91 if (pitch * h > hv->fb_size)
97 static void hyperv_pipe_enable(struct drm_simple_display_pipe *pipe,
98 struct drm_crtc_state *crtc_state,
99 struct drm_plane_state *plane_state)
101 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
102 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
104 hyperv_hide_hw_ptr(hv->hdev);
105 hyperv_update_situation(hv->hdev, 1, hv->screen_depth,
106 crtc_state->mode.hdisplay,
107 crtc_state->mode.vdisplay,
108 plane_state->fb->pitches[0]);
109 hyperv_blit_to_vram_fullscreen(plane_state->fb, &shadow_plane_state->data[0]);
112 static int hyperv_pipe_check(struct drm_simple_display_pipe *pipe,
113 struct drm_plane_state *plane_state,
114 struct drm_crtc_state *crtc_state)
116 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
117 struct drm_framebuffer *fb = plane_state->fb;
119 if (fb->format->format != DRM_FORMAT_XRGB8888)
122 if (fb->pitches[0] * fb->height > hv->fb_size)
128 static void hyperv_pipe_update(struct drm_simple_display_pipe *pipe,
129 struct drm_plane_state *old_state)
131 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
132 struct drm_plane_state *state = pipe->plane.state;
133 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
134 struct drm_rect rect;
136 if (drm_atomic_helper_damage_merged(old_state, state, &rect)) {
137 hyperv_blit_to_vram_rect(state->fb, &shadow_plane_state->data[0], &rect);
138 hyperv_update_dirt(hv->hdev, &rect);
142 static const struct drm_simple_display_pipe_funcs hyperv_pipe_funcs = {
143 .enable = hyperv_pipe_enable,
144 .check = hyperv_pipe_check,
145 .update = hyperv_pipe_update,
146 DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
149 static const uint32_t hyperv_formats[] = {
153 static const uint64_t hyperv_modifiers[] = {
154 DRM_FORMAT_MOD_LINEAR,
155 DRM_FORMAT_MOD_INVALID
158 static inline int hyperv_pipe_init(struct hyperv_drm_device *hv)
162 ret = drm_simple_display_pipe_init(&hv->dev,
166 ARRAY_SIZE(hyperv_formats),
172 drm_plane_enable_fb_damage_clips(&hv->pipe.plane);
177 static enum drm_mode_status
178 hyperv_mode_valid(struct drm_device *dev,
179 const struct drm_display_mode *mode)
181 struct hyperv_drm_device *hv = to_hv(dev);
183 if (hyperv_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
189 static const struct drm_mode_config_funcs hyperv_mode_config_funcs = {
190 .fb_create = drm_gem_fb_create_with_dirty,
191 .mode_valid = hyperv_mode_valid,
192 .atomic_check = drm_atomic_helper_check,
193 .atomic_commit = drm_atomic_helper_commit,
196 int hyperv_mode_config_init(struct hyperv_drm_device *hv)
198 struct drm_device *dev = &hv->dev;
201 ret = drmm_mode_config_init(dev);
203 drm_err(dev, "Failed to initialized mode setting.\n");
207 dev->mode_config.min_width = 0;
208 dev->mode_config.min_height = 0;
209 dev->mode_config.max_width = hv->screen_width_max;
210 dev->mode_config.max_height = hv->screen_height_max;
212 dev->mode_config.preferred_depth = hv->screen_depth;
213 dev->mode_config.prefer_shadow = 0;
215 dev->mode_config.funcs = &hyperv_mode_config_funcs;
217 ret = hyperv_conn_init(hv);
219 drm_err(dev, "Failed to initialized connector.\n");
223 ret = hyperv_pipe_init(hv);
225 drm_err(dev, "Failed to initialized pipe.\n");
229 drm_mode_config_reset(dev);