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_update_situation(hv->hdev, 1, hv->screen_depth,
105 crtc_state->mode.hdisplay,
106 crtc_state->mode.vdisplay,
107 plane_state->fb->pitches[0]);
108 hyperv_blit_to_vram_fullscreen(plane_state->fb, &shadow_plane_state->data[0]);
111 static int hyperv_pipe_check(struct drm_simple_display_pipe *pipe,
112 struct drm_plane_state *plane_state,
113 struct drm_crtc_state *crtc_state)
115 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
116 struct drm_framebuffer *fb = plane_state->fb;
118 if (fb->format->format != DRM_FORMAT_XRGB8888)
121 if (fb->pitches[0] * fb->height > hv->fb_size)
127 static void hyperv_pipe_update(struct drm_simple_display_pipe *pipe,
128 struct drm_plane_state *old_state)
130 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
131 struct drm_plane_state *state = pipe->plane.state;
132 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
133 struct drm_rect rect;
135 if (drm_atomic_helper_damage_merged(old_state, state, &rect)) {
136 hyperv_blit_to_vram_rect(state->fb, &shadow_plane_state->data[0], &rect);
137 hyperv_update_dirt(hv->hdev, &rect);
141 static const struct drm_simple_display_pipe_funcs hyperv_pipe_funcs = {
142 .enable = hyperv_pipe_enable,
143 .check = hyperv_pipe_check,
144 .update = hyperv_pipe_update,
145 DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
148 static const uint32_t hyperv_formats[] = {
152 static const uint64_t hyperv_modifiers[] = {
153 DRM_FORMAT_MOD_LINEAR,
154 DRM_FORMAT_MOD_INVALID
157 static inline int hyperv_pipe_init(struct hyperv_drm_device *hv)
161 ret = drm_simple_display_pipe_init(&hv->dev,
165 ARRAY_SIZE(hyperv_formats),
171 drm_plane_enable_fb_damage_clips(&hv->pipe.plane);
176 static enum drm_mode_status
177 hyperv_mode_valid(struct drm_device *dev,
178 const struct drm_display_mode *mode)
180 struct hyperv_drm_device *hv = to_hv(dev);
182 if (hyperv_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
188 static const struct drm_mode_config_funcs hyperv_mode_config_funcs = {
189 .fb_create = drm_gem_fb_create_with_dirty,
190 .mode_valid = hyperv_mode_valid,
191 .atomic_check = drm_atomic_helper_check,
192 .atomic_commit = drm_atomic_helper_commit,
195 int hyperv_mode_config_init(struct hyperv_drm_device *hv)
197 struct drm_device *dev = &hv->dev;
200 ret = drmm_mode_config_init(dev);
202 drm_err(dev, "Failed to initialized mode setting.\n");
206 dev->mode_config.min_width = 0;
207 dev->mode_config.min_height = 0;
208 dev->mode_config.max_width = hv->screen_width_max;
209 dev->mode_config.max_height = hv->screen_height_max;
211 dev->mode_config.preferred_depth = hv->screen_depth;
212 dev->mode_config.prefer_shadow = 0;
214 dev->mode_config.funcs = &hyperv_mode_config_funcs;
216 ret = hyperv_conn_init(hv);
218 drm_err(dev, "Failed to initialized connector.\n");
222 ret = hyperv_pipe_init(hv);
224 drm_err(dev, "Failed to initialized pipe.\n");
228 drm_mode_config_reset(dev);