| .. | .. |
|---|
| 25 | 25 | * OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 26 | 26 | */ |
|---|
| 27 | 27 | |
|---|
| 28 | | -#include "virtgpu_drv.h" |
|---|
| 29 | | -#include <drm/drm_crtc_helper.h> |
|---|
| 30 | 28 | #include <drm/drm_atomic_helper.h> |
|---|
| 29 | +#include <drm/drm_damage_helper.h> |
|---|
| 30 | +#include <drm/drm_fourcc.h> |
|---|
| 31 | 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" |
|---|
| 32 | 36 | |
|---|
| 33 | 37 | #define XRES_MIN 32 |
|---|
| 34 | 38 | #define YRES_MIN 32 |
|---|
| .. | .. |
|---|
| 38 | 42 | |
|---|
| 39 | 43 | #define XRES_MAX 8192 |
|---|
| 40 | 44 | #define YRES_MAX 8192 |
|---|
| 45 | + |
|---|
| 46 | +#define drm_connector_to_virtio_gpu_output(x) \ |
|---|
| 47 | + container_of(x, struct virtio_gpu_output, conn) |
|---|
| 41 | 48 | |
|---|
| 42 | 49 | static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = { |
|---|
| 43 | 50 | .set_config = drm_atomic_helper_set_config, |
|---|
| .. | .. |
|---|
| 49 | 56 | .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, |
|---|
| 50 | 57 | }; |
|---|
| 51 | 58 | |
|---|
| 52 | | -static int |
|---|
| 53 | | -virtio_gpu_framebuffer_surface_dirty(struct drm_framebuffer *fb, |
|---|
| 54 | | - struct drm_file *file_priv, |
|---|
| 55 | | - unsigned int flags, unsigned int color, |
|---|
| 56 | | - struct drm_clip_rect *clips, |
|---|
| 57 | | - unsigned int num_clips) |
|---|
| 58 | | -{ |
|---|
| 59 | | - struct virtio_gpu_framebuffer *virtio_gpu_fb |
|---|
| 60 | | - = to_virtio_gpu_framebuffer(fb); |
|---|
| 61 | | - |
|---|
| 62 | | - return virtio_gpu_surface_dirty(virtio_gpu_fb, clips, num_clips); |
|---|
| 63 | | -} |
|---|
| 64 | | - |
|---|
| 65 | 59 | static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = { |
|---|
| 66 | 60 | .create_handle = drm_gem_fb_create_handle, |
|---|
| 67 | 61 | .destroy = drm_gem_fb_destroy, |
|---|
| 68 | | - .dirty = virtio_gpu_framebuffer_surface_dirty, |
|---|
| 62 | + .dirty = drm_atomic_helper_dirtyfb, |
|---|
| 69 | 63 | }; |
|---|
| 70 | 64 | |
|---|
| 71 | | -int |
|---|
| 65 | +static int |
|---|
| 72 | 66 | virtio_gpu_framebuffer_init(struct drm_device *dev, |
|---|
| 73 | 67 | struct virtio_gpu_framebuffer *vgfb, |
|---|
| 74 | 68 | const struct drm_mode_fb_cmd2 *mode_cmd, |
|---|
| .. | .. |
|---|
| 85 | 79 | vgfb->base.obj[0] = NULL; |
|---|
| 86 | 80 | return ret; |
|---|
| 87 | 81 | } |
|---|
| 88 | | - |
|---|
| 89 | | - spin_lock_init(&vgfb->dirty_lock); |
|---|
| 90 | | - vgfb->x1 = vgfb->y1 = INT_MAX; |
|---|
| 91 | | - vgfb->x2 = vgfb->y2 = 0; |
|---|
| 92 | 82 | return 0; |
|---|
| 93 | 83 | } |
|---|
| 94 | 84 | |
|---|
| .. | .. |
|---|
| 101 | 91 | virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, |
|---|
| 102 | 92 | crtc->mode.hdisplay, |
|---|
| 103 | 93 | crtc->mode.vdisplay, 0, 0); |
|---|
| 94 | + virtio_gpu_notify(vgdev); |
|---|
| 104 | 95 | } |
|---|
| 105 | 96 | |
|---|
| 106 | 97 | static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc, |
|---|
| 107 | 98 | struct drm_crtc_state *old_state) |
|---|
| 108 | 99 | { |
|---|
| 109 | | - struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc); |
|---|
| 110 | | - |
|---|
| 111 | | - output->enabled = true; |
|---|
| 112 | 100 | } |
|---|
| 113 | 101 | |
|---|
| 114 | 102 | static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc, |
|---|
| .. | .. |
|---|
| 119 | 107 | struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc); |
|---|
| 120 | 108 | |
|---|
| 121 | 109 | virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0); |
|---|
| 122 | | - output->enabled = false; |
|---|
| 110 | + virtio_gpu_notify(vgdev); |
|---|
| 123 | 111 | } |
|---|
| 124 | 112 | |
|---|
| 125 | 113 | static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc, |
|---|
| .. | .. |
|---|
| 131 | 119 | static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc, |
|---|
| 132 | 120 | struct drm_crtc_state *old_state) |
|---|
| 133 | 121 | { |
|---|
| 134 | | - unsigned long flags; |
|---|
| 122 | + struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc); |
|---|
| 135 | 123 | |
|---|
| 136 | | - spin_lock_irqsave(&crtc->dev->event_lock, flags); |
|---|
| 137 | | - if (crtc->state->event) |
|---|
| 138 | | - drm_crtc_send_vblank_event(crtc, crtc->state->event); |
|---|
| 139 | | - crtc->state->event = NULL; |
|---|
| 140 | | - spin_unlock_irqrestore(&crtc->dev->event_lock, flags); |
|---|
| 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 | + } |
|---|
| 141 | 133 | } |
|---|
| 142 | 134 | |
|---|
| 143 | 135 | static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = { |
|---|
| .. | .. |
|---|
| 180 | 172 | count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX); |
|---|
| 181 | 173 | |
|---|
| 182 | 174 | if (width == 0 || height == 0) { |
|---|
| 183 | | - width = XRES_DEF; |
|---|
| 184 | | - height = YRES_DEF; |
|---|
| 185 | 175 | drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF); |
|---|
| 186 | 176 | } else { |
|---|
| 187 | 177 | DRM_DEBUG("add mode: %dx%d\n", width, height); |
|---|
| 188 | 178 | mode = drm_cvt_mode(connector->dev, width, height, 60, |
|---|
| 189 | 179 | false, false, false); |
|---|
| 180 | + if (!mode) |
|---|
| 181 | + return count; |
|---|
| 190 | 182 | mode->type |= DRM_MODE_TYPE_PREFERRED; |
|---|
| 191 | 183 | drm_mode_probed_add(connector, mode); |
|---|
| 192 | 184 | count++; |
|---|
| .. | .. |
|---|
| 256 | 248 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
|---|
| 257 | 249 | }; |
|---|
| 258 | 250 | |
|---|
| 259 | | -static const struct drm_encoder_funcs virtio_gpu_enc_funcs = { |
|---|
| 260 | | - .destroy = drm_encoder_cleanup, |
|---|
| 261 | | -}; |
|---|
| 262 | | - |
|---|
| 263 | 251 | static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index) |
|---|
| 264 | 252 | { |
|---|
| 265 | 253 | struct drm_device *dev = vgdev->ddev; |
|---|
| .. | .. |
|---|
| 292 | 280 | if (vgdev->has_edid) |
|---|
| 293 | 281 | drm_connector_attach_edid_property(connector); |
|---|
| 294 | 282 | |
|---|
| 295 | | - drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs, |
|---|
| 296 | | - DRM_MODE_ENCODER_VIRTUAL, NULL); |
|---|
| 283 | + drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_VIRTUAL); |
|---|
| 297 | 284 | drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs); |
|---|
| 298 | 285 | encoder->possible_crtcs = 1 << index; |
|---|
| 299 | 286 | |
|---|
| .. | .. |
|---|
| 323 | 310 | ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj); |
|---|
| 324 | 311 | if (ret) { |
|---|
| 325 | 312 | kfree(virtio_gpu_fb); |
|---|
| 326 | | - drm_gem_object_put_unlocked(obj); |
|---|
| 313 | + drm_gem_object_put(obj); |
|---|
| 327 | 314 | return NULL; |
|---|
| 328 | 315 | } |
|---|
| 329 | 316 | |
|---|
| 330 | 317 | return &virtio_gpu_fb->base; |
|---|
| 331 | 318 | } |
|---|
| 332 | | - |
|---|
| 333 | | -static void vgdev_atomic_commit_tail(struct drm_atomic_state *state) |
|---|
| 334 | | -{ |
|---|
| 335 | | - struct drm_device *dev = state->dev; |
|---|
| 336 | | - |
|---|
| 337 | | - drm_atomic_helper_commit_modeset_disables(dev, state); |
|---|
| 338 | | - drm_atomic_helper_commit_modeset_enables(dev, state); |
|---|
| 339 | | - drm_atomic_helper_commit_planes(dev, state, 0); |
|---|
| 340 | | - |
|---|
| 341 | | - drm_atomic_helper_commit_hw_done(state); |
|---|
| 342 | | - |
|---|
| 343 | | - drm_atomic_helper_wait_for_vblanks(dev, state); |
|---|
| 344 | | - drm_atomic_helper_cleanup_planes(dev, state); |
|---|
| 345 | | -} |
|---|
| 346 | | - |
|---|
| 347 | | -static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = { |
|---|
| 348 | | - .atomic_commit_tail = vgdev_atomic_commit_tail, |
|---|
| 349 | | -}; |
|---|
| 350 | 319 | |
|---|
| 351 | 320 | static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = { |
|---|
| 352 | 321 | .fb_create = virtio_gpu_user_framebuffer_create, |
|---|
| .. | .. |
|---|
| 354 | 323 | .atomic_commit = drm_atomic_helper_commit, |
|---|
| 355 | 324 | }; |
|---|
| 356 | 325 | |
|---|
| 357 | | -void virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev) |
|---|
| 326 | +int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev) |
|---|
| 358 | 327 | { |
|---|
| 359 | | - int i; |
|---|
| 328 | + int i, ret; |
|---|
| 360 | 329 | |
|---|
| 361 | | - drm_mode_config_init(vgdev->ddev); |
|---|
| 330 | + ret = drmm_mode_config_init(vgdev->ddev); |
|---|
| 331 | + if (ret) |
|---|
| 332 | + return ret; |
|---|
| 333 | + |
|---|
| 362 | 334 | vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs; |
|---|
| 363 | | - vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers; |
|---|
| 364 | 335 | |
|---|
| 365 | 336 | /* modes will be validated against the framebuffer size */ |
|---|
| 366 | 337 | vgdev->ddev->mode_config.min_width = XRES_MIN; |
|---|
| .. | .. |
|---|
| 372 | 343 | vgdev_output_init(vgdev, i); |
|---|
| 373 | 344 | |
|---|
| 374 | 345 | drm_mode_config_reset(vgdev->ddev); |
|---|
| 346 | + return 0; |
|---|
| 375 | 347 | } |
|---|
| 376 | 348 | |
|---|
| 377 | 349 | void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev) |
|---|
| .. | .. |
|---|
| 380 | 352 | |
|---|
| 381 | 353 | for (i = 0 ; i < vgdev->num_scanouts; ++i) |
|---|
| 382 | 354 | kfree(vgdev->outputs[i].edid); |
|---|
| 383 | | - drm_atomic_helper_shutdown(vgdev->ddev); |
|---|
| 384 | | - drm_mode_config_cleanup(vgdev->ddev); |
|---|
| 385 | 355 | } |
|---|