1 /************************************************************************** 2 * 3 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA 4 * All Rights Reserved. 5 * 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: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 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. 25 * 26 **************************************************************************/ 27 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> 32 33 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) 40 41 struct vmw_legacy_display { 42 struct list_head active; 43 44 unsigned num_active; 45 unsigned last_num_active; 46 47 struct vmw_framebuffer *fb; 48 }; 49 50 /** 51 * Display unit using the legacy register interface. 52 */ 53 struct vmw_legacy_display_unit { 54 struct vmw_display_unit base; 55 56 struct list_head active; 57 }; 58 59 static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu) 60 { 61 list_del_init(&ldu->active); 62 vmw_du_cleanup(&ldu->base); 63 kfree(ldu); 64 } 65 66 67 /* 68 * Legacy Display Unit CRTC functions 69 */ 70 71 static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc) 72 { 73 vmw_ldu_destroy(vmw_crtc_to_ldu(crtc)); 74 } 75 76 static int vmw_ldu_commit_list(struct vmw_private *dev_priv) 77 { 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; 82 int i = 0; 83 84 /* If there is no display topology the host just assumes 85 * that the guest will set the same layout as the host. 86 */ 87 if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) { 88 int w = 0, h = 0; 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); 93 i++; 94 } 95 96 if (crtc == NULL) 97 return 0; 98 fb = entry->base.crtc.primary->state->fb; 99 100 return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0], 101 fb->format->cpp[0] * 8, 102 fb->format->depth); 103 } 104 105 if (!list_empty(&lds->active)) { 106 entry = list_entry(lds->active.next, typeof(*entry), active); 107 fb = entry->base.crtc.primary->state->fb; 108 109 vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0], 110 fb->format->cpp[0] * 8, fb->format->depth); 111 } 112 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); 116 117 i = 0; 118 list_for_each_entry(entry, &lds->active, active) { 119 crtc = &entry->base.crtc; 120 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); 128 129 i++; 130 } 131 132 BUG_ON(i != lds->num_active); 133 134 lds->last_num_active = lds->num_active; 135 136 return 0; 137 } 138 139 static int vmw_ldu_del_active(struct vmw_private *vmw_priv, 140 struct vmw_legacy_display_unit *ldu) 141 { 142 struct vmw_legacy_display *ld = vmw_priv->ldu_priv; 143 if (list_empty(&ldu->active)) 144 return 0; 145 146 /* Must init otherwise list_empty(&ldu->active) will not work. */ 147 list_del_init(&ldu->active); 148 if (--(ld->num_active) == 0) { 149 BUG_ON(!ld->fb); 150 if (ld->fb->unpin) 151 ld->fb->unpin(ld->fb); 152 ld->fb = NULL; 153 } 154 155 return 0; 156 } 157 158 static int vmw_ldu_add_active(struct vmw_private *vmw_priv, 159 struct vmw_legacy_display_unit *ldu, 160 struct vmw_framebuffer *vfb) 161 { 162 struct vmw_legacy_display *ld = vmw_priv->ldu_priv; 163 struct vmw_legacy_display_unit *entry; 164 struct list_head *at; 165 166 BUG_ON(!ld->num_active && ld->fb); 167 if (vfb != ld->fb) { 168 if (ld->fb && ld->fb->unpin) 169 ld->fb->unpin(ld->fb); 170 vmw_svga_enable(vmw_priv); 171 if (vfb->pin) 172 vfb->pin(vfb); 173 ld->fb = vfb; 174 } 175 176 if (!list_empty(&ldu->active)) 177 return 0; 178 179 at = &ld->active; 180 list_for_each_entry(entry, &ld->active, active) { 181 if (entry->base.unit > ldu->base.unit) 182 break; 183 184 at = &entry->active; 185 } 186 187 list_add(&ldu->active, at); 188 189 ld->num_active++; 190 191 return 0; 192 } 193 194 /** 195 * vmw_ldu_crtc_mode_set_nofb - Enable svga 196 * 197 * @crtc: CRTC associated with the new screen 198 * 199 * For LDU, just enable the svga 200 */ 201 static void vmw_ldu_crtc_mode_set_nofb(struct drm_crtc *crtc) 202 { 203 } 204 205 /** 206 * vmw_ldu_crtc_helper_prepare - Noop 207 * 208 * @crtc: CRTC associated with the new screen 209 * 210 * Prepares the CRTC for a mode set, but we don't need to do anything here. 211 * 212 */ 213 static void vmw_ldu_crtc_helper_prepare(struct drm_crtc *crtc) 214 { 215 } 216 217 /** 218 * vmw_ldu_crtc_helper_commit - Noop 219 * 220 * @crtc: CRTC associated with the new screen 221 * 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. 226 */ 227 static void vmw_ldu_crtc_helper_commit(struct drm_crtc *crtc) 228 { 229 } 230 231 /** 232 * vmw_ldu_crtc_helper_disable - Turns off CRTC 233 * 234 * @crtc: CRTC to be turned off 235 */ 236 static void vmw_ldu_crtc_helper_disable(struct drm_crtc *crtc) 237 { 238 } 239 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, 247 }; 248 249 250 /* 251 * Legacy Display Unit encoder functions 252 */ 253 254 static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder) 255 { 256 vmw_ldu_destroy(vmw_encoder_to_ldu(encoder)); 257 } 258 259 static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = { 260 .destroy = vmw_ldu_encoder_destroy, 261 }; 262 263 /* 264 * Legacy Display Unit connector functions 265 */ 266 267 static void vmw_ldu_connector_destroy(struct drm_connector *connector) 268 { 269 vmw_ldu_destroy(vmw_connector_to_ldu(connector)); 270 } 271 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, 283 }; 284 285 static const struct 286 drm_connector_helper_funcs vmw_ldu_connector_helper_funcs = { 287 .best_encoder = drm_atomic_helper_best_encoder, 288 }; 289 290 /* 291 * Legacy Display Plane Functions 292 */ 293 294 /** 295 * vmw_ldu_primary_plane_cleanup_fb - Noop 296 * 297 * @plane: display plane 298 * @old_state: Contains the FB to clean up 299 * 300 * Unpins the display surface 301 * 302 * Returns 0 on success 303 */ 304 static void 305 vmw_ldu_primary_plane_cleanup_fb(struct drm_plane *plane, 306 struct drm_plane_state *old_state) 307 { 308 } 309 310 311 /** 312 * vmw_ldu_primary_plane_prepare_fb - Noop 313 * 314 * @plane: display plane 315 * @new_state: info on the new plane state, including the FB 316 * 317 * Returns 0 on success 318 */ 319 static int 320 vmw_ldu_primary_plane_prepare_fb(struct drm_plane *plane, 321 struct drm_plane_state *new_state) 322 { 323 return 0; 324 } 325 326 327 static void 328 vmw_ldu_primary_plane_atomic_update(struct drm_plane *plane, 329 struct drm_plane_state *old_state) 330 { 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; 336 337 338 ldu = vmw_crtc_to_ldu(crtc); 339 dev_priv = vmw_priv(plane->dev); 340 fb = plane->state->fb; 341 342 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL; 343 344 if (vfb) 345 vmw_ldu_add_active(dev_priv, ldu, vfb); 346 else 347 vmw_ldu_del_active(dev_priv, ldu); 348 349 vmw_ldu_commit_list(dev_priv); 350 } 351 352 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, 360 }; 361 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, 369 }; 370 371 /* 372 * Atomic Helpers 373 */ 374 static const struct 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, 380 }; 381 382 static const struct 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, 388 }; 389 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, 398 }; 399 400 401 static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit) 402 { 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; 409 int ret; 410 411 ldu = kzalloc(sizeof(*ldu), GFP_KERNEL); 412 if (!ldu) 413 return -ENOMEM; 414 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; 421 422 INIT_LIST_HEAD(&ldu->active); 423 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; 428 429 /* 430 * Remove this after enabling atomic because property values can 431 * only exist in a state object 432 */ 433 ldu->base.is_implicit = true; 434 435 /* Initialize primary plane */ 436 vmw_du_plane_reset(primary); 437 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); 443 if (ret) { 444 DRM_ERROR("Failed to initialize primary plane"); 445 goto err_free; 446 } 447 448 drm_plane_helper_add(primary, &vmw_ldu_primary_plane_helper_funcs); 449 450 /* Initialize cursor plane */ 451 vmw_du_plane_reset(cursor); 452 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); 458 if (ret) { 459 DRM_ERROR("Failed to initialize cursor plane"); 460 drm_plane_cleanup(&ldu->base.primary); 461 goto err_free; 462 } 463 464 drm_plane_helper_add(cursor, &vmw_ldu_cursor_plane_helper_funcs); 465 466 467 vmw_du_connector_reset(connector); 468 ret = drm_connector_init(dev, connector, &vmw_legacy_connector_funcs, 469 DRM_MODE_CONNECTOR_VIRTUAL); 470 if (ret) { 471 DRM_ERROR("Failed to initialize connector\n"); 472 goto err_free; 473 } 474 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; 478 479 480 ret = drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs, 481 DRM_MODE_ENCODER_VIRTUAL, NULL); 482 if (ret) { 483 DRM_ERROR("Failed to initialize encoder\n"); 484 goto err_free_connector; 485 } 486 487 (void) drm_mode_connector_attach_encoder(connector, encoder); 488 encoder->possible_crtcs = (1 << unit); 489 encoder->possible_clones = 0; 490 491 ret = drm_connector_register(connector); 492 if (ret) { 493 DRM_ERROR("Failed to register connector\n"); 494 goto err_free_encoder; 495 } 496 497 498 vmw_du_crtc_reset(crtc); 499 ret = drm_crtc_init_with_planes(dev, crtc, &ldu->base.primary, 500 &ldu->base.cursor, 501 &vmw_legacy_crtc_funcs, NULL); 502 if (ret) { 503 DRM_ERROR("Failed to initialize CRTC\n"); 504 goto err_free_unregister; 505 } 506 507 drm_crtc_helper_add(crtc, &vmw_ldu_crtc_helper_funcs); 508 509 drm_mode_crtc_set_gamma_size(crtc, 256); 510 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 519 (&connector->base, 520 dev_priv->implicit_placement_property, 521 1); 522 523 return 0; 524 525 err_free_unregister: 526 drm_connector_unregister(connector); 527 err_free_encoder: 528 drm_encoder_cleanup(encoder); 529 err_free_connector: 530 drm_connector_cleanup(connector); 531 err_free: 532 kfree(ldu); 533 return ret; 534 } 535 536 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv) 537 { 538 struct drm_device *dev = dev_priv->dev; 539 int i, ret; 540 541 if (dev_priv->ldu_priv) { 542 DRM_INFO("ldu system already on\n"); 543 return -EINVAL; 544 } 545 546 dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL); 547 if (!dev_priv->ldu_priv) 548 return -ENOMEM; 549 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; 554 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); 558 else 559 ret = drm_vblank_init(dev, 1); 560 if (ret != 0) 561 goto err_free; 562 563 vmw_kms_create_implicit_placement_property(dev_priv, true); 564 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); 568 else 569 vmw_ldu_init(dev_priv, 0); 570 571 dev_priv->active_display_unit = vmw_du_legacy; 572 573 DRM_INFO("Legacy Display Unit initialized\n"); 574 575 return 0; 576 577 err_free: 578 kfree(dev_priv->ldu_priv); 579 dev_priv->ldu_priv = NULL; 580 return ret; 581 } 582 583 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv) 584 { 585 struct drm_device *dev = dev_priv->dev; 586 587 if (!dev_priv->ldu_priv) 588 return -ENOSYS; 589 590 drm_vblank_cleanup(dev); 591 592 BUG_ON(!list_empty(&dev_priv->ldu_priv->active)); 593 594 kfree(dev_priv->ldu_priv); 595 596 return 0; 597 } 598 599 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) 605 { 606 size_t fifo_size; 607 int i; 608 609 struct { 610 uint32_t header; 611 SVGAFifoCmdUpdate body; 612 } *cmd; 613 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"); 618 return -ENOMEM; 619 } 620 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; 628 } 629 630 vmw_fifo_commit(dev_priv, fifo_size); 631 return 0; 632 } 633