linux/drivers/gpu/drm/virtio/virtgpu_display.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2015 Red Hat, Inc.
   3 * All Rights Reserved.
   4 *
   5 * Authors:
   6 *    Dave Airlie
   7 *    Alon Levy
   8 *
   9 * Permission is hereby granted, free of charge, to any person obtaining a
  10 * copy of this software and associated documentation files (the "Software"),
  11 * to deal in the Software without restriction, including without limitation
  12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13 * and/or sell copies of the Software, and to permit persons to whom the
  14 * Software is furnished to do so, subject to the following conditions:
  15 *
  16 * The above copyright notice and this permission notice shall be included in
  17 * all copies or substantial portions of the Software.
  18 *
  19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25 * OTHER DEALINGS IN THE SOFTWARE.
  26 */
  27
  28#include <drm/drm_atomic_helper.h>
  29#include <drm/drm_damage_helper.h>
  30#include <drm/drm_fourcc.h>
  31#include <drm/drm_gem_framebuffer_helper.h>
  32#include <drm/drm_probe_helper.h>
  33#include <drm/drm_simple_kms_helper.h>
  34
  35#include "virtgpu_drv.h"
  36
  37#define XRES_MIN    32
  38#define YRES_MIN    32
  39
  40#define XRES_DEF  1024
  41#define YRES_DEF   768
  42
  43#define XRES_MAX  8192
  44#define YRES_MAX  8192
  45
  46#define drm_connector_to_virtio_gpu_output(x) \
  47        container_of(x, struct virtio_gpu_output, conn)
  48
  49static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
  50        .set_config             = drm_atomic_helper_set_config,
  51        .destroy                = drm_crtc_cleanup,
  52
  53        .page_flip              = drm_atomic_helper_page_flip,
  54        .reset                  = drm_atomic_helper_crtc_reset,
  55        .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  56        .atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
  57};
  58
  59static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
  60        .create_handle = drm_gem_fb_create_handle,
  61        .destroy = drm_gem_fb_destroy,
  62        .dirty = drm_atomic_helper_dirtyfb,
  63};
  64
  65static int
  66virtio_gpu_framebuffer_init(struct drm_device *dev,
  67                            struct virtio_gpu_framebuffer *vgfb,
  68                            const struct drm_mode_fb_cmd2 *mode_cmd,
  69                            struct drm_gem_object *obj)
  70{
  71        int ret;
  72
  73        vgfb->base.obj[0] = obj;
  74
  75        drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
  76
  77        ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
  78        if (ret) {
  79                vgfb->base.obj[0] = NULL;
  80                return ret;
  81        }
  82        return 0;
  83}
  84
  85static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
  86{
  87        struct drm_device *dev = crtc->dev;
  88        struct virtio_gpu_device *vgdev = dev->dev_private;
  89        struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
  90
  91        virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
  92                                   crtc->mode.hdisplay,
  93                                   crtc->mode.vdisplay, 0, 0);
  94        virtio_gpu_notify(vgdev);
  95}
  96
  97static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
  98                                          struct drm_crtc_state *old_state)
  99{
 100}
 101
 102static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
 103                                           struct drm_crtc_state *old_state)
 104{
 105        struct drm_device *dev = crtc->dev;
 106        struct virtio_gpu_device *vgdev = dev->dev_private;
 107        struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
 108
 109        virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
 110        virtio_gpu_notify(vgdev);
 111}
 112
 113static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
 114                                        struct drm_crtc_state *state)
 115{
 116        return 0;
 117}
 118
 119static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
 120                                         struct drm_crtc_state *old_state)
 121{
 122        struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
 123
 124        /*
 125         * virtio-gpu can't do modeset and plane update operations
 126         * independent from each other.  So the actual modeset happens
 127         * in the plane update callback, and here we just check
 128         * whenever we must force the modeset.
 129         */
 130        if (drm_atomic_crtc_needs_modeset(crtc->state)) {
 131                output->needs_modeset = true;
 132        }
 133}
 134
 135static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
 136        .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
 137        .atomic_check  = virtio_gpu_crtc_atomic_check,
 138        .atomic_flush  = virtio_gpu_crtc_atomic_flush,
 139        .atomic_enable = virtio_gpu_crtc_atomic_enable,
 140        .atomic_disable = virtio_gpu_crtc_atomic_disable,
 141};
 142
 143static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
 144                                    struct drm_display_mode *mode,
 145                                    struct drm_display_mode *adjusted_mode)
 146{
 147}
 148
 149static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
 150{
 151}
 152
 153static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
 154{
 155}
 156
 157static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
 158{
 159        struct virtio_gpu_output *output =
 160                drm_connector_to_virtio_gpu_output(connector);
 161        struct drm_display_mode *mode = NULL;
 162        int count, width, height;
 163
 164        if (output->edid) {
 165                count = drm_add_edid_modes(connector, output->edid);
 166                if (count)
 167                        return count;
 168        }
 169
 170        width  = le32_to_cpu(output->info.r.width);
 171        height = le32_to_cpu(output->info.r.height);
 172        count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
 173
 174        if (width == 0 || height == 0) {
 175                width = XRES_DEF;
 176                height = YRES_DEF;
 177                drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
 178        } else {
 179                DRM_DEBUG("add mode: %dx%d\n", width, height);
 180                mode = drm_cvt_mode(connector->dev, width, height, 60,
 181                                    false, false, false);
 182                mode->type |= DRM_MODE_TYPE_PREFERRED;
 183                drm_mode_probed_add(connector, mode);
 184                count++;
 185        }
 186
 187        return count;
 188}
 189
 190static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
 191                                      struct drm_display_mode *mode)
 192{
 193        struct virtio_gpu_output *output =
 194                drm_connector_to_virtio_gpu_output(connector);
 195        int width, height;
 196
 197        width  = le32_to_cpu(output->info.r.width);
 198        height = le32_to_cpu(output->info.r.height);
 199
 200        if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
 201                return MODE_OK;
 202        if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
 203                return MODE_OK;
 204        if (mode->hdisplay <= width  && mode->hdisplay >= width - 16 &&
 205            mode->vdisplay <= height && mode->vdisplay >= height - 16)
 206                return MODE_OK;
 207
 208        DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
 209        return MODE_BAD;
 210}
 211
 212static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
 213        .mode_set   = virtio_gpu_enc_mode_set,
 214        .enable     = virtio_gpu_enc_enable,
 215        .disable    = virtio_gpu_enc_disable,
 216};
 217
 218static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
 219        .get_modes    = virtio_gpu_conn_get_modes,
 220        .mode_valid   = virtio_gpu_conn_mode_valid,
 221};
 222
 223static enum drm_connector_status virtio_gpu_conn_detect(
 224                        struct drm_connector *connector,
 225                        bool force)
 226{
 227        struct virtio_gpu_output *output =
 228                drm_connector_to_virtio_gpu_output(connector);
 229
 230        if (output->info.enabled)
 231                return connector_status_connected;
 232        else
 233                return connector_status_disconnected;
 234}
 235
 236static void virtio_gpu_conn_destroy(struct drm_connector *connector)
 237{
 238        drm_connector_unregister(connector);
 239        drm_connector_cleanup(connector);
 240}
 241
 242static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
 243        .detect = virtio_gpu_conn_detect,
 244        .fill_modes = drm_helper_probe_single_connector_modes,
 245        .destroy = virtio_gpu_conn_destroy,
 246        .reset = drm_atomic_helper_connector_reset,
 247        .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
 248        .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
 249};
 250
 251static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
 252{
 253        struct drm_device *dev = vgdev->ddev;
 254        struct virtio_gpu_output *output = vgdev->outputs + index;
 255        struct drm_connector *connector = &output->conn;
 256        struct drm_encoder *encoder = &output->enc;
 257        struct drm_crtc *crtc = &output->crtc;
 258        struct drm_plane *primary, *cursor;
 259
 260        output->index = index;
 261        if (index == 0) {
 262                output->info.enabled = cpu_to_le32(true);
 263                output->info.r.width = cpu_to_le32(XRES_DEF);
 264                output->info.r.height = cpu_to_le32(YRES_DEF);
 265        }
 266
 267        primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
 268        if (IS_ERR(primary))
 269                return PTR_ERR(primary);
 270        cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
 271        if (IS_ERR(cursor))
 272                return PTR_ERR(cursor);
 273        drm_crtc_init_with_planes(dev, crtc, primary, cursor,
 274                                  &virtio_gpu_crtc_funcs, NULL);
 275        drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
 276
 277        drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
 278                           DRM_MODE_CONNECTOR_VIRTUAL);
 279        drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
 280        if (vgdev->has_edid)
 281                drm_connector_attach_edid_property(connector);
 282
 283        drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL);
 284        drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
 285        encoder->possible_crtcs = 1 << index;
 286
 287        drm_connector_attach_encoder(connector, encoder);
 288        drm_connector_register(connector);
 289        return 0;
 290}
 291
 292static struct drm_framebuffer *
 293virtio_gpu_user_framebuffer_create(struct drm_device *dev,
 294                                   struct drm_file *file_priv,
 295                                   const struct drm_mode_fb_cmd2 *mode_cmd)
 296{
 297        struct drm_gem_object *obj = NULL;
 298        struct virtio_gpu_framebuffer *virtio_gpu_fb;
 299        int ret;
 300
 301        if (mode_cmd->pixel_format != DRM_FORMAT_HOST_XRGB8888 &&
 302            mode_cmd->pixel_format != DRM_FORMAT_HOST_ARGB8888)
 303                return ERR_PTR(-ENOENT);
 304
 305        /* lookup object associated with res handle */
 306        obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
 307        if (!obj)
 308                return ERR_PTR(-EINVAL);
 309
 310        virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
 311        if (virtio_gpu_fb == NULL)
 312                return ERR_PTR(-ENOMEM);
 313
 314        ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
 315        if (ret) {
 316                kfree(virtio_gpu_fb);
 317                drm_gem_object_put(obj);
 318                return NULL;
 319        }
 320
 321        return &virtio_gpu_fb->base;
 322}
 323
 324static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
 325        .fb_create = virtio_gpu_user_framebuffer_create,
 326        .atomic_check = drm_atomic_helper_check,
 327        .atomic_commit = drm_atomic_helper_commit,
 328};
 329
 330void virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
 331{
 332        int i;
 333
 334        drm_mode_config_init(vgdev->ddev);
 335        vgdev->ddev->mode_config.quirk_addfb_prefer_host_byte_order = true;
 336        vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
 337
 338        /* modes will be validated against the framebuffer size */
 339        vgdev->ddev->mode_config.min_width = XRES_MIN;
 340        vgdev->ddev->mode_config.min_height = YRES_MIN;
 341        vgdev->ddev->mode_config.max_width = XRES_MAX;
 342        vgdev->ddev->mode_config.max_height = YRES_MAX;
 343
 344        for (i = 0 ; i < vgdev->num_scanouts; ++i)
 345                vgdev_output_init(vgdev, i);
 346
 347        drm_mode_config_reset(vgdev->ddev);
 348}
 349
 350void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
 351{
 352        int i;
 353
 354        for (i = 0 ; i < vgdev->num_scanouts; ++i)
 355                kfree(vgdev->outputs[i].edid);
 356        drm_mode_config_cleanup(vgdev->ddev);
 357}
 358