| 1 | |
| 2 | |
| 3 | * Copyright 2021 Microsoft
|
| 4 |
|
| 5 |
|
| 6 | #include <linux/hyperv.h>
|
| 7 |
|
| 8 | #include <drm/drm_atomic.h>
|
| 9 | #include <drm/drm_crtc_helper.h>
|
| 10 | #include <drm/drm_damage_helper.h>
|
| 11 | #include <drm/drm_drv.h>
|
| 12 | #include <drm/drm_edid.h>
|
| 13 | #include <drm/drm_format_helper.h>
|
| 14 | #include <drm/drm_fourcc.h>
|
| 15 | #include <drm/drm_framebuffer.h>
|
| 16 | #include <drm/drm_gem_atomic_helper.h>
|
| 17 | #include <drm/drm_gem_framebuffer_helper.h>
|
| 18 | #include <drm/drm_gem_shmem_helper.h>
|
| 19 | #include <drm/drm_probe_helper.h>
|
| 20 | #include <drm/drm_panic.h>
|
| 21 | #include <drm/drm_plane.h>
|
| 22 | #include <drm/drm_print.h>
|
| 23 | #include <drm/drm_vblank.h>
|
| 24 | #include <drm/drm_vblank_helper.h>
|
| 25 |
|
| 26 | #include "hyperv_drm.h"
|
| 27 |
|
| 28 | static int hv_drm_blit_to_vram_rect(struct drm_framebuffer *fb,
|
| 29 | const struct iosys_map *vmap,
|
| 30 | struct drm_rect *rect)
|
| 31 | {
|
| 32 | struct hv_drm_device *hv = to_hv_drm(fb->dev);
|
| 33 | struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(hv->vram);
|
| 34 | int idx;
|
| 35 |
|
| 36 | if (!drm_dev_enter(&hv->dev, &idx))
|
| 37 | return -ENODEV;
|
| 38 |
|
| 39 | iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, rect));
|
| 40 | drm_fb_memcpy(&dst, fb->pitches, vmap, fb, rect);
|
| 41 |
|
| 42 | drm_dev_exit(idx);
|
| 43 |
|
| 44 | return 0;
|
| 45 | }
|
| 46 |
|
| 47 | static int hv_drm_connector_get_modes(struct drm_connector *connector)
|
| 48 | {
|
| 49 | struct hv_drm_device *hv = to_hv_drm(connector->dev);
|
| 50 | int count;
|
| 51 |
|
| 52 | count = drm_add_modes_noedid(connector,
|
| 53 | connector->dev->mode_config.max_width,
|
| 54 | connector->dev->mode_config.max_height);
|
| 55 | drm_set_preferred_mode(connector, hv->preferred_width,
|
| 56 | hv->preferred_height);
|
| 57 |
|
| 58 | return count;
|
| 59 | }
|
| 60 |
|
| 61 | static const struct drm_connector_helper_funcs hv_drm_connector_helper_funcs = {
|
| 62 | .get_modes = hv_drm_connector_get_modes,
|
| 63 | };
|
| 64 |
|
| 65 | static const struct drm_connector_funcs hv_drm_connector_funcs = {
|
| 66 | .fill_modes = drm_helper_probe_single_connector_modes,
|
| 67 | .destroy = drm_connector_cleanup,
|
| 68 | .reset = drm_atomic_helper_connector_reset,
|
| 69 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
|
| 70 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
|
| 71 | };
|
| 72 |
|
| 73 | static inline int hv_drm_conn_init(struct hv_drm_device *hv)
|
| 74 | {
|
| 75 | drm_connector_helper_add(&hv->connector, &hv_drm_connector_helper_funcs);
|
| 76 | return drm_connector_init(&hv->dev, &hv->connector,
|
| 77 | &hv_drm_connector_funcs,
|
| 78 | DRM_MODE_CONNECTOR_VIRTUAL);
|
| 79 | }
|
| 80 |
|
| 81 | static int hv_drm_check_size(struct hv_drm_device *hv, int w, int h,
|
| 82 | struct drm_framebuffer *fb)
|
| 83 | {
|
| 84 | u32 pitch = w * (hv->screen_depth / 8);
|
| 85 |
|
| 86 | if (fb)
|
| 87 | pitch = fb->pitches[0];
|
| 88 |
|
| 89 | if (pitch * h > hv->fb_size)
|
| 90 | return -EINVAL;
|
| 91 |
|
| 92 | return 0;
|
| 93 | }
|
| 94 |
|
| 95 | static const uint32_t hv_drm_formats[] = {
|
| 96 | DRM_FORMAT_XRGB8888,
|
| 97 | };
|
| 98 |
|
| 99 | static const uint64_t hv_drm_modifiers[] = {
|
| 100 | DRM_FORMAT_MOD_LINEAR,
|
| 101 | DRM_FORMAT_MOD_INVALID
|
| 102 | };
|
| 103 |
|
| 104 | static void hv_drm_crtc_helper_atomic_enable(struct drm_crtc *crtc,
|
| 105 | struct drm_atomic_commit *state)
|
| 106 | {
|
| 107 | struct hv_drm_device *hv = to_hv_drm(crtc->dev);
|
| 108 | struct drm_plane *plane = &hv->plane;
|
| 109 | struct drm_plane_state *plane_state = plane->state;
|
| 110 | struct drm_crtc_state *crtc_state = crtc->state;
|
| 111 |
|
| 112 | hv_drm_hide_hw_ptr(hv->hdev);
|
| 113 | hv_drm_update_situation(hv->hdev, 1, hv->screen_depth,
|
| 114 | crtc_state->mode.hdisplay,
|
| 115 | crtc_state->mode.vdisplay,
|
| 116 | plane_state->fb->pitches[0]);
|
| 117 |
|
| 118 | drm_crtc_vblank_on(crtc);
|
| 119 | }
|
| 120 |
|
| 121 | static const struct drm_crtc_helper_funcs hv_drm_crtc_helper_funcs = {
|
| 122 | .atomic_check = drm_crtc_helper_atomic_check,
|
| 123 | .atomic_flush = drm_crtc_vblank_atomic_flush,
|
| 124 | .atomic_enable = hv_drm_crtc_helper_atomic_enable,
|
| 125 | .atomic_disable = drm_crtc_vblank_atomic_disable,
|
| 126 | };
|
| 127 |
|
| 128 | static const struct drm_crtc_funcs hv_drm_crtc_funcs = {
|
| 129 | .reset = drm_atomic_helper_crtc_reset,
|
| 130 | .destroy = drm_crtc_cleanup,
|
| 131 | .set_config = drm_atomic_helper_set_config,
|
| 132 | .page_flip = drm_atomic_helper_page_flip,
|
| 133 | .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
|
| 134 | .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
|
| 135 | DRM_CRTC_VBLANK_TIMER_FUNCS,
|
| 136 | };
|
| 137 |
|
| 138 | static int hv_drm_plane_atomic_check(struct drm_plane *plane,
|
| 139 | struct drm_atomic_commit *state)
|
| 140 | {
|
| 141 | struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
|
| 142 | struct hv_drm_device *hv = to_hv_drm(plane->dev);
|
| 143 | struct drm_framebuffer *fb = plane_state->fb;
|
| 144 | struct drm_crtc *crtc = plane_state->crtc;
|
| 145 | struct drm_crtc_state *crtc_state = NULL;
|
| 146 | int ret;
|
| 147 |
|
| 148 | if (crtc)
|
| 149 | crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
|
| 150 |
|
| 151 | ret = drm_atomic_helper_check_plane_state(plane_state, crtc_state,
|
| 152 | DRM_PLANE_NO_SCALING,
|
| 153 | DRM_PLANE_NO_SCALING,
|
| 154 | false, false);
|
| 155 | if (ret)
|
| 156 | return ret;
|
| 157 |
|
| 158 | if (!plane_state->visible)
|
| 159 | return 0;
|
| 160 |
|
| 161 | if (fb->pitches[0] * fb->height > hv->fb_size) {
|
| 162 | drm_err(&hv->dev, "fb size requested by %s for %dX%d (pitch %d) greater than %ld\n",
|
| 163 | current->comm, fb->width, fb->height, fb->pitches[0], hv->fb_size);
|
| 164 | return -EINVAL;
|
| 165 | }
|
| 166 |
|
| 167 | return 0;
|
| 168 | }
|
| 169 |
|
| 170 | static void hv_drm_plane_atomic_update(struct drm_plane *plane,
|
| 171 | struct drm_atomic_commit *state)
|
| 172 | {
|
| 173 | struct hv_drm_device *hv = to_hv_drm(plane->dev);
|
| 174 | struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
|
| 175 | struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
|
| 176 | struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(new_state);
|
| 177 | struct drm_rect damage;
|
| 178 | struct drm_rect dst_clip;
|
| 179 | struct drm_atomic_helper_damage_iter iter;
|
| 180 |
|
| 181 | drm_atomic_helper_damage_iter_init(&iter, old_state, new_state);
|
| 182 | drm_atomic_for_each_plane_damage(&iter, &damage) {
|
| 183 | dst_clip = new_state->dst;
|
| 184 |
|
| 185 | if (!drm_rect_intersect(&dst_clip, &damage))
|
| 186 | continue;
|
| 187 |
|
| 188 | hv_drm_blit_to_vram_rect(new_state->fb, &shadow_plane_state->data[0], &damage);
|
| 189 | hv_drm_update_dirt(hv->hdev, &damage);
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | static int hv_drm_plane_get_scanout_buffer(struct drm_plane *plane,
|
| 194 | struct drm_scanout_buffer *sb)
|
| 195 | {
|
| 196 | struct hv_drm_device *hv = to_hv_drm(plane->dev);
|
| 197 | struct iosys_map map = IOSYS_MAP_INIT_VADDR_IOMEM(hv->vram);
|
| 198 |
|
| 199 | if (plane->state && plane->state->fb) {
|
| 200 | sb->format = plane->state->fb->format;
|
| 201 | sb->width = plane->state->fb->width;
|
| 202 | sb->height = plane->state->fb->height;
|
| 203 | sb->pitch[0] = plane->state->fb->pitches[0];
|
| 204 | sb->map[0] = map;
|
| 205 | return 0;
|
| 206 | }
|
| 207 | return -ENODEV;
|
| 208 | }
|
| 209 |
|
| 210 | static void hv_drm_plane_panic_flush(struct drm_plane *plane)
|
| 211 | {
|
| 212 | struct hv_drm_device *hv = to_hv_drm(plane->dev);
|
| 213 | struct drm_rect rect;
|
| 214 |
|
| 215 | if (plane->state && plane->state->fb) {
|
| 216 | rect.x1 = 0;
|
| 217 | rect.y1 = 0;
|
| 218 | rect.x2 = plane->state->fb->width;
|
| 219 | rect.y2 = plane->state->fb->height;
|
| 220 |
|
| 221 | hv_drm_update_dirt(hv->hdev, &rect);
|
| 222 | }
|
| 223 |
|
| 224 | vmbus_initiate_unload(true);
|
| 225 | }
|
| 226 |
|
| 227 | static const struct drm_plane_helper_funcs hv_drm_plane_helper_funcs = {
|
| 228 | DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
|
| 229 | .atomic_check = hv_drm_plane_atomic_check,
|
| 230 | .atomic_update = hv_drm_plane_atomic_update,
|
| 231 | .get_scanout_buffer = hv_drm_plane_get_scanout_buffer,
|
| 232 | .panic_flush = hv_drm_plane_panic_flush,
|
| 233 | };
|
| 234 |
|
| 235 | static const struct drm_plane_funcs hv_drm_plane_funcs = {
|
| 236 | .update_plane = drm_atomic_helper_update_plane,
|
| 237 | .disable_plane = drm_atomic_helper_disable_plane,
|
| 238 | .destroy = drm_plane_cleanup,
|
| 239 | DRM_GEM_SHADOW_PLANE_FUNCS,
|
| 240 | };
|
| 241 |
|
| 242 | static const struct drm_encoder_funcs hv_drm_simple_encoder_funcs_cleanup = {
|
| 243 | .destroy = drm_encoder_cleanup,
|
| 244 | };
|
| 245 |
|
| 246 | static inline int hv_drm_pipe_init(struct hv_drm_device *hv)
|
| 247 | {
|
| 248 | struct drm_device *dev = &hv->dev;
|
| 249 | struct drm_encoder *encoder = &hv->encoder;
|
| 250 | struct drm_plane *plane = &hv->plane;
|
| 251 | struct drm_crtc *crtc = &hv->crtc;
|
| 252 | struct drm_connector *connector = &hv->connector;
|
| 253 | int ret;
|
| 254 |
|
| 255 | ret = drm_universal_plane_init(dev, plane, 0,
|
| 256 | &hv_drm_plane_funcs,
|
| 257 | hv_drm_formats, ARRAY_SIZE(hv_drm_formats),
|
| 258 | hv_drm_modifiers,
|
| 259 | DRM_PLANE_TYPE_PRIMARY, NULL);
|
| 260 | if (ret)
|
| 261 | return ret;
|
| 262 | drm_plane_helper_add(plane, &hv_drm_plane_helper_funcs);
|
| 263 | drm_plane_enable_fb_damage_clips(plane);
|
| 264 |
|
| 265 | ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
|
| 266 | &hv_drm_crtc_funcs, NULL);
|
| 267 | if (ret)
|
| 268 | return ret;
|
| 269 | drm_crtc_helper_add(crtc, &hv_drm_crtc_helper_funcs);
|
| 270 |
|
| 271 | encoder->possible_crtcs = drm_crtc_mask(crtc);
|
| 272 | ret = drm_encoder_init(dev, encoder,
|
| 273 | &hv_drm_simple_encoder_funcs_cleanup,
|
| 274 | DRM_MODE_ENCODER_NONE, NULL);
|
| 275 | if (ret)
|
| 276 | return ret;
|
| 277 |
|
| 278 | ret = hv_drm_conn_init(hv);
|
| 279 | if (ret) {
|
| 280 | drm_err(dev, "Failed to initialized connector.\n");
|
| 281 | return ret;
|
| 282 | }
|
| 283 |
|
| 284 | return drm_connector_attach_encoder(connector, encoder);
|
| 285 | }
|
| 286 |
|
| 287 | static enum drm_mode_status
|
| 288 | hv_drm_mode_valid(struct drm_device *dev,
|
| 289 | const struct drm_display_mode *mode)
|
| 290 | {
|
| 291 | struct hv_drm_device *hv = to_hv_drm(dev);
|
| 292 |
|
| 293 | if (hv_drm_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
|
| 294 | return MODE_BAD;
|
| 295 |
|
| 296 | return MODE_OK;
|
| 297 | }
|
| 298 |
|
| 299 | static const struct drm_mode_config_funcs hv_drm_mode_config_funcs = {
|
| 300 | .fb_create = drm_gem_fb_create_with_dirty,
|
| 301 | .mode_valid = hv_drm_mode_valid,
|
| 302 | .atomic_check = drm_atomic_helper_check,
|
| 303 | .atomic_commit = drm_atomic_helper_commit,
|
| 304 | };
|
| 305 |
|
| 306 | int hv_drm_mode_config_init(struct hv_drm_device *hv)
|
| 307 | {
|
| 308 | struct drm_device *dev = &hv->dev;
|
| 309 | int ret;
|
| 310 |
|
| 311 | ret = drmm_mode_config_init(dev);
|
| 312 | if (ret) {
|
| 313 | drm_err(dev, "Failed to initialized mode setting.\n");
|
| 314 | return ret;
|
| 315 | }
|
| 316 |
|
| 317 | dev->mode_config.min_width = 0;
|
| 318 | dev->mode_config.min_height = 0;
|
| 319 | dev->mode_config.max_width = hv->screen_width_max;
|
| 320 | dev->mode_config.max_height = hv->screen_height_max;
|
| 321 |
|
| 322 | dev->mode_config.preferred_depth = hv->screen_depth;
|
| 323 | dev->mode_config.prefer_shadow = 0;
|
| 324 |
|
| 325 | dev->mode_config.funcs = &hv_drm_mode_config_funcs;
|
| 326 |
|
| 327 | ret = hv_drm_pipe_init(hv);
|
| 328 | if (ret) {
|
| 329 | drm_err(dev, "Failed to initialized pipe.\n");
|
| 330 | return ret;
|
| 331 | }
|
| 332 |
|
| 333 | ret = drm_vblank_init(dev, 1);
|
| 334 | if (ret)
|
| 335 | return ret;
|
| 336 |
|
| 337 | drm_mode_config_reset(dev);
|
| 338 |
|
| 339 | return 0;
|
| 340 | }
|