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_edid.h>
11 #include <drm/drm_format_helper.h>
12 #include <drm/drm_fourcc.h>
13 #include <drm/drm_framebuffer.h>
14 #include <drm/drm_gem_atomic_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_gem_shmem_helper.h>
17 #include <drm/drm_probe_helper.h>
18 #include <drm/drm_simple_kms_helper.h>
20 #include "hyperv_drm.h"
22 static int hyperv_blit_to_vram_rect(struct drm_framebuffer *fb,
23 const struct iosys_map *vmap,
24 struct drm_rect *rect)
26 struct hyperv_drm_device *hv = to_hv(fb->dev);
27 struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(hv->vram);
30 if (!drm_dev_enter(&hv->dev, &idx))
33 iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, rect));
34 drm_fb_memcpy(&dst, fb->pitches, vmap, fb, rect);
41 static int hyperv_blit_to_vram_fullscreen(struct drm_framebuffer *fb,
42 const struct iosys_map *map)
44 struct drm_rect fullscreen = {
50 return hyperv_blit_to_vram_rect(fb, map, &fullscreen);
53 static int hyperv_connector_get_modes(struct drm_connector *connector)
55 struct hyperv_drm_device *hv = to_hv(connector->dev);
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);
67 static const struct drm_connector_helper_funcs hyperv_connector_helper_funcs = {
68 .get_modes = hyperv_connector_get_modes,
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,
79 static inline int hyperv_conn_init(struct hyperv_drm_device *hv)
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);
87 static int hyperv_check_size(struct hyperv_drm_device *hv, int w, int h,
88 struct drm_framebuffer *fb)
90 u32 pitch = w * (hv->screen_depth / 8);
93 pitch = fb->pitches[0];
95 if (pitch * h > hv->fb_size)
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)
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);
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]);
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)
120 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
121 struct drm_framebuffer *fb = plane_state->fb;
123 if (fb->format->format != DRM_FORMAT_XRGB8888)
126 if (fb->pitches[0] * fb->height > hv->fb_size) {
127 drm_err(&hv->dev, "fb size requested by %s for %dX%d (pitch %d) greater than %ld\n",
128 current->comm, fb->width, fb->height, fb->pitches[0], hv->fb_size);
135 static void hyperv_pipe_update(struct drm_simple_display_pipe *pipe,
136 struct drm_plane_state *old_state)
138 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
139 struct drm_plane_state *state = pipe->plane.state;
140 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
141 struct drm_rect rect;
143 if (drm_atomic_helper_damage_merged(old_state, state, &rect)) {
144 hyperv_blit_to_vram_rect(state->fb, &shadow_plane_state->data[0], &rect);
145 hyperv_update_dirt(hv->hdev, &rect);
149 static const struct drm_simple_display_pipe_funcs hyperv_pipe_funcs = {
150 .enable = hyperv_pipe_enable,
151 .check = hyperv_pipe_check,
152 .update = hyperv_pipe_update,
153 DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
156 static const uint32_t hyperv_formats[] = {
160 static const uint64_t hyperv_modifiers[] = {
161 DRM_FORMAT_MOD_LINEAR,
162 DRM_FORMAT_MOD_INVALID
165 static inline int hyperv_pipe_init(struct hyperv_drm_device *hv)
169 ret = drm_simple_display_pipe_init(&hv->dev,
173 ARRAY_SIZE(hyperv_formats),
179 drm_plane_enable_fb_damage_clips(&hv->pipe.plane);
184 static enum drm_mode_status
185 hyperv_mode_valid(struct drm_device *dev,
186 const struct drm_display_mode *mode)
188 struct hyperv_drm_device *hv = to_hv(dev);
190 if (hyperv_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
196 static const struct drm_mode_config_funcs hyperv_mode_config_funcs = {
197 .fb_create = drm_gem_fb_create_with_dirty,
198 .mode_valid = hyperv_mode_valid,
199 .atomic_check = drm_atomic_helper_check,
200 .atomic_commit = drm_atomic_helper_commit,
203 int hyperv_mode_config_init(struct hyperv_drm_device *hv)
205 struct drm_device *dev = &hv->dev;
208 ret = drmm_mode_config_init(dev);
210 drm_err(dev, "Failed to initialized mode setting.\n");
214 dev->mode_config.min_width = 0;
215 dev->mode_config.min_height = 0;
216 dev->mode_config.max_width = hv->screen_width_max;
217 dev->mode_config.max_height = hv->screen_height_max;
219 dev->mode_config.preferred_depth = hv->screen_depth;
220 dev->mode_config.prefer_shadow = 0;
222 dev->mode_config.funcs = &hyperv_mode_config_funcs;
224 ret = hyperv_conn_init(hv);
226 drm_err(dev, "Failed to initialized connector.\n");
230 ret = hyperv_pipe_init(hv);
232 drm_err(dev, "Failed to initialized pipe.\n");
236 drm_mode_config_reset(dev);