1 /**************************************************************************
3 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vmwgfx_kms.h"
29 #include <drm/drm_plane_helper.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_atomic_helper.h>
34 #define vmw_crtc_to_ldu(x) \
35 container_of(x, struct vmw_legacy_display_unit, base.crtc)
36 #define vmw_encoder_to_ldu(x) \
37 container_of(x, struct vmw_legacy_display_unit, base.encoder)
38 #define vmw_connector_to_ldu(x) \
39 container_of(x, struct vmw_legacy_display_unit, base.connector)
41 struct vmw_legacy_display {
42 struct list_head active;
45 unsigned last_num_active;
47 struct vmw_framebuffer *fb;
51 * Display unit using the legacy register interface.
53 struct vmw_legacy_display_unit {
54 struct vmw_display_unit base;
56 struct list_head active;
59 static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
61 list_del_init(&ldu->active);
62 vmw_du_cleanup(&ldu->base);
68 * Legacy Display Unit CRTC functions
71 static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
73 vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
76 static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
78 struct vmw_legacy_display *lds = dev_priv->ldu_priv;
79 struct vmw_legacy_display_unit *entry;
80 struct drm_framebuffer *fb = NULL;
81 struct drm_crtc *crtc = NULL;
84 /* If there is no display topology the host just assumes
85 * that the guest will set the same layout as the host.
87 if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
89 list_for_each_entry(entry, &lds->active, active) {
90 crtc = &entry->base.crtc;
91 w = max(w, crtc->x + crtc->mode.hdisplay);
92 h = max(h, crtc->y + crtc->mode.vdisplay);
98 fb = entry->base.crtc.primary->state->fb;
100 return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0],
101 fb->format->cpp[0] * 8,
105 if (!list_empty(&lds->active)) {
106 entry = list_entry(lds->active.next, typeof(*entry), active);
107 fb = entry->base.crtc.primary->state->fb;
109 vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0],
110 fb->format->cpp[0] * 8, fb->format->depth);
113 /* Make sure we always show something. */
114 vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
115 lds->num_active ? lds->num_active : 1);
118 list_for_each_entry(entry, &lds->active, active) {
119 crtc = &entry->base.crtc;
121 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
122 vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
123 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
124 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
125 vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
126 vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
127 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
132 BUG_ON(i != lds->num_active);
134 lds->last_num_active = lds->num_active;
139 static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
140 struct vmw_legacy_display_unit *ldu)
142 struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
143 if (list_empty(&ldu->active))
146 /* Must init otherwise list_empty(&ldu->active) will not work. */
147 list_del_init(&ldu->active);
148 if (--(ld->num_active) == 0) {
151 ld->fb->unpin(ld->fb);
158 static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
159 struct vmw_legacy_display_unit *ldu,
160 struct vmw_framebuffer *vfb)
162 struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
163 struct vmw_legacy_display_unit *entry;
164 struct list_head *at;
166 BUG_ON(!ld->num_active && ld->fb);
168 if (ld->fb && ld->fb->unpin)
169 ld->fb->unpin(ld->fb);
170 vmw_svga_enable(vmw_priv);
176 if (!list_empty(&ldu->active))
180 list_for_each_entry(entry, &ld->active, active) {
181 if (entry->base.unit > ldu->base.unit)
187 list_add(&ldu->active, at);
195 * vmw_ldu_crtc_mode_set_nofb - Enable svga
197 * @crtc: CRTC associated with the new screen
199 * For LDU, just enable the svga
201 static void vmw_ldu_crtc_mode_set_nofb(struct drm_crtc *crtc)
206 * vmw_ldu_crtc_helper_prepare - Noop
208 * @crtc: CRTC associated with the new screen
210 * Prepares the CRTC for a mode set, but we don't need to do anything here.
213 static void vmw_ldu_crtc_helper_prepare(struct drm_crtc *crtc)
218 * vmw_ldu_crtc_helper_commit - Noop
220 * @crtc: CRTC associated with the new screen
222 * This is called after a mode set has been completed. Here's
223 * usually a good place to call vmw_ldu_add_active/vmw_ldu_del_active
224 * but since for LDU the display plane is closely tied to the
225 * CRTC, it makes more sense to do those at plane update time.
227 static void vmw_ldu_crtc_helper_commit(struct drm_crtc *crtc)
232 * vmw_ldu_crtc_helper_disable - Turns off CRTC
234 * @crtc: CRTC to be turned off
236 static void vmw_ldu_crtc_helper_disable(struct drm_crtc *crtc)
240 static const struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
241 .gamma_set = vmw_du_crtc_gamma_set,
242 .destroy = vmw_ldu_crtc_destroy,
243 .reset = vmw_du_crtc_reset,
244 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
245 .atomic_destroy_state = vmw_du_crtc_destroy_state,
246 .set_config = vmw_kms_set_config,
251 * Legacy Display Unit encoder functions
254 static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
256 vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
259 static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
260 .destroy = vmw_ldu_encoder_destroy,
264 * Legacy Display Unit connector functions
267 static void vmw_ldu_connector_destroy(struct drm_connector *connector)
269 vmw_ldu_destroy(vmw_connector_to_ldu(connector));
272 static const struct drm_connector_funcs vmw_legacy_connector_funcs = {
273 .dpms = vmw_du_connector_dpms,
274 .detect = vmw_du_connector_detect,
275 .fill_modes = vmw_du_connector_fill_modes,
276 .set_property = vmw_du_connector_set_property,
277 .destroy = vmw_ldu_connector_destroy,
278 .reset = vmw_du_connector_reset,
279 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
280 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
281 .atomic_set_property = vmw_du_connector_atomic_set_property,
282 .atomic_get_property = vmw_du_connector_atomic_get_property,
286 drm_connector_helper_funcs vmw_ldu_connector_helper_funcs = {
287 .best_encoder = drm_atomic_helper_best_encoder,
291 * Legacy Display Plane Functions
295 * vmw_ldu_primary_plane_cleanup_fb - Noop
297 * @plane: display plane
298 * @old_state: Contains the FB to clean up
300 * Unpins the display surface
302 * Returns 0 on success
305 vmw_ldu_primary_plane_cleanup_fb(struct drm_plane *plane,
306 struct drm_plane_state *old_state)
312 * vmw_ldu_primary_plane_prepare_fb - Noop
314 * @plane: display plane
315 * @new_state: info on the new plane state, including the FB
317 * Returns 0 on success
320 vmw_ldu_primary_plane_prepare_fb(struct drm_plane *plane,
321 struct drm_plane_state *new_state)
328 vmw_ldu_primary_plane_atomic_update(struct drm_plane *plane,
329 struct drm_plane_state *old_state)
331 struct vmw_private *dev_priv;
332 struct vmw_legacy_display_unit *ldu;
333 struct vmw_framebuffer *vfb;
334 struct drm_framebuffer *fb;
335 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
338 ldu = vmw_crtc_to_ldu(crtc);
339 dev_priv = vmw_priv(plane->dev);
340 fb = plane->state->fb;
342 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
345 vmw_ldu_add_active(dev_priv, ldu, vfb);
347 vmw_ldu_del_active(dev_priv, ldu);
349 vmw_ldu_commit_list(dev_priv);
353 static const struct drm_plane_funcs vmw_ldu_plane_funcs = {
354 .update_plane = drm_atomic_helper_update_plane,
355 .disable_plane = drm_atomic_helper_disable_plane,
356 .destroy = vmw_du_primary_plane_destroy,
357 .reset = vmw_du_plane_reset,
358 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
359 .atomic_destroy_state = vmw_du_plane_destroy_state,
362 static const struct drm_plane_funcs vmw_ldu_cursor_funcs = {
363 .update_plane = drm_atomic_helper_update_plane,
364 .disable_plane = drm_atomic_helper_disable_plane,
365 .destroy = vmw_du_cursor_plane_destroy,
366 .reset = vmw_du_plane_reset,
367 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
368 .atomic_destroy_state = vmw_du_plane_destroy_state,
375 drm_plane_helper_funcs vmw_ldu_cursor_plane_helper_funcs = {
376 .atomic_check = vmw_du_cursor_plane_atomic_check,
377 .atomic_update = vmw_du_cursor_plane_atomic_update,
378 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
379 .cleanup_fb = vmw_du_plane_cleanup_fb,
383 drm_plane_helper_funcs vmw_ldu_primary_plane_helper_funcs = {
384 .atomic_check = vmw_du_primary_plane_atomic_check,
385 .atomic_update = vmw_ldu_primary_plane_atomic_update,
386 .prepare_fb = vmw_ldu_primary_plane_prepare_fb,
387 .cleanup_fb = vmw_ldu_primary_plane_cleanup_fb,
390 static const struct drm_crtc_helper_funcs vmw_ldu_crtc_helper_funcs = {
391 .prepare = vmw_ldu_crtc_helper_prepare,
392 .commit = vmw_ldu_crtc_helper_commit,
393 .disable = vmw_ldu_crtc_helper_disable,
394 .mode_set_nofb = vmw_ldu_crtc_mode_set_nofb,
395 .atomic_check = vmw_du_crtc_atomic_check,
396 .atomic_begin = vmw_du_crtc_atomic_begin,
397 .atomic_flush = vmw_du_crtc_atomic_flush,
401 static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
403 struct vmw_legacy_display_unit *ldu;
404 struct drm_device *dev = dev_priv->dev;
405 struct drm_connector *connector;
406 struct drm_encoder *encoder;
407 struct drm_plane *primary, *cursor;
408 struct drm_crtc *crtc;
411 ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
415 ldu->base.unit = unit;
416 crtc = &ldu->base.crtc;
417 encoder = &ldu->base.encoder;
418 connector = &ldu->base.connector;
419 primary = &ldu->base.primary;
420 cursor = &ldu->base.cursor;
422 INIT_LIST_HEAD(&ldu->active);
424 ldu->base.pref_active = (unit == 0);
425 ldu->base.pref_width = dev_priv->initial_width;
426 ldu->base.pref_height = dev_priv->initial_height;
427 ldu->base.pref_mode = NULL;
430 * Remove this after enabling atomic because property values can
431 * only exist in a state object
433 ldu->base.is_implicit = true;
435 /* Initialize primary plane */
436 vmw_du_plane_reset(primary);
438 ret = drm_universal_plane_init(dev, &ldu->base.primary,
439 0, &vmw_ldu_plane_funcs,
440 vmw_primary_plane_formats,
441 ARRAY_SIZE(vmw_primary_plane_formats),
442 DRM_PLANE_TYPE_PRIMARY, NULL);
444 DRM_ERROR("Failed to initialize primary plane");
448 drm_plane_helper_add(primary, &vmw_ldu_primary_plane_helper_funcs);
450 /* Initialize cursor plane */
451 vmw_du_plane_reset(cursor);
453 ret = drm_universal_plane_init(dev, &ldu->base.cursor,
454 0, &vmw_ldu_cursor_funcs,
455 vmw_cursor_plane_formats,
456 ARRAY_SIZE(vmw_cursor_plane_formats),
457 DRM_PLANE_TYPE_CURSOR, NULL);
459 DRM_ERROR("Failed to initialize cursor plane");
460 drm_plane_cleanup(&ldu->base.primary);
464 drm_plane_helper_add(cursor, &vmw_ldu_cursor_plane_helper_funcs);
467 vmw_du_connector_reset(connector);
468 ret = drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
469 DRM_MODE_CONNECTOR_VIRTUAL);
471 DRM_ERROR("Failed to initialize connector\n");
475 drm_connector_helper_add(connector, &vmw_ldu_connector_helper_funcs);
476 connector->status = vmw_du_connector_detect(connector, true);
477 vmw_connector_state_to_vcs(connector->state)->is_implicit = true;
480 ret = drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
481 DRM_MODE_ENCODER_VIRTUAL, NULL);
483 DRM_ERROR("Failed to initialize encoder\n");
484 goto err_free_connector;
487 (void) drm_mode_connector_attach_encoder(connector, encoder);
488 encoder->possible_crtcs = (1 << unit);
489 encoder->possible_clones = 0;
491 ret = drm_connector_register(connector);
493 DRM_ERROR("Failed to register connector\n");
494 goto err_free_encoder;
498 vmw_du_crtc_reset(crtc);
499 ret = drm_crtc_init_with_planes(dev, crtc, &ldu->base.primary,
501 &vmw_legacy_crtc_funcs, NULL);
503 DRM_ERROR("Failed to initialize CRTC\n");
504 goto err_free_unregister;
507 drm_crtc_helper_add(crtc, &vmw_ldu_crtc_helper_funcs);
509 drm_mode_crtc_set_gamma_size(crtc, 256);
511 drm_object_attach_property(&connector->base,
512 dev_priv->hotplug_mode_update_property, 1);
513 drm_object_attach_property(&connector->base,
514 dev->mode_config.suggested_x_property, 0);
515 drm_object_attach_property(&connector->base,
516 dev->mode_config.suggested_y_property, 0);
517 if (dev_priv->implicit_placement_property)
518 drm_object_attach_property
520 dev_priv->implicit_placement_property,
526 drm_connector_unregister(connector);
528 drm_encoder_cleanup(encoder);
530 drm_connector_cleanup(connector);
536 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv)
538 struct drm_device *dev = dev_priv->dev;
541 if (dev_priv->ldu_priv) {
542 DRM_INFO("ldu system already on\n");
546 dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
547 if (!dev_priv->ldu_priv)
550 INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
551 dev_priv->ldu_priv->num_active = 0;
552 dev_priv->ldu_priv->last_num_active = 0;
553 dev_priv->ldu_priv->fb = NULL;
555 /* for old hardware without multimon only enable one display */
556 if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
557 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
559 ret = drm_vblank_init(dev, 1);
563 vmw_kms_create_implicit_placement_property(dev_priv, true);
565 if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
566 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
567 vmw_ldu_init(dev_priv, i);
569 vmw_ldu_init(dev_priv, 0);
571 dev_priv->active_display_unit = vmw_du_legacy;
573 DRM_INFO("Legacy Display Unit initialized\n");
578 kfree(dev_priv->ldu_priv);
579 dev_priv->ldu_priv = NULL;
583 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv)
585 struct drm_device *dev = dev_priv->dev;
587 if (!dev_priv->ldu_priv)
590 drm_vblank_cleanup(dev);
592 BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
594 kfree(dev_priv->ldu_priv);
600 int vmw_kms_ldu_do_dmabuf_dirty(struct vmw_private *dev_priv,
601 struct vmw_framebuffer *framebuffer,
602 unsigned flags, unsigned color,
603 struct drm_clip_rect *clips,
604 unsigned num_clips, int increment)
611 SVGAFifoCmdUpdate body;
614 fifo_size = sizeof(*cmd) * num_clips;
615 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
616 if (unlikely(cmd == NULL)) {
617 DRM_ERROR("Fifo reserve failed.\n");
621 memset(cmd, 0, fifo_size);
622 for (i = 0; i < num_clips; i++, clips += increment) {
623 cmd[i].header = SVGA_CMD_UPDATE;
624 cmd[i].body.x = clips->x1;
625 cmd[i].body.y = clips->y1;
626 cmd[i].body.width = clips->x2 - clips->x1;
627 cmd[i].body.height = clips->y2 - clips->y1;
630 vmw_fifo_commit(dev_priv, fifo_size);