| 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_edid.h> | 
|---|
| 31 | #include <drm/drm_fourcc.h> | 
|---|
| 32 | #include <drm/drm_gem_framebuffer_helper.h> | 
|---|
| 33 | #include <drm/drm_probe_helper.h> | 
|---|
| 34 | #include <drm/drm_simple_kms_helper.h> | 
|---|
| 35 |  | 
|---|
| 36 | #include "virtgpu_drv.h" | 
|---|
| 37 |  | 
|---|
| 38 | #define XRES_MIN    32 | 
|---|
| 39 | #define YRES_MIN    32 | 
|---|
| 40 |  | 
|---|
| 41 | #define XRES_DEF  1024 | 
|---|
| 42 | #define YRES_DEF   768 | 
|---|
| 43 |  | 
|---|
| 44 | #define XRES_MAX  8192 | 
|---|
| 45 | #define YRES_MAX  8192 | 
|---|
| 46 |  | 
|---|
| 47 | #define drm_connector_to_virtio_gpu_output(x) \ | 
|---|
| 48 | container_of(x, struct virtio_gpu_output, conn) | 
|---|
| 49 |  | 
|---|
| 50 | static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = { | 
|---|
| 51 | .set_config             = drm_atomic_helper_set_config, | 
|---|
| 52 | .destroy                = drm_crtc_cleanup, | 
|---|
| 53 |  | 
|---|
| 54 | .page_flip              = drm_atomic_helper_page_flip, | 
|---|
| 55 | .reset                  = drm_atomic_helper_crtc_reset, | 
|---|
| 56 | .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, | 
|---|
| 57 | .atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state, | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = { | 
|---|
| 61 | .create_handle = drm_gem_fb_create_handle, | 
|---|
| 62 | .destroy = drm_gem_fb_destroy, | 
|---|
| 63 | .dirty = drm_atomic_helper_dirtyfb, | 
|---|
| 64 | }; | 
|---|
| 65 |  | 
|---|
| 66 | static int | 
|---|
| 67 | virtio_gpu_framebuffer_init(struct drm_device *dev, | 
|---|
| 68 | struct virtio_gpu_framebuffer *vgfb, | 
|---|
| 69 | const struct drm_format_info *info, | 
|---|
| 70 | const struct drm_mode_fb_cmd2 *mode_cmd, | 
|---|
| 71 | struct drm_gem_object *obj) | 
|---|
| 72 | { | 
|---|
| 73 | int ret; | 
|---|
| 74 |  | 
|---|
| 75 | vgfb->base.obj[0] = obj; | 
|---|
| 76 |  | 
|---|
| 77 | drm_helper_mode_fill_fb_struct(dev, fb: &vgfb->base, info, mode_cmd); | 
|---|
| 78 |  | 
|---|
| 79 | ret = drm_framebuffer_init(dev, fb: &vgfb->base, funcs: &virtio_gpu_fb_funcs); | 
|---|
| 80 | if (ret) { | 
|---|
| 81 | vgfb->base.obj[0] = NULL; | 
|---|
| 82 | return ret; | 
|---|
| 83 | } | 
|---|
| 84 | return 0; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc) | 
|---|
| 88 | { | 
|---|
| 89 | struct drm_device *dev = crtc->dev; | 
|---|
| 90 | struct virtio_gpu_device *vgdev = dev->dev_private; | 
|---|
| 91 | struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc); | 
|---|
| 92 |  | 
|---|
| 93 | virtio_gpu_cmd_set_scanout(vgdev, scanout_id: output->index, resource_id: 0, | 
|---|
| 94 | width: crtc->mode.hdisplay, | 
|---|
| 95 | height: crtc->mode.vdisplay, x: 0, y: 0); | 
|---|
| 96 | virtio_gpu_notify(vgdev); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc, | 
|---|
| 100 | struct drm_atomic_state *state) | 
|---|
| 101 | { | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc, | 
|---|
| 105 | struct drm_atomic_state *state) | 
|---|
| 106 | { | 
|---|
| 107 | struct drm_device *dev = crtc->dev; | 
|---|
| 108 | struct virtio_gpu_device *vgdev = dev->dev_private; | 
|---|
| 109 | struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc); | 
|---|
| 110 |  | 
|---|
| 111 | virtio_gpu_cmd_set_scanout(vgdev, scanout_id: output->index, resource_id: 0, width: 0, height: 0, x: 0, y: 0); | 
|---|
| 112 | virtio_gpu_notify(vgdev); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc, | 
|---|
| 116 | struct drm_atomic_state *state) | 
|---|
| 117 | { | 
|---|
| 118 | return 0; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc, | 
|---|
| 122 | struct drm_atomic_state *state) | 
|---|
| 123 | { | 
|---|
| 124 | struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, | 
|---|
| 125 | crtc); | 
|---|
| 126 | struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc); | 
|---|
| 127 |  | 
|---|
| 128 | /* | 
|---|
| 129 | * virtio-gpu can't do modeset and plane update operations | 
|---|
| 130 | * independent from each other.  So the actual modeset happens | 
|---|
| 131 | * in the plane update callback, and here we just check | 
|---|
| 132 | * whenever we must force the modeset. | 
|---|
| 133 | */ | 
|---|
| 134 | if (drm_atomic_crtc_needs_modeset(state: crtc_state)) | 
|---|
| 135 | output->needs_modeset = true; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = { | 
|---|
| 139 | .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb, | 
|---|
| 140 | .atomic_check  = virtio_gpu_crtc_atomic_check, | 
|---|
| 141 | .atomic_flush  = virtio_gpu_crtc_atomic_flush, | 
|---|
| 142 | .atomic_enable = virtio_gpu_crtc_atomic_enable, | 
|---|
| 143 | .atomic_disable = virtio_gpu_crtc_atomic_disable, | 
|---|
| 144 | }; | 
|---|
| 145 |  | 
|---|
| 146 | static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder, | 
|---|
| 147 | struct drm_display_mode *mode, | 
|---|
| 148 | struct drm_display_mode *adjusted_mode) | 
|---|
| 149 | { | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | static void virtio_gpu_enc_enable(struct drm_encoder *encoder) | 
|---|
| 153 | { | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | static void virtio_gpu_enc_disable(struct drm_encoder *encoder) | 
|---|
| 157 | { | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | static int virtio_gpu_conn_get_modes(struct drm_connector *connector) | 
|---|
| 161 | { | 
|---|
| 162 | struct virtio_gpu_output *output = | 
|---|
| 163 | drm_connector_to_virtio_gpu_output(connector); | 
|---|
| 164 | struct drm_display_mode *mode = NULL; | 
|---|
| 165 | int count, width, height; | 
|---|
| 166 |  | 
|---|
| 167 | count = drm_edid_connector_add_modes(connector); | 
|---|
| 168 | if (count) | 
|---|
| 169 | return count; | 
|---|
| 170 |  | 
|---|
| 171 | width  = le32_to_cpu(output->info.r.width); | 
|---|
| 172 | height = le32_to_cpu(output->info.r.height); | 
|---|
| 173 | count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX); | 
|---|
| 174 |  | 
|---|
| 175 | if (width == 0 || height == 0) { | 
|---|
| 176 | drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF); | 
|---|
| 177 | } else { | 
|---|
| 178 | DRM_DEBUG( "add mode: %dx%d\n", width, height); | 
|---|
| 179 | mode = drm_cvt_mode(dev: connector->dev, hdisplay: width, vdisplay: height, vrefresh: 60, | 
|---|
| 180 | reduced: false, interlaced: false, margins: false); | 
|---|
| 181 | if (!mode) | 
|---|
| 182 | return count; | 
|---|
| 183 | mode->type |= DRM_MODE_TYPE_PREFERRED; | 
|---|
| 184 | drm_mode_probed_add(connector, mode); | 
|---|
| 185 | count++; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | return count; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector, | 
|---|
| 192 | const struct drm_display_mode *mode) | 
|---|
| 193 | { | 
|---|
| 194 | struct virtio_gpu_output *output = | 
|---|
| 195 | drm_connector_to_virtio_gpu_output(connector); | 
|---|
| 196 | int width, height; | 
|---|
| 197 |  | 
|---|
| 198 | width  = le32_to_cpu(output->info.r.width); | 
|---|
| 199 | height = le32_to_cpu(output->info.r.height); | 
|---|
| 200 |  | 
|---|
| 201 | if (!(mode->type & DRM_MODE_TYPE_PREFERRED)) | 
|---|
| 202 | return MODE_OK; | 
|---|
| 203 | if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF) | 
|---|
| 204 | return MODE_OK; | 
|---|
| 205 | if (mode->hdisplay <= width  && mode->hdisplay >= width - 16 && | 
|---|
| 206 | mode->vdisplay <= height && mode->vdisplay >= height - 16) | 
|---|
| 207 | return MODE_OK; | 
|---|
| 208 |  | 
|---|
| 209 | DRM_DEBUG( "del mode: %dx%d\n", mode->hdisplay, mode->vdisplay); | 
|---|
| 210 | return MODE_BAD; | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = { | 
|---|
| 214 | .mode_set   = virtio_gpu_enc_mode_set, | 
|---|
| 215 | .enable     = virtio_gpu_enc_enable, | 
|---|
| 216 | .disable    = virtio_gpu_enc_disable, | 
|---|
| 217 | }; | 
|---|
| 218 |  | 
|---|
| 219 | static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = { | 
|---|
| 220 | .get_modes    = virtio_gpu_conn_get_modes, | 
|---|
| 221 | .mode_valid   = virtio_gpu_conn_mode_valid, | 
|---|
| 222 | }; | 
|---|
| 223 |  | 
|---|
| 224 | static enum drm_connector_status virtio_gpu_conn_detect( | 
|---|
| 225 | struct drm_connector *connector, | 
|---|
| 226 | bool force) | 
|---|
| 227 | { | 
|---|
| 228 | struct virtio_gpu_output *output = | 
|---|
| 229 | drm_connector_to_virtio_gpu_output(connector); | 
|---|
| 230 |  | 
|---|
| 231 | if (output->info.enabled) | 
|---|
| 232 | return connector_status_connected; | 
|---|
| 233 | else | 
|---|
| 234 | return connector_status_disconnected; | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | static void virtio_gpu_conn_destroy(struct drm_connector *connector) | 
|---|
| 238 | { | 
|---|
| 239 | drm_connector_unregister(connector); | 
|---|
| 240 | drm_connector_cleanup(connector); | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | static const struct drm_connector_funcs virtio_gpu_connector_funcs = { | 
|---|
| 244 | .detect = virtio_gpu_conn_detect, | 
|---|
| 245 | .fill_modes = drm_helper_probe_single_connector_modes, | 
|---|
| 246 | .destroy = virtio_gpu_conn_destroy, | 
|---|
| 247 | .reset = drm_atomic_helper_connector_reset, | 
|---|
| 248 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, | 
|---|
| 249 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, | 
|---|
| 250 | }; | 
|---|
| 251 |  | 
|---|
| 252 | static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index) | 
|---|
| 253 | { | 
|---|
| 254 | struct drm_device *dev = vgdev->ddev; | 
|---|
| 255 | struct virtio_gpu_output *output = vgdev->outputs + index; | 
|---|
| 256 | struct drm_connector *connector = &output->conn; | 
|---|
| 257 | struct drm_encoder *encoder = &output->enc; | 
|---|
| 258 | struct drm_crtc *crtc = &output->crtc; | 
|---|
| 259 | struct drm_plane *primary, *cursor; | 
|---|
| 260 |  | 
|---|
| 261 | output->index = index; | 
|---|
| 262 | if (index == 0) { | 
|---|
| 263 | output->info.enabled = cpu_to_le32(true); | 
|---|
| 264 | output->info.r.width = cpu_to_le32(XRES_DEF); | 
|---|
| 265 | output->info.r.height = cpu_to_le32(YRES_DEF); | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | primary = virtio_gpu_plane_init(vgdev, type: DRM_PLANE_TYPE_PRIMARY, index); | 
|---|
| 269 | if (IS_ERR(ptr: primary)) | 
|---|
| 270 | return PTR_ERR(ptr: primary); | 
|---|
| 271 | cursor = virtio_gpu_plane_init(vgdev, type: DRM_PLANE_TYPE_CURSOR, index); | 
|---|
| 272 | if (IS_ERR(ptr: cursor)) | 
|---|
| 273 | return PTR_ERR(ptr: cursor); | 
|---|
| 274 | drm_crtc_init_with_planes(dev, crtc, primary, cursor, | 
|---|
| 275 | funcs: &virtio_gpu_crtc_funcs, NULL); | 
|---|
| 276 | drm_crtc_helper_add(crtc, funcs: &virtio_gpu_crtc_helper_funcs); | 
|---|
| 277 |  | 
|---|
| 278 | drm_connector_init(dev, connector, funcs: &virtio_gpu_connector_funcs, | 
|---|
| 279 | DRM_MODE_CONNECTOR_VIRTUAL); | 
|---|
| 280 | drm_connector_helper_add(connector, funcs: &virtio_gpu_conn_helper_funcs); | 
|---|
| 281 | if (vgdev->has_edid) | 
|---|
| 282 | drm_connector_attach_edid_property(connector); | 
|---|
| 283 |  | 
|---|
| 284 | drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL); | 
|---|
| 285 | drm_encoder_helper_add(encoder, funcs: &virtio_gpu_enc_helper_funcs); | 
|---|
| 286 | encoder->possible_crtcs = 1 << index; | 
|---|
| 287 |  | 
|---|
| 288 | drm_connector_attach_encoder(connector, encoder); | 
|---|
| 289 | drm_connector_register(connector); | 
|---|
| 290 | return 0; | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | static struct drm_framebuffer * | 
|---|
| 294 | virtio_gpu_user_framebuffer_create(struct drm_device *dev, | 
|---|
| 295 | struct drm_file *file_priv, | 
|---|
| 296 | const struct drm_format_info *info, | 
|---|
| 297 | const struct drm_mode_fb_cmd2 *mode_cmd) | 
|---|
| 298 | { | 
|---|
| 299 | struct drm_gem_object *obj = NULL; | 
|---|
| 300 | struct virtio_gpu_framebuffer *virtio_gpu_fb; | 
|---|
| 301 | int ret; | 
|---|
| 302 |  | 
|---|
| 303 | if (mode_cmd->pixel_format != DRM_FORMAT_HOST_XRGB8888 && | 
|---|
| 304 | mode_cmd->pixel_format != DRM_FORMAT_HOST_ARGB8888) | 
|---|
| 305 | return ERR_PTR(error: -ENOENT); | 
|---|
| 306 |  | 
|---|
| 307 | /* lookup object associated with res handle */ | 
|---|
| 308 | obj = drm_gem_object_lookup(filp: file_priv, handle: mode_cmd->handles[0]); | 
|---|
| 309 | if (!obj) | 
|---|
| 310 | return ERR_PTR(error: -EINVAL); | 
|---|
| 311 |  | 
|---|
| 312 | virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL); | 
|---|
| 313 | if (virtio_gpu_fb == NULL) { | 
|---|
| 314 | drm_gem_object_put(obj); | 
|---|
| 315 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 316 | } | 
|---|
| 317 |  | 
|---|
| 318 | ret = virtio_gpu_framebuffer_init(dev, vgfb: virtio_gpu_fb, info, mode_cmd, obj); | 
|---|
| 319 | if (ret) { | 
|---|
| 320 | kfree(objp: virtio_gpu_fb); | 
|---|
| 321 | drm_gem_object_put(obj); | 
|---|
| 322 | return NULL; | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | return &virtio_gpu_fb->base; | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = { | 
|---|
| 329 | .fb_create = virtio_gpu_user_framebuffer_create, | 
|---|
| 330 | .atomic_check = drm_atomic_helper_check, | 
|---|
| 331 | .atomic_commit = drm_atomic_helper_commit, | 
|---|
| 332 | }; | 
|---|
| 333 |  | 
|---|
| 334 | int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev) | 
|---|
| 335 | { | 
|---|
| 336 | int i, ret; | 
|---|
| 337 |  | 
|---|
| 338 | if (!vgdev->num_scanouts) | 
|---|
| 339 | return 0; | 
|---|
| 340 |  | 
|---|
| 341 | ret = drmm_mode_config_init(dev: vgdev->ddev); | 
|---|
| 342 | if (ret) | 
|---|
| 343 | return ret; | 
|---|
| 344 |  | 
|---|
| 345 | vgdev->ddev->mode_config.quirk_addfb_prefer_host_byte_order = true; | 
|---|
| 346 | vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs; | 
|---|
| 347 |  | 
|---|
| 348 | /* modes will be validated against the framebuffer size */ | 
|---|
| 349 | vgdev->ddev->mode_config.min_width = XRES_MIN; | 
|---|
| 350 | vgdev->ddev->mode_config.min_height = YRES_MIN; | 
|---|
| 351 | vgdev->ddev->mode_config.max_width = XRES_MAX; | 
|---|
| 352 | vgdev->ddev->mode_config.max_height = YRES_MAX; | 
|---|
| 353 |  | 
|---|
| 354 | vgdev->ddev->mode_config.fb_modifiers_not_supported = true; | 
|---|
| 355 |  | 
|---|
| 356 | for (i = 0 ; i < vgdev->num_scanouts; ++i) | 
|---|
| 357 | vgdev_output_init(vgdev, index: i); | 
|---|
| 358 |  | 
|---|
| 359 | drm_mode_config_reset(dev: vgdev->ddev); | 
|---|
| 360 | return 0; | 
|---|
| 361 | } | 
|---|
| 362 |  | 
|---|
| 363 | void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev) | 
|---|
| 364 | { | 
|---|
| 365 | int i; | 
|---|
| 366 |  | 
|---|
| 367 | if (!vgdev->num_scanouts) | 
|---|
| 368 | return; | 
|---|
| 369 |  | 
|---|
| 370 | for (i = 0 ; i < vgdev->num_scanouts; ++i) | 
|---|
| 371 | drm_edid_free(drm_edid: vgdev->outputs[i].drm_edid); | 
|---|
| 372 | } | 
|---|
| 373 |  | 
|---|