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