Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR MIT
0002 /**************************************************************************
0003  *
0004  * Copyright 2009-2022 VMware, Inc., Palo Alto, CA., USA
0005  *
0006  * Permission is hereby granted, free of charge, to any person obtaining a
0007  * copy of this software and associated documentation files (the
0008  * "Software"), to deal in the Software without restriction, including
0009  * without limitation the rights to use, copy, modify, merge, publish,
0010  * distribute, sub license, and/or sell copies of the Software, and to
0011  * permit persons to whom the Software is furnished to do so, subject to
0012  * the following conditions:
0013  *
0014  * The above copyright notice and this permission notice (including the
0015  * next paragraph) shall be included in all copies or substantial portions
0016  * of the Software.
0017  *
0018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0020  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
0021  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
0022  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0023  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
0024  * USE OR OTHER DEALINGS IN THE SOFTWARE.
0025  *
0026  **************************************************************************/
0027 
0028 #include <drm/drm_atomic.h>
0029 #include <drm/drm_atomic_helper.h>
0030 #include <drm/drm_fourcc.h>
0031 #include <drm/drm_plane_helper.h>
0032 #include <drm/drm_vblank.h>
0033 
0034 #include "vmwgfx_kms.h"
0035 
0036 #define vmw_crtc_to_ldu(x) \
0037     container_of(x, struct vmw_legacy_display_unit, base.crtc)
0038 #define vmw_encoder_to_ldu(x) \
0039     container_of(x, struct vmw_legacy_display_unit, base.encoder)
0040 #define vmw_connector_to_ldu(x) \
0041     container_of(x, struct vmw_legacy_display_unit, base.connector)
0042 
0043 struct vmw_legacy_display {
0044     struct list_head active;
0045 
0046     unsigned num_active;
0047     unsigned last_num_active;
0048 
0049     struct vmw_framebuffer *fb;
0050 };
0051 
0052 /*
0053  * Display unit using the legacy register interface.
0054  */
0055 struct vmw_legacy_display_unit {
0056     struct vmw_display_unit base;
0057 
0058     struct list_head active;
0059 };
0060 
0061 static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
0062 {
0063     list_del_init(&ldu->active);
0064     vmw_du_cleanup(&ldu->base);
0065     kfree(ldu);
0066 }
0067 
0068 
0069 /*
0070  * Legacy Display Unit CRTC functions
0071  */
0072 
0073 static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
0074 {
0075     vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
0076 }
0077 
0078 static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
0079 {
0080     struct vmw_legacy_display *lds = dev_priv->ldu_priv;
0081     struct vmw_legacy_display_unit *entry;
0082     struct drm_framebuffer *fb = NULL;
0083     struct drm_crtc *crtc = NULL;
0084     int i;
0085 
0086     /* If there is no display topology the host just assumes
0087      * that the guest will set the same layout as the host.
0088      */
0089     if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
0090         int w = 0, h = 0;
0091         list_for_each_entry(entry, &lds->active, active) {
0092             crtc = &entry->base.crtc;
0093             w = max(w, crtc->x + crtc->mode.hdisplay);
0094             h = max(h, crtc->y + crtc->mode.vdisplay);
0095         }
0096 
0097         if (crtc == NULL)
0098             return 0;
0099         fb = crtc->primary->state->fb;
0100 
0101         return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0],
0102                       fb->format->cpp[0] * 8,
0103                       fb->format->depth);
0104     }
0105 
0106     if (!list_empty(&lds->active)) {
0107         entry = list_entry(lds->active.next, typeof(*entry), active);
0108         fb = entry->base.crtc.primary->state->fb;
0109 
0110         vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0],
0111                    fb->format->cpp[0] * 8, fb->format->depth);
0112     }
0113 
0114     /* Make sure we always show something. */
0115     vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
0116           lds->num_active ? lds->num_active : 1);
0117 
0118     i = 0;
0119     list_for_each_entry(entry, &lds->active, active) {
0120         crtc = &entry->base.crtc;
0121 
0122         vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
0123         vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
0124         vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
0125         vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
0126         vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
0127         vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
0128 
0129         i++;
0130     }
0131 
0132     BUG_ON(i != lds->num_active);
0133 
0134     lds->last_num_active = lds->num_active;
0135 
0136     return 0;
0137 }
0138 
0139 static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
0140                   struct vmw_legacy_display_unit *ldu)
0141 {
0142     struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
0143     if (list_empty(&ldu->active))
0144         return 0;
0145 
0146     /* Must init otherwise list_empty(&ldu->active) will not work. */
0147     list_del_init(&ldu->active);
0148     if (--(ld->num_active) == 0) {
0149         BUG_ON(!ld->fb);
0150         if (ld->fb->unpin)
0151             ld->fb->unpin(ld->fb);
0152         ld->fb = NULL;
0153     }
0154 
0155     return 0;
0156 }
0157 
0158 static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
0159                   struct vmw_legacy_display_unit *ldu,
0160                   struct vmw_framebuffer *vfb)
0161 {
0162     struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
0163     struct vmw_legacy_display_unit *entry;
0164     struct list_head *at;
0165 
0166     BUG_ON(!ld->num_active && ld->fb);
0167     if (vfb != ld->fb) {
0168         if (ld->fb && ld->fb->unpin)
0169             ld->fb->unpin(ld->fb);
0170         vmw_svga_enable(vmw_priv);
0171         if (vfb->pin)
0172             vfb->pin(vfb);
0173         ld->fb = vfb;
0174     }
0175 
0176     if (!list_empty(&ldu->active))
0177         return 0;
0178 
0179     at = &ld->active;
0180     list_for_each_entry(entry, &ld->active, active) {
0181         if (entry->base.unit > ldu->base.unit)
0182             break;
0183 
0184         at = &entry->active;
0185     }
0186 
0187     list_add(&ldu->active, at);
0188 
0189     ld->num_active++;
0190 
0191     return 0;
0192 }
0193 
0194 /**
0195  * vmw_ldu_crtc_mode_set_nofb - Enable svga
0196  *
0197  * @crtc: CRTC associated with the new screen
0198  *
0199  * For LDU, just enable the svga
0200  */
0201 static void vmw_ldu_crtc_mode_set_nofb(struct drm_crtc *crtc)
0202 {
0203 }
0204 
0205 /**
0206  * vmw_ldu_crtc_atomic_enable - Noop
0207  *
0208  * @crtc: CRTC associated with the new screen
0209  * @state: Unused
0210  *
0211  * This is called after a mode set has been completed.  Here's
0212  * usually a good place to call vmw_ldu_add_active/vmw_ldu_del_active
0213  * but since for LDU the display plane is closely tied to the
0214  * CRTC, it makes more sense to do those at plane update time.
0215  */
0216 static void vmw_ldu_crtc_atomic_enable(struct drm_crtc *crtc,
0217                        struct drm_atomic_state *state)
0218 {
0219 }
0220 
0221 /**
0222  * vmw_ldu_crtc_atomic_disable - Turns off CRTC
0223  *
0224  * @crtc: CRTC to be turned off
0225  * @state: Unused
0226  */
0227 static void vmw_ldu_crtc_atomic_disable(struct drm_crtc *crtc,
0228                     struct drm_atomic_state *state)
0229 {
0230 }
0231 
0232 static const struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
0233     .gamma_set = vmw_du_crtc_gamma_set,
0234     .destroy = vmw_ldu_crtc_destroy,
0235     .reset = vmw_du_crtc_reset,
0236     .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
0237     .atomic_destroy_state = vmw_du_crtc_destroy_state,
0238     .set_config = drm_atomic_helper_set_config,
0239     .get_vblank_counter = vmw_get_vblank_counter,
0240     .enable_vblank = vmw_enable_vblank,
0241     .disable_vblank = vmw_disable_vblank,
0242 };
0243 
0244 
0245 /*
0246  * Legacy Display Unit encoder functions
0247  */
0248 
0249 static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
0250 {
0251     vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
0252 }
0253 
0254 static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
0255     .destroy = vmw_ldu_encoder_destroy,
0256 };
0257 
0258 /*
0259  * Legacy Display Unit connector functions
0260  */
0261 
0262 static void vmw_ldu_connector_destroy(struct drm_connector *connector)
0263 {
0264     vmw_ldu_destroy(vmw_connector_to_ldu(connector));
0265 }
0266 
0267 static const struct drm_connector_funcs vmw_legacy_connector_funcs = {
0268     .dpms = vmw_du_connector_dpms,
0269     .detect = vmw_du_connector_detect,
0270     .fill_modes = vmw_du_connector_fill_modes,
0271     .destroy = vmw_ldu_connector_destroy,
0272     .reset = vmw_du_connector_reset,
0273     .atomic_duplicate_state = vmw_du_connector_duplicate_state,
0274     .atomic_destroy_state = vmw_du_connector_destroy_state,
0275 };
0276 
0277 static const struct
0278 drm_connector_helper_funcs vmw_ldu_connector_helper_funcs = {
0279 };
0280 
0281 /*
0282  * Legacy Display Plane Functions
0283  */
0284 
0285 static void
0286 vmw_ldu_primary_plane_atomic_update(struct drm_plane *plane,
0287                     struct drm_atomic_state *state)
0288 {
0289     struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
0290                                        plane);
0291     struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
0292                                        plane);
0293     struct vmw_private *dev_priv;
0294     struct vmw_legacy_display_unit *ldu;
0295     struct vmw_framebuffer *vfb;
0296     struct drm_framebuffer *fb;
0297     struct drm_crtc *crtc = new_state->crtc ?: old_state->crtc;
0298 
0299 
0300     ldu = vmw_crtc_to_ldu(crtc);
0301     dev_priv = vmw_priv(plane->dev);
0302     fb       = new_state->fb;
0303 
0304     vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
0305 
0306     if (vfb)
0307         vmw_ldu_add_active(dev_priv, ldu, vfb);
0308     else
0309         vmw_ldu_del_active(dev_priv, ldu);
0310 
0311     vmw_ldu_commit_list(dev_priv);
0312 }
0313 
0314 
0315 static const struct drm_plane_funcs vmw_ldu_plane_funcs = {
0316     .update_plane = drm_atomic_helper_update_plane,
0317     .disable_plane = drm_atomic_helper_disable_plane,
0318     .destroy = vmw_du_primary_plane_destroy,
0319     .reset = vmw_du_plane_reset,
0320     .atomic_duplicate_state = vmw_du_plane_duplicate_state,
0321     .atomic_destroy_state = vmw_du_plane_destroy_state,
0322 };
0323 
0324 static const struct drm_plane_funcs vmw_ldu_cursor_funcs = {
0325     .update_plane = drm_atomic_helper_update_plane,
0326     .disable_plane = drm_atomic_helper_disable_plane,
0327     .destroy = vmw_du_cursor_plane_destroy,
0328     .reset = vmw_du_plane_reset,
0329     .atomic_duplicate_state = vmw_du_plane_duplicate_state,
0330     .atomic_destroy_state = vmw_du_plane_destroy_state,
0331 };
0332 
0333 /*
0334  * Atomic Helpers
0335  */
0336 static const struct
0337 drm_plane_helper_funcs vmw_ldu_cursor_plane_helper_funcs = {
0338     .atomic_check = vmw_du_cursor_plane_atomic_check,
0339     .atomic_update = vmw_du_cursor_plane_atomic_update,
0340     .prepare_fb = vmw_du_cursor_plane_prepare_fb,
0341     .cleanup_fb = vmw_du_cursor_plane_cleanup_fb,
0342 };
0343 
0344 static const struct
0345 drm_plane_helper_funcs vmw_ldu_primary_plane_helper_funcs = {
0346     .atomic_check = vmw_du_primary_plane_atomic_check,
0347     .atomic_update = vmw_ldu_primary_plane_atomic_update,
0348 };
0349 
0350 static const struct drm_crtc_helper_funcs vmw_ldu_crtc_helper_funcs = {
0351     .mode_set_nofb = vmw_ldu_crtc_mode_set_nofb,
0352     .atomic_check = vmw_du_crtc_atomic_check,
0353     .atomic_begin = vmw_du_crtc_atomic_begin,
0354     .atomic_flush = vmw_du_crtc_atomic_flush,
0355     .atomic_enable = vmw_ldu_crtc_atomic_enable,
0356     .atomic_disable = vmw_ldu_crtc_atomic_disable,
0357 };
0358 
0359 
0360 static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
0361 {
0362     struct vmw_legacy_display_unit *ldu;
0363     struct drm_device *dev = &dev_priv->drm;
0364     struct drm_connector *connector;
0365     struct drm_encoder *encoder;
0366     struct drm_plane *primary;
0367     struct vmw_cursor_plane *cursor;
0368     struct drm_crtc *crtc;
0369     int ret;
0370 
0371     ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
0372     if (!ldu)
0373         return -ENOMEM;
0374 
0375     ldu->base.unit = unit;
0376     crtc = &ldu->base.crtc;
0377     encoder = &ldu->base.encoder;
0378     connector = &ldu->base.connector;
0379     primary = &ldu->base.primary;
0380     cursor = &ldu->base.cursor;
0381 
0382     INIT_LIST_HEAD(&ldu->active);
0383 
0384     ldu->base.pref_active = (unit == 0);
0385     ldu->base.pref_width = dev_priv->initial_width;
0386     ldu->base.pref_height = dev_priv->initial_height;
0387     ldu->base.pref_mode = NULL;
0388 
0389     /*
0390      * Remove this after enabling atomic because property values can
0391      * only exist in a state object
0392      */
0393     ldu->base.is_implicit = true;
0394 
0395     /* Initialize primary plane */
0396     ret = drm_universal_plane_init(dev, primary,
0397                        0, &vmw_ldu_plane_funcs,
0398                        vmw_primary_plane_formats,
0399                        ARRAY_SIZE(vmw_primary_plane_formats),
0400                        NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
0401     if (ret) {
0402         DRM_ERROR("Failed to initialize primary plane");
0403         goto err_free;
0404     }
0405 
0406     drm_plane_helper_add(primary, &vmw_ldu_primary_plane_helper_funcs);
0407 
0408     /*
0409      * We're going to be using traces and software cursors
0410      */
0411     if (vmw_cmd_supported(dev_priv)) {
0412         /* Initialize cursor plane */
0413         ret = drm_universal_plane_init(dev, &cursor->base,
0414                            0, &vmw_ldu_cursor_funcs,
0415                            vmw_cursor_plane_formats,
0416                            ARRAY_SIZE(vmw_cursor_plane_formats),
0417                            NULL, DRM_PLANE_TYPE_CURSOR, NULL);
0418         if (ret) {
0419             DRM_ERROR("Failed to initialize cursor plane");
0420             drm_plane_cleanup(&ldu->base.primary);
0421             goto err_free;
0422         }
0423 
0424         drm_plane_helper_add(&cursor->base, &vmw_ldu_cursor_plane_helper_funcs);
0425     }
0426 
0427     ret = drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
0428                  DRM_MODE_CONNECTOR_VIRTUAL);
0429     if (ret) {
0430         DRM_ERROR("Failed to initialize connector\n");
0431         goto err_free;
0432     }
0433 
0434     drm_connector_helper_add(connector, &vmw_ldu_connector_helper_funcs);
0435     connector->status = vmw_du_connector_detect(connector, true);
0436 
0437     ret = drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
0438                    DRM_MODE_ENCODER_VIRTUAL, NULL);
0439     if (ret) {
0440         DRM_ERROR("Failed to initialize encoder\n");
0441         goto err_free_connector;
0442     }
0443 
0444     (void) drm_connector_attach_encoder(connector, encoder);
0445     encoder->possible_crtcs = (1 << unit);
0446     encoder->possible_clones = 0;
0447 
0448     ret = drm_connector_register(connector);
0449     if (ret) {
0450         DRM_ERROR("Failed to register connector\n");
0451         goto err_free_encoder;
0452     }
0453 
0454     ret = drm_crtc_init_with_planes(dev, crtc, primary,
0455               vmw_cmd_supported(dev_priv) ? &cursor->base : NULL,
0456               &vmw_legacy_crtc_funcs, NULL);
0457     if (ret) {
0458         DRM_ERROR("Failed to initialize CRTC\n");
0459         goto err_free_unregister;
0460     }
0461 
0462     drm_crtc_helper_add(crtc, &vmw_ldu_crtc_helper_funcs);
0463 
0464     drm_mode_crtc_set_gamma_size(crtc, 256);
0465 
0466     drm_object_attach_property(&connector->base,
0467                    dev_priv->hotplug_mode_update_property, 1);
0468     drm_object_attach_property(&connector->base,
0469                    dev->mode_config.suggested_x_property, 0);
0470     drm_object_attach_property(&connector->base,
0471                    dev->mode_config.suggested_y_property, 0);
0472     if (dev_priv->implicit_placement_property)
0473         drm_object_attach_property
0474             (&connector->base,
0475              dev_priv->implicit_placement_property,
0476              1);
0477 
0478     return 0;
0479 
0480 err_free_unregister:
0481     drm_connector_unregister(connector);
0482 err_free_encoder:
0483     drm_encoder_cleanup(encoder);
0484 err_free_connector:
0485     drm_connector_cleanup(connector);
0486 err_free:
0487     kfree(ldu);
0488     return ret;
0489 }
0490 
0491 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv)
0492 {
0493     struct drm_device *dev = &dev_priv->drm;
0494     int i, ret;
0495     int num_display_units = (dev_priv->capabilities & SVGA_CAP_MULTIMON) ?
0496                     VMWGFX_NUM_DISPLAY_UNITS : 1;
0497 
0498     if (unlikely(dev_priv->ldu_priv)) {
0499         return -EINVAL;
0500     }
0501 
0502     dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
0503     if (!dev_priv->ldu_priv)
0504         return -ENOMEM;
0505 
0506     INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
0507     dev_priv->ldu_priv->num_active = 0;
0508     dev_priv->ldu_priv->last_num_active = 0;
0509     dev_priv->ldu_priv->fb = NULL;
0510 
0511     ret = drm_vblank_init(dev, num_display_units);
0512     if (ret != 0)
0513         goto err_free;
0514 
0515     vmw_kms_create_implicit_placement_property(dev_priv);
0516 
0517     for (i = 0; i < num_display_units; ++i) {
0518         ret = vmw_ldu_init(dev_priv, i);
0519         if (ret != 0)
0520             goto err_free;
0521     }
0522 
0523     dev_priv->active_display_unit = vmw_du_legacy;
0524 
0525     drm_mode_config_reset(dev);
0526 
0527     return 0;
0528 
0529 err_free:
0530     kfree(dev_priv->ldu_priv);
0531     dev_priv->ldu_priv = NULL;
0532     return ret;
0533 }
0534 
0535 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv)
0536 {
0537     if (!dev_priv->ldu_priv)
0538         return -ENOSYS;
0539 
0540     BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
0541 
0542     kfree(dev_priv->ldu_priv);
0543 
0544     return 0;
0545 }
0546 
0547 
0548 int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
0549                 struct vmw_framebuffer *framebuffer,
0550                 unsigned int flags, unsigned int color,
0551                 struct drm_clip_rect *clips,
0552                 unsigned int num_clips, int increment)
0553 {
0554     size_t fifo_size;
0555     int i;
0556 
0557     struct {
0558         uint32_t header;
0559         SVGAFifoCmdUpdate body;
0560     } *cmd;
0561 
0562     fifo_size = sizeof(*cmd) * num_clips;
0563     cmd = VMW_CMD_RESERVE(dev_priv, fifo_size);
0564     if (unlikely(cmd == NULL))
0565         return -ENOMEM;
0566 
0567     memset(cmd, 0, fifo_size);
0568     for (i = 0; i < num_clips; i++, clips += increment) {
0569         cmd[i].header = SVGA_CMD_UPDATE;
0570         cmd[i].body.x = clips->x1;
0571         cmd[i].body.y = clips->y1;
0572         cmd[i].body.width = clips->x2 - clips->x1;
0573         cmd[i].body.height = clips->y2 - clips->y1;
0574     }
0575 
0576     vmw_cmd_commit(dev_priv, fifo_size);
0577     return 0;
0578 }