linux/drivers/gpu/drm/hyperv/hyperv_drm_modeset.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright 2021 Microsoft
   4 */
   5
   6#include <linux/hyperv.h>
   7
   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>
  18
  19#include "hyperv_drm.h"
  20
  21static int hyperv_blit_to_vram_rect(struct drm_framebuffer *fb,
  22                                    const struct dma_buf_map *map,
  23                                    struct drm_rect *rect)
  24{
  25        struct hyperv_drm_device *hv = to_hv(fb->dev);
  26        void *vmap = map->vaddr; /* TODO: Use mapping abstraction properly */
  27        int idx;
  28
  29        if (!drm_dev_enter(&hv->dev, &idx))
  30                return -ENODEV;
  31
  32        drm_fb_memcpy_dstclip(hv->vram, fb->pitches[0], vmap, fb, rect);
  33        drm_dev_exit(idx);
  34
  35        return 0;
  36}
  37
  38static int hyperv_blit_to_vram_fullscreen(struct drm_framebuffer *fb, const struct dma_buf_map *map)
  39{
  40        struct drm_rect fullscreen = {
  41                .x1 = 0,
  42                .x2 = fb->width,
  43                .y1 = 0,
  44                .y2 = fb->height,
  45        };
  46        return hyperv_blit_to_vram_rect(fb, map, &fullscreen);
  47}
  48
  49static int hyperv_connector_get_modes(struct drm_connector *connector)
  50{
  51        struct hyperv_drm_device *hv = to_hv(connector->dev);
  52        int count;
  53
  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);
  59
  60        return count;
  61}
  62
  63static const struct drm_connector_helper_funcs hyperv_connector_helper_funcs = {
  64        .get_modes = hyperv_connector_get_modes,
  65};
  66
  67static 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,
  73};
  74
  75static inline int hyperv_conn_init(struct hyperv_drm_device *hv)
  76{
  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);
  81}
  82
  83static int hyperv_check_size(struct hyperv_drm_device *hv, int w, int h,
  84                             struct drm_framebuffer *fb)
  85{
  86        u32 pitch = w * (hv->screen_depth / 8);
  87
  88        if (fb)
  89                pitch = fb->pitches[0];
  90
  91        if (pitch * h > hv->fb_size)
  92                return -EINVAL;
  93
  94        return 0;
  95}
  96
  97static void hyperv_pipe_enable(struct drm_simple_display_pipe *pipe,
  98                               struct drm_crtc_state *crtc_state,
  99                               struct drm_plane_state *plane_state)
 100{
 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);
 103
 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->map[0]);
 109}
 110
 111static int hyperv_pipe_check(struct drm_simple_display_pipe *pipe,
 112                             struct drm_plane_state *plane_state,
 113                             struct drm_crtc_state *crtc_state)
 114{
 115        struct hyperv_drm_device *hv = to_hv(pipe->crtc.dev);
 116        struct drm_framebuffer *fb = plane_state->fb;
 117
 118        if (fb->format->format != DRM_FORMAT_XRGB8888)
 119                return -EINVAL;
 120
 121        if (fb->pitches[0] * fb->height > hv->fb_size)
 122                return -EINVAL;
 123
 124        return 0;
 125}
 126
 127static void hyperv_pipe_update(struct drm_simple_display_pipe *pipe,
 128                               struct drm_plane_state *old_state)
 129{
 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;
 134
 135        if (drm_atomic_helper_damage_merged(old_state, state, &rect)) {
 136                hyperv_blit_to_vram_rect(state->fb, &shadow_plane_state->map[0], &rect);
 137                hyperv_update_dirt(hv->hdev, &rect);
 138        }
 139}
 140
 141static 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,
 146};
 147
 148static const uint32_t hyperv_formats[] = {
 149        DRM_FORMAT_XRGB8888,
 150};
 151
 152static const uint64_t hyperv_modifiers[] = {
 153        DRM_FORMAT_MOD_LINEAR,
 154        DRM_FORMAT_MOD_INVALID
 155};
 156
 157static inline int hyperv_pipe_init(struct hyperv_drm_device *hv)
 158{
 159        int ret;
 160
 161        ret = drm_simple_display_pipe_init(&hv->dev,
 162                                           &hv->pipe,
 163                                           &hyperv_pipe_funcs,
 164                                           hyperv_formats,
 165                                           ARRAY_SIZE(hyperv_formats),
 166                                           hyperv_modifiers,
 167                                           &hv->connector);
 168        if (ret)
 169                return ret;
 170
 171        drm_plane_enable_fb_damage_clips(&hv->pipe.plane);
 172
 173        return 0;
 174}
 175
 176static enum drm_mode_status
 177hyperv_mode_valid(struct drm_device *dev,
 178                  const struct drm_display_mode *mode)
 179{
 180        struct hyperv_drm_device *hv = to_hv(dev);
 181
 182        if (hyperv_check_size(hv, mode->hdisplay, mode->vdisplay, NULL))
 183                return MODE_BAD;
 184
 185        return MODE_OK;
 186}
 187
 188static 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,
 193};
 194
 195int hyperv_mode_config_init(struct hyperv_drm_device *hv)
 196{
 197        struct drm_device *dev = &hv->dev;
 198        int ret;
 199
 200        ret = drmm_mode_config_init(dev);
 201        if (ret) {
 202                drm_err(dev, "Failed to initialized mode setting.\n");
 203                return ret;
 204        }
 205
 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;
 210
 211        dev->mode_config.preferred_depth = hv->screen_depth;
 212        dev->mode_config.prefer_shadow = 0;
 213
 214        dev->mode_config.funcs = &hyperv_mode_config_funcs;
 215
 216        ret = hyperv_conn_init(hv);
 217        if (ret) {
 218                drm_err(dev, "Failed to initialized connector.\n");
 219                return ret;
 220        }
 221
 222        ret = hyperv_pipe_init(hv);
 223        if (ret) {
 224                drm_err(dev, "Failed to initialized pipe.\n");
 225                return ret;
 226        }
 227
 228        drm_mode_config_reset(dev);
 229
 230        return 0;
 231}
 232