Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2015 Red Hat, Inc.
0003  * All Rights Reserved.
0004  *
0005  * Authors:
0006  *    Dave Airlie
0007  *    Alon Levy
0008  *
0009  * Permission is hereby granted, free of charge, to any person obtaining a
0010  * copy of this software and associated documentation files (the "Software"),
0011  * to deal in the Software without restriction, including without limitation
0012  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0013  * and/or sell copies of the Software, and to permit persons to whom the
0014  * Software is furnished to do so, subject to the following conditions:
0015  *
0016  * The above copyright notice and this permission notice shall be included in
0017  * all copies or substantial portions of the Software.
0018  *
0019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0022  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0023  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0024  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0025  * OTHER DEALINGS IN THE SOFTWARE.
0026  */
0027 
0028 #include <drm/drm_atomic_helper.h>
0029 #include <drm/drm_damage_helper.h>
0030 #include <drm/drm_edid.h>
0031 #include <drm/drm_fourcc.h>
0032 #include <drm/drm_gem_framebuffer_helper.h>
0033 #include <drm/drm_probe_helper.h>
0034 #include <drm/drm_simple_kms_helper.h>
0035 
0036 #include "virtgpu_drv.h"
0037 
0038 #define XRES_MIN    32
0039 #define YRES_MIN    32
0040 
0041 #define XRES_DEF  1024
0042 #define YRES_DEF   768
0043 
0044 #define XRES_MAX  8192
0045 #define YRES_MAX  8192
0046 
0047 #define drm_connector_to_virtio_gpu_output(x) \
0048     container_of(x, struct virtio_gpu_output, conn)
0049 
0050 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
0051     .set_config             = drm_atomic_helper_set_config,
0052     .destroy                = drm_crtc_cleanup,
0053 
0054     .page_flip              = drm_atomic_helper_page_flip,
0055     .reset                  = drm_atomic_helper_crtc_reset,
0056     .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
0057     .atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
0058 };
0059 
0060 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
0061     .create_handle = drm_gem_fb_create_handle,
0062     .destroy = drm_gem_fb_destroy,
0063     .dirty = drm_atomic_helper_dirtyfb,
0064 };
0065 
0066 static int
0067 virtio_gpu_framebuffer_init(struct drm_device *dev,
0068                 struct virtio_gpu_framebuffer *vgfb,
0069                 const struct drm_mode_fb_cmd2 *mode_cmd,
0070                 struct drm_gem_object *obj)
0071 {
0072     int ret;
0073 
0074     vgfb->base.obj[0] = obj;
0075 
0076     drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
0077 
0078     ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
0079     if (ret) {
0080         vgfb->base.obj[0] = NULL;
0081         return ret;
0082     }
0083     return 0;
0084 }
0085 
0086 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
0087 {
0088     struct drm_device *dev = crtc->dev;
0089     struct virtio_gpu_device *vgdev = dev->dev_private;
0090     struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
0091 
0092     virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
0093                    crtc->mode.hdisplay,
0094                    crtc->mode.vdisplay, 0, 0);
0095     virtio_gpu_notify(vgdev);
0096 }
0097 
0098 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
0099                       struct drm_atomic_state *state)
0100 {
0101 }
0102 
0103 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
0104                        struct drm_atomic_state *state)
0105 {
0106     struct drm_device *dev = crtc->dev;
0107     struct virtio_gpu_device *vgdev = dev->dev_private;
0108     struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
0109 
0110     virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
0111     virtio_gpu_notify(vgdev);
0112 }
0113 
0114 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
0115                     struct drm_atomic_state *state)
0116 {
0117     return 0;
0118 }
0119 
0120 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
0121                      struct drm_atomic_state *state)
0122 {
0123     struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
0124                                       crtc);
0125     struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
0126 
0127     /*
0128      * virtio-gpu can't do modeset and plane update operations
0129      * independent from each other.  So the actual modeset happens
0130      * in the plane update callback, and here we just check
0131      * whenever we must force the modeset.
0132      */
0133     if (drm_atomic_crtc_needs_modeset(crtc_state)) {
0134         output->needs_modeset = true;
0135     }
0136 }
0137 
0138 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
0139     .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
0140     .atomic_check  = virtio_gpu_crtc_atomic_check,
0141     .atomic_flush  = virtio_gpu_crtc_atomic_flush,
0142     .atomic_enable = virtio_gpu_crtc_atomic_enable,
0143     .atomic_disable = virtio_gpu_crtc_atomic_disable,
0144 };
0145 
0146 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
0147                     struct drm_display_mode *mode,
0148                     struct drm_display_mode *adjusted_mode)
0149 {
0150 }
0151 
0152 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
0153 {
0154 }
0155 
0156 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
0157 {
0158 }
0159 
0160 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
0161 {
0162     struct virtio_gpu_output *output =
0163         drm_connector_to_virtio_gpu_output(connector);
0164     struct drm_display_mode *mode = NULL;
0165     int count, width, height;
0166 
0167     if (output->edid) {
0168         count = drm_add_edid_modes(connector, output->edid);
0169         if (count)
0170             return count;
0171     }
0172 
0173     width  = le32_to_cpu(output->info.r.width);
0174     height = le32_to_cpu(output->info.r.height);
0175     count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
0176 
0177     if (width == 0 || height == 0) {
0178         drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
0179     } else {
0180         DRM_DEBUG("add mode: %dx%d\n", width, height);
0181         mode = drm_cvt_mode(connector->dev, width, height, 60,
0182                     false, false, false);
0183         if (!mode)
0184             return count;
0185         mode->type |= DRM_MODE_TYPE_PREFERRED;
0186         drm_mode_probed_add(connector, mode);
0187         count++;
0188     }
0189 
0190     return count;
0191 }
0192 
0193 static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
0194                       struct drm_display_mode *mode)
0195 {
0196     struct virtio_gpu_output *output =
0197         drm_connector_to_virtio_gpu_output(connector);
0198     int width, height;
0199 
0200     width  = le32_to_cpu(output->info.r.width);
0201     height = le32_to_cpu(output->info.r.height);
0202 
0203     if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
0204         return MODE_OK;
0205     if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
0206         return MODE_OK;
0207     if (mode->hdisplay <= width  && mode->hdisplay >= width - 16 &&
0208         mode->vdisplay <= height && mode->vdisplay >= height - 16)
0209         return MODE_OK;
0210 
0211     DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
0212     return MODE_BAD;
0213 }
0214 
0215 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
0216     .mode_set   = virtio_gpu_enc_mode_set,
0217     .enable     = virtio_gpu_enc_enable,
0218     .disable    = virtio_gpu_enc_disable,
0219 };
0220 
0221 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
0222     .get_modes    = virtio_gpu_conn_get_modes,
0223     .mode_valid   = virtio_gpu_conn_mode_valid,
0224 };
0225 
0226 static enum drm_connector_status virtio_gpu_conn_detect(
0227             struct drm_connector *connector,
0228             bool force)
0229 {
0230     struct virtio_gpu_output *output =
0231         drm_connector_to_virtio_gpu_output(connector);
0232 
0233     if (output->info.enabled)
0234         return connector_status_connected;
0235     else
0236         return connector_status_disconnected;
0237 }
0238 
0239 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
0240 {
0241     drm_connector_unregister(connector);
0242     drm_connector_cleanup(connector);
0243 }
0244 
0245 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
0246     .detect = virtio_gpu_conn_detect,
0247     .fill_modes = drm_helper_probe_single_connector_modes,
0248     .destroy = virtio_gpu_conn_destroy,
0249     .reset = drm_atomic_helper_connector_reset,
0250     .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
0251     .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
0252 };
0253 
0254 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
0255 {
0256     struct drm_device *dev = vgdev->ddev;
0257     struct virtio_gpu_output *output = vgdev->outputs + index;
0258     struct drm_connector *connector = &output->conn;
0259     struct drm_encoder *encoder = &output->enc;
0260     struct drm_crtc *crtc = &output->crtc;
0261     struct drm_plane *primary, *cursor;
0262 
0263     output->index = index;
0264     if (index == 0) {
0265         output->info.enabled = cpu_to_le32(true);
0266         output->info.r.width = cpu_to_le32(XRES_DEF);
0267         output->info.r.height = cpu_to_le32(YRES_DEF);
0268     }
0269 
0270     primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
0271     if (IS_ERR(primary))
0272         return PTR_ERR(primary);
0273     cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
0274     if (IS_ERR(cursor))
0275         return PTR_ERR(cursor);
0276     drm_crtc_init_with_planes(dev, crtc, primary, cursor,
0277                   &virtio_gpu_crtc_funcs, NULL);
0278     drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
0279 
0280     drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
0281                DRM_MODE_CONNECTOR_VIRTUAL);
0282     drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
0283     if (vgdev->has_edid)
0284         drm_connector_attach_edid_property(connector);
0285 
0286     drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL);
0287     drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
0288     encoder->possible_crtcs = 1 << index;
0289 
0290     drm_connector_attach_encoder(connector, encoder);
0291     drm_connector_register(connector);
0292     return 0;
0293 }
0294 
0295 static struct drm_framebuffer *
0296 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
0297                    struct drm_file *file_priv,
0298                    const struct drm_mode_fb_cmd2 *mode_cmd)
0299 {
0300     struct drm_gem_object *obj = NULL;
0301     struct virtio_gpu_framebuffer *virtio_gpu_fb;
0302     int ret;
0303 
0304     if (mode_cmd->pixel_format != DRM_FORMAT_HOST_XRGB8888 &&
0305         mode_cmd->pixel_format != DRM_FORMAT_HOST_ARGB8888)
0306         return ERR_PTR(-ENOENT);
0307 
0308     /* lookup object associated with res handle */
0309     obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
0310     if (!obj)
0311         return ERR_PTR(-EINVAL);
0312 
0313     virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
0314     if (virtio_gpu_fb == NULL) {
0315         drm_gem_object_put(obj);
0316         return ERR_PTR(-ENOMEM);
0317     }
0318 
0319     ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
0320     if (ret) {
0321         kfree(virtio_gpu_fb);
0322         drm_gem_object_put(obj);
0323         return NULL;
0324     }
0325 
0326     return &virtio_gpu_fb->base;
0327 }
0328 
0329 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
0330     .fb_create = virtio_gpu_user_framebuffer_create,
0331     .atomic_check = drm_atomic_helper_check,
0332     .atomic_commit = drm_atomic_helper_commit,
0333 };
0334 
0335 int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
0336 {
0337     int i, ret;
0338 
0339     ret = drmm_mode_config_init(vgdev->ddev);
0340     if (ret)
0341         return ret;
0342 
0343     vgdev->ddev->mode_config.quirk_addfb_prefer_host_byte_order = true;
0344     vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
0345 
0346     /* modes will be validated against the framebuffer size */
0347     vgdev->ddev->mode_config.min_width = XRES_MIN;
0348     vgdev->ddev->mode_config.min_height = YRES_MIN;
0349     vgdev->ddev->mode_config.max_width = XRES_MAX;
0350     vgdev->ddev->mode_config.max_height = YRES_MAX;
0351 
0352     for (i = 0 ; i < vgdev->num_scanouts; ++i)
0353         vgdev_output_init(vgdev, i);
0354 
0355     drm_mode_config_reset(vgdev->ddev);
0356     return 0;
0357 }
0358 
0359 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
0360 {
0361     int i;
0362 
0363     for (i = 0 ; i < vgdev->num_scanouts; ++i)
0364         kfree(vgdev->outputs[i].edid);
0365 }