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 __iomem *dst = hv->vram;
27 void *vmap = map->vaddr; /* TODO: Use mapping abstraction properly */
30 if (!drm_dev_enter(&hv->dev, &idx))
33 dst += drm_fb_clip_offset(fb->pitches[0], fb->format, rect);
34 drm_fb_memcpy_toio(dst, fb->pitches[0], vmap, fb, rect);
41 static int hyperv_blit_to_vram_fullscreen(struct drm_framebuffer *fb, const struct dma_buf_map *map)
43 struct drm_rect fullscreen = {
49 return hyperv_blit_to_vram_rect(fb, map, &fullscreen);
52 static int hyperv_connector_get_modes(struct drm_connector *connector)
54 struct hyperv_drm_device *hv = to_hv(connector->dev);
57 count = drm_add_modes_noedid(connector,
58 connector->dev->mode_config.max_width,
59 connector->dev->mode_config.max_height);
60 drm_set_preferred_mode(connector, hv->preferred_width,
61 hv->preferred_height);
66 static const struct drm_connector_helper_funcs hyperv_connector_helper_funcs = {
67 .get_modes = hyperv_connector_get_modes,
70 static const struct drm_connector_funcs hyperv_connector_funcs = {
71 .fill_modes = drm_helper_probe_single_connector_modes,
72 .destroy = drm_connector_cleanup,
73 .reset = drm_atomic_helper_connector_reset,
74 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
75 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
78 static inline int hyperv_conn_init(struct hyperv_drm_device *hv)
80 drm_connector_helper_add(&hv->connector, &hyperv_connector_helper_funcs);
81 return drm_connector_init(&hv->dev, &hv->connector,
82 &hyperv_connector_funcs,
83 DRM_MODE_CONNECTOR_VIRTUAL);
86 static int hyperv_check_size(struct hyperv_drm_device *hv, int w, int h,
87 struct drm_framebuffer *fb)
89 u32 pitch = w * (hv->screen_depth / 8);
92 pitch = fb->pitches[0];
94 if (pitch * h > hv->fb_size)
100 static void hyperv_pipe_enable(struct drm_simple_display_pipe *pipe,
101 struct drm_crtc_state *crtc_state,
102 struct drm_plane_state *plane_state)
104 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
105 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
107 hyperv_hide_hw_ptr(hv->hdev);
108 hyperv_update_situation(hv->hdev, 1, hv->screen_depth,
109 crtc_state->mode.hdisplay,
110 crtc_state->mode.vdisplay,
111 plane_state->fb->pitches[0]);
112 hyperv_blit_to_vram_fullscreen(plane_state->fb, &shadow_plane_state->data[0]);
115 static int hyperv_pipe_check(struct drm_simple_display_pipe *pipe,
116 struct drm_plane_state *plane_state,
117 struct drm_crtc_state *crtc_state)
119 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
120 struct drm_framebuffer *fb = plane_state->fb;
122 if (fb->format->format != DRM_FORMAT_XRGB8888)
125 if (fb->pitches[0] * fb->height > hv->fb_size)
131 static void hyperv_pipe_update(struct drm_simple_display_pipe *pipe,
132 struct drm_plane_state *old_state)
134 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
135 struct drm_plane_state *state = pipe->plane.state;
136 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
137 struct drm_rect rect;
139 if (drm_atomic_helper_damage_merged(old_state, state, &rect)) {
140 hyperv_blit_to_vram_rect(state->fb, &shadow_plane_state->data[0], &rect);
141 hyperv_update_dirt(hv->hdev, &rect);
145 static const struct drm_simple_display_pipe_funcs hyperv_pipe_funcs = {
146 .enable = hyperv_pipe_enable,
147 .check = hyperv_pipe_check,
148 .update = hyperv_pipe_update,
149 DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
152 static const uint32_t hyperv_formats[] = {
156 static const uint64_t hyperv_modifiers[] = {
157 DRM_FORMAT_MOD_LINEAR,
158 DRM_FORMAT_MOD_INVALID
161 static inline int hyperv_pipe_init(struct hyperv_drm_device *hv)
165 ret = drm_simple_display_pipe_init(&hv->dev,
169 ARRAY_SIZE(hyperv_formats),
175 drm_plane_enable_fb_damage_clips(&hv->pipe.plane);
180 static enum drm_mode_status
181 hyperv_mode_valid(struct drm_device *dev,
182 const struct drm_display_mode *mode)
184 struct hyperv_drm_device *hv = to_hv(dev);
186 if (hyperv_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
192 static const struct drm_mode_config_funcs hyperv_mode_config_funcs = {
193 .fb_create = drm_gem_fb_create_with_dirty,
194 .mode_valid = hyperv_mode_valid,
195 .atomic_check = drm_atomic_helper_check,
196 .atomic_commit = drm_atomic_helper_commit,
199 int hyperv_mode_config_init(struct hyperv_drm_device *hv)
201 struct drm_device *dev = &hv->dev;
204 ret = drmm_mode_config_init(dev);
206 drm_err(dev, "Failed to initialized mode setting.\n");
210 dev->mode_config.min_width = 0;
211 dev->mode_config.min_height = 0;
212 dev->mode_config.max_width = hv->screen_width_max;
213 dev->mode_config.max_height = hv->screen_height_max;
215 dev->mode_config.preferred_depth = hv->screen_depth;
216 dev->mode_config.prefer_shadow = 0;
218 dev->mode_config.funcs = &hyperv_mode_config_funcs;
220 ret = hyperv_conn_init(hv);
222 drm_err(dev, "Failed to initialized connector.\n");
226 ret = hyperv_pipe_init(hv);
228 drm_err(dev, "Failed to initialized pipe.\n");
232 drm_mode_config_reset(dev);