0001
0002
0003
0004
0005
0006 #include <linux/hyperv.h>
0007
0008 #include <drm/drm_damage_helper.h>
0009 #include <drm/drm_drv.h>
0010 #include <drm/drm_edid.h>
0011 #include <drm/drm_fb_helper.h>
0012 #include <drm/drm_format_helper.h>
0013 #include <drm/drm_fourcc.h>
0014 #include <drm/drm_framebuffer.h>
0015 #include <drm/drm_gem_atomic_helper.h>
0016 #include <drm/drm_gem_framebuffer_helper.h>
0017 #include <drm/drm_gem_shmem_helper.h>
0018 #include <drm/drm_probe_helper.h>
0019 #include <drm/drm_simple_kms_helper.h>
0020
0021 #include "hyperv_drm.h"
0022
0023 static int hyperv_blit_to_vram_rect(struct drm_framebuffer *fb,
0024 const struct iosys_map *map,
0025 struct drm_rect *rect)
0026 {
0027 struct hyperv_drm_device *hv = to_hv(fb->dev);
0028 void __iomem *dst = hv->vram;
0029 void *vmap = map->vaddr;
0030 int idx;
0031
0032 if (!drm_dev_enter(&hv->dev, &idx))
0033 return -ENODEV;
0034
0035 dst += drm_fb_clip_offset(fb->pitches[0], fb->format, rect);
0036 drm_fb_memcpy_toio(dst, fb->pitches[0], vmap, fb, rect);
0037
0038 drm_dev_exit(idx);
0039
0040 return 0;
0041 }
0042
0043 static int hyperv_blit_to_vram_fullscreen(struct drm_framebuffer *fb,
0044 const struct iosys_map *map)
0045 {
0046 struct drm_rect fullscreen = {
0047 .x1 = 0,
0048 .x2 = fb->width,
0049 .y1 = 0,
0050 .y2 = fb->height,
0051 };
0052 return hyperv_blit_to_vram_rect(fb, map, &fullscreen);
0053 }
0054
0055 static int hyperv_connector_get_modes(struct drm_connector *connector)
0056 {
0057 struct hyperv_drm_device *hv = to_hv(connector->dev);
0058 int count;
0059
0060 count = drm_add_modes_noedid(connector,
0061 connector->dev->mode_config.max_width,
0062 connector->dev->mode_config.max_height);
0063 drm_set_preferred_mode(connector, hv->preferred_width,
0064 hv->preferred_height);
0065
0066 return count;
0067 }
0068
0069 static const struct drm_connector_helper_funcs hyperv_connector_helper_funcs = {
0070 .get_modes = hyperv_connector_get_modes,
0071 };
0072
0073 static const struct drm_connector_funcs hyperv_connector_funcs = {
0074 .fill_modes = drm_helper_probe_single_connector_modes,
0075 .destroy = drm_connector_cleanup,
0076 .reset = drm_atomic_helper_connector_reset,
0077 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
0078 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
0079 };
0080
0081 static inline int hyperv_conn_init(struct hyperv_drm_device *hv)
0082 {
0083 drm_connector_helper_add(&hv->connector, &hyperv_connector_helper_funcs);
0084 return drm_connector_init(&hv->dev, &hv->connector,
0085 &hyperv_connector_funcs,
0086 DRM_MODE_CONNECTOR_VIRTUAL);
0087 }
0088
0089 static int hyperv_check_size(struct hyperv_drm_device *hv, int w, int h,
0090 struct drm_framebuffer *fb)
0091 {
0092 u32 pitch = w * (hv->screen_depth / 8);
0093
0094 if (fb)
0095 pitch = fb->pitches[0];
0096
0097 if (pitch * h > hv->fb_size)
0098 return -EINVAL;
0099
0100 return 0;
0101 }
0102
0103 static void hyperv_pipe_enable(struct drm_simple_display_pipe *pipe,
0104 struct drm_crtc_state *crtc_state,
0105 struct drm_plane_state *plane_state)
0106 {
0107 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
0108 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
0109
0110 hyperv_hide_hw_ptr(hv->hdev);
0111 hyperv_update_situation(hv->hdev, 1, hv->screen_depth,
0112 crtc_state->mode.hdisplay,
0113 crtc_state->mode.vdisplay,
0114 plane_state->fb->pitches[0]);
0115 hyperv_blit_to_vram_fullscreen(plane_state->fb, &shadow_plane_state->data[0]);
0116 }
0117
0118 static int hyperv_pipe_check(struct drm_simple_display_pipe *pipe,
0119 struct drm_plane_state *plane_state,
0120 struct drm_crtc_state *crtc_state)
0121 {
0122 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
0123 struct drm_framebuffer *fb = plane_state->fb;
0124
0125 if (fb->format->format != DRM_FORMAT_XRGB8888)
0126 return -EINVAL;
0127
0128 if (fb->pitches[0] * fb->height > hv->fb_size) {
0129 drm_err(&hv->dev, "fb size requested by %s for %dX%d (pitch %d) greater than %ld\n",
0130 current->comm, fb->width, fb->height, fb->pitches[0], hv->fb_size);
0131 return -EINVAL;
0132 }
0133
0134 return 0;
0135 }
0136
0137 static void hyperv_pipe_update(struct drm_simple_display_pipe *pipe,
0138 struct drm_plane_state *old_state)
0139 {
0140 struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
0141 struct drm_plane_state *state = pipe->plane.state;
0142 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
0143 struct drm_rect rect;
0144
0145 if (drm_atomic_helper_damage_merged(old_state, state, &rect)) {
0146 hyperv_blit_to_vram_rect(state->fb, &shadow_plane_state->data[0], &rect);
0147 hyperv_update_dirt(hv->hdev, &rect);
0148 }
0149 }
0150
0151 static const struct drm_simple_display_pipe_funcs hyperv_pipe_funcs = {
0152 .enable = hyperv_pipe_enable,
0153 .check = hyperv_pipe_check,
0154 .update = hyperv_pipe_update,
0155 DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
0156 };
0157
0158 static const uint32_t hyperv_formats[] = {
0159 DRM_FORMAT_XRGB8888,
0160 };
0161
0162 static const uint64_t hyperv_modifiers[] = {
0163 DRM_FORMAT_MOD_LINEAR,
0164 DRM_FORMAT_MOD_INVALID
0165 };
0166
0167 static inline int hyperv_pipe_init(struct hyperv_drm_device *hv)
0168 {
0169 int ret;
0170
0171 ret = drm_simple_display_pipe_init(&hv->dev,
0172 &hv->pipe,
0173 &hyperv_pipe_funcs,
0174 hyperv_formats,
0175 ARRAY_SIZE(hyperv_formats),
0176 hyperv_modifiers,
0177 &hv->connector);
0178 if (ret)
0179 return ret;
0180
0181 drm_plane_enable_fb_damage_clips(&hv->pipe.plane);
0182
0183 return 0;
0184 }
0185
0186 static enum drm_mode_status
0187 hyperv_mode_valid(struct drm_device *dev,
0188 const struct drm_display_mode *mode)
0189 {
0190 struct hyperv_drm_device *hv = to_hv(dev);
0191
0192 if (hyperv_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
0193 return MODE_BAD;
0194
0195 return MODE_OK;
0196 }
0197
0198 static const struct drm_mode_config_funcs hyperv_mode_config_funcs = {
0199 .fb_create = drm_gem_fb_create_with_dirty,
0200 .mode_valid = hyperv_mode_valid,
0201 .atomic_check = drm_atomic_helper_check,
0202 .atomic_commit = drm_atomic_helper_commit,
0203 };
0204
0205 int hyperv_mode_config_init(struct hyperv_drm_device *hv)
0206 {
0207 struct drm_device *dev = &hv->dev;
0208 int ret;
0209
0210 ret = drmm_mode_config_init(dev);
0211 if (ret) {
0212 drm_err(dev, "Failed to initialized mode setting.\n");
0213 return ret;
0214 }
0215
0216 dev->mode_config.min_width = 0;
0217 dev->mode_config.min_height = 0;
0218 dev->mode_config.max_width = hv->screen_width_max;
0219 dev->mode_config.max_height = hv->screen_height_max;
0220
0221 dev->mode_config.preferred_depth = hv->screen_depth;
0222 dev->mode_config.prefer_shadow = 0;
0223
0224 dev->mode_config.funcs = &hyperv_mode_config_funcs;
0225
0226 ret = hyperv_conn_init(hv);
0227 if (ret) {
0228 drm_err(dev, "Failed to initialized connector.\n");
0229 return ret;
0230 }
0231
0232 ret = hyperv_pipe_init(hv);
0233 if (ret) {
0234 drm_err(dev, "Failed to initialized pipe.\n");
0235 return ret;
0236 }
0237
0238 drm_mode_config_reset(dev);
0239
0240 return 0;
0241 }