1 /* 2 * Copyright 2013 Red Hat Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: Dave Airlie 23 * Alon Levy 24 */ 25 26 #include <linux/crc32.h> 27 #include <linux/delay.h> 28 29 #include <drm/drm_atomic.h> 30 #include <drm/drm_atomic_helper.h> 31 #include <drm/drm_gem_framebuffer_helper.h> 32 #include <drm/drm_plane_helper.h> 33 #include <drm/drm_probe_helper.h> 34 35 #include "qxl_drv.h" 36 #include "qxl_object.h" 37 38 static bool qxl_head_enabled(struct qxl_head *head) 39 { 40 return head->width && head->height; 41 } 42 43 static int qxl_alloc_client_monitors_config(struct qxl_device *qdev, 44 unsigned int count) 45 { 46 if (qdev->client_monitors_config && 47 count > qdev->client_monitors_config->count) { 48 kfree(qdev->client_monitors_config); 49 qdev->client_monitors_config = NULL; 50 } 51 if (!qdev->client_monitors_config) { 52 qdev->client_monitors_config = kzalloc( 53 struct_size(qdev->client_monitors_config, 54 heads, count), GFP_KERNEL); 55 if (!qdev->client_monitors_config) 56 return -ENOMEM; 57 } 58 qdev->client_monitors_config->count = count; 59 return 0; 60 } 61 62 enum { 63 MONITORS_CONFIG_MODIFIED, 64 MONITORS_CONFIG_UNCHANGED, 65 MONITORS_CONFIG_BAD_CRC, 66 MONITORS_CONFIG_ERROR, 67 }; 68 69 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev) 70 { 71 int i; 72 int num_monitors; 73 uint32_t crc; 74 int status = MONITORS_CONFIG_UNCHANGED; 75 76 num_monitors = qdev->rom->client_monitors_config.count; 77 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config, 78 sizeof(qdev->rom->client_monitors_config)); 79 if (crc != qdev->rom->client_monitors_config_crc) 80 return MONITORS_CONFIG_BAD_CRC; 81 if (!num_monitors) { 82 DRM_DEBUG_KMS("no client monitors configured\n"); 83 return status; 84 } 85 if (num_monitors > qxl_num_crtc) { 86 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n", 87 qxl_num_crtc, num_monitors); 88 num_monitors = qxl_num_crtc; 89 } else { 90 num_monitors = qdev->rom->client_monitors_config.count; 91 } 92 if (qdev->client_monitors_config 93 && (num_monitors != qdev->client_monitors_config->count)) { 94 status = MONITORS_CONFIG_MODIFIED; 95 } 96 if (qxl_alloc_client_monitors_config(qdev, num_monitors)) { 97 status = MONITORS_CONFIG_ERROR; 98 return status; 99 } 100 /* we copy max from the client but it isn't used */ 101 qdev->client_monitors_config->max_allowed = qxl_num_crtc; 102 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) { 103 struct qxl_urect *c_rect = 104 &qdev->rom->client_monitors_config.heads[i]; 105 struct qxl_head *client_head = 106 &qdev->client_monitors_config->heads[i]; 107 if (client_head->x != c_rect->left) { 108 client_head->x = c_rect->left; 109 status = MONITORS_CONFIG_MODIFIED; 110 } 111 if (client_head->y != c_rect->top) { 112 client_head->y = c_rect->top; 113 status = MONITORS_CONFIG_MODIFIED; 114 } 115 if (client_head->width != c_rect->right - c_rect->left) { 116 client_head->width = c_rect->right - c_rect->left; 117 status = MONITORS_CONFIG_MODIFIED; 118 } 119 if (client_head->height != c_rect->bottom - c_rect->top) { 120 client_head->height = c_rect->bottom - c_rect->top; 121 status = MONITORS_CONFIG_MODIFIED; 122 } 123 if (client_head->surface_id != 0) { 124 client_head->surface_id = 0; 125 status = MONITORS_CONFIG_MODIFIED; 126 } 127 if (client_head->id != i) { 128 client_head->id = i; 129 status = MONITORS_CONFIG_MODIFIED; 130 } 131 if (client_head->flags != 0) { 132 client_head->flags = 0; 133 status = MONITORS_CONFIG_MODIFIED; 134 } 135 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height, 136 client_head->x, client_head->y); 137 } 138 139 return status; 140 } 141 142 static void qxl_update_offset_props(struct qxl_device *qdev) 143 { 144 struct drm_device *dev = &qdev->ddev; 145 struct drm_connector *connector; 146 struct qxl_output *output; 147 struct qxl_head *head; 148 149 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 150 output = drm_connector_to_qxl_output(connector); 151 152 head = &qdev->client_monitors_config->heads[output->index]; 153 154 drm_object_property_set_value(&connector->base, 155 dev->mode_config.suggested_x_property, head->x); 156 drm_object_property_set_value(&connector->base, 157 dev->mode_config.suggested_y_property, head->y); 158 } 159 } 160 161 void qxl_display_read_client_monitors_config(struct qxl_device *qdev) 162 { 163 struct drm_device *dev = &qdev->ddev; 164 int status, retries; 165 166 for (retries = 0; retries < 10; retries++) { 167 status = qxl_display_copy_rom_client_monitors_config(qdev); 168 if (status != MONITORS_CONFIG_BAD_CRC) 169 break; 170 udelay(5); 171 } 172 if (status == MONITORS_CONFIG_ERROR) { 173 DRM_DEBUG_KMS("ignoring client monitors config: error"); 174 return; 175 } 176 if (status == MONITORS_CONFIG_BAD_CRC) { 177 DRM_DEBUG_KMS("ignoring client monitors config: bad crc"); 178 return; 179 } 180 if (status == MONITORS_CONFIG_UNCHANGED) { 181 DRM_DEBUG_KMS("ignoring client monitors config: unchanged"); 182 return; 183 } 184 185 drm_modeset_lock_all(dev); 186 qxl_update_offset_props(qdev); 187 drm_modeset_unlock_all(dev); 188 if (!drm_helper_hpd_irq_event(dev)) { 189 /* notify that the monitor configuration changed, to 190 adjust at the arbitrary resolution */ 191 drm_kms_helper_hotplug_event(dev); 192 } 193 } 194 195 static int qxl_check_mode(struct qxl_device *qdev, 196 unsigned int width, 197 unsigned int height) 198 { 199 unsigned int stride; 200 unsigned int size; 201 202 if (check_mul_overflow(width, 4u, &stride)) 203 return -EINVAL; 204 if (check_mul_overflow(stride, height, &size)) 205 return -EINVAL; 206 if (size > qdev->vram_size) 207 return -ENOMEM; 208 return 0; 209 } 210 211 static int qxl_check_framebuffer(struct qxl_device *qdev, 212 struct qxl_bo *bo) 213 { 214 return qxl_check_mode(qdev, bo->surf.width, bo->surf.height); 215 } 216 217 static int qxl_add_mode(struct drm_connector *connector, 218 unsigned int width, 219 unsigned int height, 220 bool preferred) 221 { 222 struct drm_device *dev = connector->dev; 223 struct qxl_device *qdev = dev->dev_private; 224 struct drm_display_mode *mode = NULL; 225 int rc; 226 227 rc = qxl_check_mode(qdev, width, height); 228 if (rc != 0) 229 return 0; 230 231 mode = drm_cvt_mode(dev, width, height, 60, false, false, false); 232 if (preferred) 233 mode->type |= DRM_MODE_TYPE_PREFERRED; 234 mode->hdisplay = width; 235 mode->vdisplay = height; 236 drm_mode_set_name(mode); 237 drm_mode_probed_add(connector, mode); 238 return 1; 239 } 240 241 static int qxl_add_monitors_config_modes(struct drm_connector *connector) 242 { 243 struct drm_device *dev = connector->dev; 244 struct qxl_device *qdev = dev->dev_private; 245 struct qxl_output *output = drm_connector_to_qxl_output(connector); 246 int h = output->index; 247 struct qxl_head *head; 248 249 if (!qdev->monitors_config) 250 return 0; 251 if (h >= qxl_num_crtc) 252 return 0; 253 if (!qdev->client_monitors_config) 254 return 0; 255 if (h >= qdev->client_monitors_config->count) 256 return 0; 257 258 head = &qdev->client_monitors_config->heads[h]; 259 DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height); 260 261 return qxl_add_mode(connector, head->width, head->height, true); 262 } 263 264 static struct mode_size { 265 int w; 266 int h; 267 } extra_modes[] = { 268 { 720, 480}, 269 {1152, 768}, 270 {1280, 854}, 271 }; 272 273 static int qxl_add_extra_modes(struct drm_connector *connector) 274 { 275 int i, ret = 0; 276 277 for (i = 0; i < ARRAY_SIZE(extra_modes); i++) 278 ret += qxl_add_mode(connector, 279 extra_modes[i].w, 280 extra_modes[i].h, 281 false); 282 return ret; 283 } 284 285 static void qxl_send_monitors_config(struct qxl_device *qdev) 286 { 287 int i; 288 289 BUG_ON(!qdev->ram_header->monitors_config); 290 291 if (qdev->monitors_config->count == 0) 292 return; 293 294 for (i = 0 ; i < qdev->monitors_config->count ; ++i) { 295 struct qxl_head *head = &qdev->monitors_config->heads[i]; 296 297 if (head->y > 8192 || head->x > 8192 || 298 head->width > 8192 || head->height > 8192) { 299 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n", 300 i, head->width, head->height, 301 head->x, head->y); 302 return; 303 } 304 } 305 qxl_io_monitors_config(qdev); 306 } 307 308 static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc, 309 const char *reason) 310 { 311 struct drm_device *dev = crtc->dev; 312 struct qxl_device *qdev = dev->dev_private; 313 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); 314 struct qxl_head head; 315 int oldcount, i = qcrtc->index; 316 317 if (!qdev->primary_bo) { 318 DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason); 319 return; 320 } 321 322 if (!qdev->monitors_config || qxl_num_crtc <= i) 323 return; 324 325 head.id = i; 326 head.flags = 0; 327 oldcount = qdev->monitors_config->count; 328 if (crtc->state->active) { 329 struct drm_display_mode *mode = &crtc->mode; 330 331 head.width = mode->hdisplay; 332 head.height = mode->vdisplay; 333 head.x = crtc->x; 334 head.y = crtc->y; 335 if (qdev->monitors_config->count < i + 1) 336 qdev->monitors_config->count = i + 1; 337 if (qdev->primary_bo == qdev->dumb_shadow_bo) 338 head.x += qdev->dumb_heads[i].x; 339 } else if (i > 0) { 340 head.width = 0; 341 head.height = 0; 342 head.x = 0; 343 head.y = 0; 344 if (qdev->monitors_config->count == i + 1) 345 qdev->monitors_config->count = i; 346 } else { 347 DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason); 348 return; 349 } 350 351 if (head.width == qdev->monitors_config->heads[i].width && 352 head.height == qdev->monitors_config->heads[i].height && 353 head.x == qdev->monitors_config->heads[i].x && 354 head.y == qdev->monitors_config->heads[i].y && 355 oldcount == qdev->monitors_config->count) 356 return; 357 358 DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n", 359 i, head.width, head.height, head.x, head.y, 360 crtc->state->active ? "on" : "off", reason); 361 if (oldcount != qdev->monitors_config->count) 362 DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n", 363 oldcount, qdev->monitors_config->count, 364 qxl_num_crtc); 365 366 qdev->monitors_config->heads[i] = head; 367 qdev->monitors_config->max_allowed = qxl_num_crtc; 368 qxl_send_monitors_config(qdev); 369 } 370 371 static void qxl_crtc_atomic_flush(struct drm_crtc *crtc, 372 struct drm_crtc_state *old_crtc_state) 373 { 374 qxl_crtc_update_monitors_config(crtc, "flush"); 375 } 376 377 static void qxl_crtc_destroy(struct drm_crtc *crtc) 378 { 379 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc); 380 381 qxl_bo_unref(&qxl_crtc->cursor_bo); 382 drm_crtc_cleanup(crtc); 383 kfree(qxl_crtc); 384 } 385 386 static const struct drm_crtc_funcs qxl_crtc_funcs = { 387 .set_config = drm_atomic_helper_set_config, 388 .destroy = qxl_crtc_destroy, 389 .page_flip = drm_atomic_helper_page_flip, 390 .reset = drm_atomic_helper_crtc_reset, 391 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 392 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 393 }; 394 395 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb, 396 struct drm_file *file_priv, 397 unsigned int flags, unsigned int color, 398 struct drm_clip_rect *clips, 399 unsigned int num_clips) 400 { 401 /* TODO: vmwgfx where this was cribbed from had locking. Why? */ 402 struct qxl_device *qdev = fb->dev->dev_private; 403 struct drm_clip_rect norect; 404 struct qxl_bo *qobj; 405 bool is_primary; 406 int inc = 1; 407 408 drm_modeset_lock_all(fb->dev); 409 410 qobj = gem_to_qxl_bo(fb->obj[0]); 411 /* if we aren't primary surface ignore this */ 412 is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary; 413 if (!is_primary) { 414 drm_modeset_unlock_all(fb->dev); 415 return 0; 416 } 417 418 if (!num_clips) { 419 num_clips = 1; 420 clips = &norect; 421 norect.x1 = norect.y1 = 0; 422 norect.x2 = fb->width; 423 norect.y2 = fb->height; 424 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) { 425 num_clips /= 2; 426 inc = 2; /* skip source rects */ 427 } 428 429 qxl_draw_dirty_fb(qdev, fb, qobj, flags, color, 430 clips, num_clips, inc, 0); 431 432 drm_modeset_unlock_all(fb->dev); 433 434 return 0; 435 } 436 437 static const struct drm_framebuffer_funcs qxl_fb_funcs = { 438 .destroy = drm_gem_fb_destroy, 439 .dirty = qxl_framebuffer_surface_dirty, 440 .create_handle = drm_gem_fb_create_handle, 441 }; 442 443 static void qxl_crtc_atomic_enable(struct drm_crtc *crtc, 444 struct drm_crtc_state *old_state) 445 { 446 qxl_crtc_update_monitors_config(crtc, "enable"); 447 } 448 449 static void qxl_crtc_atomic_disable(struct drm_crtc *crtc, 450 struct drm_crtc_state *old_state) 451 { 452 qxl_crtc_update_monitors_config(crtc, "disable"); 453 } 454 455 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = { 456 .atomic_flush = qxl_crtc_atomic_flush, 457 .atomic_enable = qxl_crtc_atomic_enable, 458 .atomic_disable = qxl_crtc_atomic_disable, 459 }; 460 461 static int qxl_primary_atomic_check(struct drm_plane *plane, 462 struct drm_plane_state *state) 463 { 464 struct qxl_device *qdev = plane->dev->dev_private; 465 struct qxl_bo *bo; 466 467 if (!state->crtc || !state->fb) 468 return 0; 469 470 bo = gem_to_qxl_bo(state->fb->obj[0]); 471 472 return qxl_check_framebuffer(qdev, bo); 473 } 474 475 static int qxl_primary_apply_cursor(struct drm_plane *plane) 476 { 477 struct drm_device *dev = plane->dev; 478 struct qxl_device *qdev = dev->dev_private; 479 struct drm_framebuffer *fb = plane->state->fb; 480 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc); 481 struct qxl_cursor_cmd *cmd; 482 struct qxl_release *release; 483 int ret = 0; 484 485 if (!qcrtc->cursor_bo) 486 return 0; 487 488 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), 489 QXL_RELEASE_CURSOR_CMD, 490 &release, NULL); 491 if (ret) 492 return ret; 493 494 ret = qxl_release_list_add(release, qcrtc->cursor_bo); 495 if (ret) 496 goto out_free_release; 497 498 ret = qxl_release_reserve_list(release, false); 499 if (ret) 500 goto out_free_release; 501 502 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release); 503 cmd->type = QXL_CURSOR_SET; 504 cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x; 505 cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y; 506 507 cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0); 508 509 cmd->u.set.visible = 1; 510 qxl_release_unmap(qdev, release, &cmd->release_info); 511 512 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); 513 qxl_release_fence_buffer_objects(release); 514 515 return ret; 516 517 out_free_release: 518 qxl_release_free(qdev, release); 519 return ret; 520 } 521 522 static void qxl_primary_atomic_update(struct drm_plane *plane, 523 struct drm_plane_state *old_state) 524 { 525 struct qxl_device *qdev = plane->dev->dev_private; 526 struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]); 527 struct qxl_bo *primary; 528 struct drm_clip_rect norect = { 529 .x1 = 0, 530 .y1 = 0, 531 .x2 = plane->state->fb->width, 532 .y2 = plane->state->fb->height 533 }; 534 uint32_t dumb_shadow_offset = 0; 535 536 primary = bo->shadow ? bo->shadow : bo; 537 538 if (!primary->is_primary) { 539 if (qdev->primary_bo) 540 qxl_io_destroy_primary(qdev); 541 qxl_io_create_primary(qdev, primary); 542 qxl_primary_apply_cursor(plane); 543 } 544 545 if (bo->is_dumb) 546 dumb_shadow_offset = 547 qdev->dumb_heads[plane->state->crtc->index].x; 548 549 qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1, 550 dumb_shadow_offset); 551 } 552 553 static void qxl_primary_atomic_disable(struct drm_plane *plane, 554 struct drm_plane_state *old_state) 555 { 556 struct qxl_device *qdev = plane->dev->dev_private; 557 558 if (old_state->fb) { 559 struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]); 560 561 if (bo->is_primary) { 562 qxl_io_destroy_primary(qdev); 563 bo->is_primary = false; 564 } 565 } 566 } 567 568 static void qxl_cursor_atomic_update(struct drm_plane *plane, 569 struct drm_plane_state *old_state) 570 { 571 struct drm_device *dev = plane->dev; 572 struct qxl_device *qdev = dev->dev_private; 573 struct drm_framebuffer *fb = plane->state->fb; 574 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc); 575 struct qxl_release *release; 576 struct qxl_cursor_cmd *cmd; 577 struct qxl_cursor *cursor; 578 struct drm_gem_object *obj; 579 struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL; 580 int ret; 581 void *user_ptr; 582 int size = 64*64*4; 583 584 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), 585 QXL_RELEASE_CURSOR_CMD, 586 &release, NULL); 587 if (ret) 588 return; 589 590 if (fb != old_state->fb) { 591 obj = fb->obj[0]; 592 user_bo = gem_to_qxl_bo(obj); 593 594 /* pinning is done in the prepare/cleanup framevbuffer */ 595 ret = qxl_bo_kmap(user_bo, &user_ptr); 596 if (ret) 597 goto out_free_release; 598 599 ret = qxl_alloc_bo_reserved(qdev, release, 600 sizeof(struct qxl_cursor) + size, 601 &cursor_bo); 602 if (ret) 603 goto out_kunmap; 604 605 ret = qxl_bo_pin(cursor_bo); 606 if (ret) 607 goto out_free_bo; 608 609 ret = qxl_release_reserve_list(release, true); 610 if (ret) 611 goto out_unpin; 612 613 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor); 614 if (ret) 615 goto out_backoff; 616 617 cursor->header.unique = 0; 618 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA; 619 cursor->header.width = 64; 620 cursor->header.height = 64; 621 cursor->header.hot_spot_x = fb->hot_x; 622 cursor->header.hot_spot_y = fb->hot_y; 623 cursor->data_size = size; 624 cursor->chunk.next_chunk = 0; 625 cursor->chunk.prev_chunk = 0; 626 cursor->chunk.data_size = size; 627 memcpy(cursor->chunk.data, user_ptr, size); 628 qxl_bo_kunmap(cursor_bo); 629 qxl_bo_kunmap(user_bo); 630 631 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release); 632 cmd->u.set.visible = 1; 633 cmd->u.set.shape = qxl_bo_physical_address(qdev, 634 cursor_bo, 0); 635 cmd->type = QXL_CURSOR_SET; 636 637 old_cursor_bo = qcrtc->cursor_bo; 638 qcrtc->cursor_bo = cursor_bo; 639 cursor_bo = NULL; 640 } else { 641 642 ret = qxl_release_reserve_list(release, true); 643 if (ret) 644 goto out_free_release; 645 646 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release); 647 cmd->type = QXL_CURSOR_MOVE; 648 } 649 650 cmd->u.position.x = plane->state->crtc_x + fb->hot_x; 651 cmd->u.position.y = plane->state->crtc_y + fb->hot_y; 652 653 qxl_release_unmap(qdev, release, &cmd->release_info); 654 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); 655 qxl_release_fence_buffer_objects(release); 656 657 if (old_cursor_bo != NULL) 658 qxl_bo_unpin(old_cursor_bo); 659 qxl_bo_unref(&old_cursor_bo); 660 qxl_bo_unref(&cursor_bo); 661 662 return; 663 664 out_backoff: 665 qxl_release_backoff_reserve_list(release); 666 out_unpin: 667 qxl_bo_unpin(cursor_bo); 668 out_free_bo: 669 qxl_bo_unref(&cursor_bo); 670 out_kunmap: 671 qxl_bo_kunmap(user_bo); 672 out_free_release: 673 qxl_release_free(qdev, release); 674 return; 675 676 } 677 678 static void qxl_cursor_atomic_disable(struct drm_plane *plane, 679 struct drm_plane_state *old_state) 680 { 681 struct qxl_device *qdev = plane->dev->dev_private; 682 struct qxl_release *release; 683 struct qxl_cursor_cmd *cmd; 684 int ret; 685 686 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), 687 QXL_RELEASE_CURSOR_CMD, 688 &release, NULL); 689 if (ret) 690 return; 691 692 ret = qxl_release_reserve_list(release, true); 693 if (ret) { 694 qxl_release_free(qdev, release); 695 return; 696 } 697 698 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release); 699 cmd->type = QXL_CURSOR_HIDE; 700 qxl_release_unmap(qdev, release, &cmd->release_info); 701 702 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); 703 qxl_release_fence_buffer_objects(release); 704 } 705 706 static void qxl_update_dumb_head(struct qxl_device *qdev, 707 int index, struct qxl_bo *bo) 708 { 709 uint32_t width, height; 710 711 if (index >= qdev->monitors_config->max_allowed) 712 return; 713 714 if (bo && bo->is_dumb) { 715 width = bo->surf.width; 716 height = bo->surf.height; 717 } else { 718 width = 0; 719 height = 0; 720 } 721 722 if (qdev->dumb_heads[index].width == width && 723 qdev->dumb_heads[index].height == height) 724 return; 725 726 DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index, 727 qdev->dumb_heads[index].width, 728 qdev->dumb_heads[index].height, 729 width, height); 730 qdev->dumb_heads[index].width = width; 731 qdev->dumb_heads[index].height = height; 732 } 733 734 static void qxl_calc_dumb_shadow(struct qxl_device *qdev, 735 struct qxl_surface *surf) 736 { 737 struct qxl_head *head; 738 int i; 739 740 memset(surf, 0, sizeof(*surf)); 741 for (i = 0; i < qdev->monitors_config->max_allowed; i++) { 742 head = qdev->dumb_heads + i; 743 head->x = surf->width; 744 surf->width += head->width; 745 if (surf->height < head->height) 746 surf->height = head->height; 747 } 748 if (surf->width < 64) 749 surf->width = 64; 750 if (surf->height < 64) 751 surf->height = 64; 752 surf->format = SPICE_SURFACE_FMT_32_xRGB; 753 surf->stride = surf->width * 4; 754 755 if (!qdev->dumb_shadow_bo || 756 qdev->dumb_shadow_bo->surf.width != surf->width || 757 qdev->dumb_shadow_bo->surf.height != surf->height) 758 DRM_DEBUG("%dx%d\n", surf->width, surf->height); 759 } 760 761 static int qxl_plane_prepare_fb(struct drm_plane *plane, 762 struct drm_plane_state *new_state) 763 { 764 struct qxl_device *qdev = plane->dev->dev_private; 765 struct drm_gem_object *obj; 766 struct qxl_bo *user_bo; 767 struct qxl_surface surf; 768 int ret; 769 770 if (!new_state->fb) 771 return 0; 772 773 obj = new_state->fb->obj[0]; 774 user_bo = gem_to_qxl_bo(obj); 775 776 if (plane->type == DRM_PLANE_TYPE_PRIMARY && 777 user_bo->is_dumb) { 778 qxl_update_dumb_head(qdev, new_state->crtc->index, 779 user_bo); 780 qxl_calc_dumb_shadow(qdev, &surf); 781 if (!qdev->dumb_shadow_bo || 782 qdev->dumb_shadow_bo->surf.width != surf.width || 783 qdev->dumb_shadow_bo->surf.height != surf.height) { 784 if (qdev->dumb_shadow_bo) { 785 drm_gem_object_put_unlocked 786 (&qdev->dumb_shadow_bo->tbo.base); 787 qdev->dumb_shadow_bo = NULL; 788 } 789 qxl_bo_create(qdev, surf.height * surf.stride, 790 true, true, QXL_GEM_DOMAIN_SURFACE, &surf, 791 &qdev->dumb_shadow_bo); 792 } 793 if (user_bo->shadow != qdev->dumb_shadow_bo) { 794 if (user_bo->shadow) { 795 drm_gem_object_put_unlocked 796 (&user_bo->shadow->tbo.base); 797 user_bo->shadow = NULL; 798 } 799 drm_gem_object_get(&qdev->dumb_shadow_bo->tbo.base); 800 user_bo->shadow = qdev->dumb_shadow_bo; 801 } 802 } 803 804 ret = qxl_bo_pin(user_bo); 805 if (ret) 806 return ret; 807 808 return 0; 809 } 810 811 static void qxl_plane_cleanup_fb(struct drm_plane *plane, 812 struct drm_plane_state *old_state) 813 { 814 struct drm_gem_object *obj; 815 struct qxl_bo *user_bo; 816 817 if (!old_state->fb) { 818 /* 819 * we never executed prepare_fb, so there's nothing to 820 * unpin. 821 */ 822 return; 823 } 824 825 obj = old_state->fb->obj[0]; 826 user_bo = gem_to_qxl_bo(obj); 827 qxl_bo_unpin(user_bo); 828 829 if (old_state->fb != plane->state->fb && user_bo->shadow) { 830 drm_gem_object_put_unlocked(&user_bo->shadow->tbo.base); 831 user_bo->shadow = NULL; 832 } 833 } 834 835 static const uint32_t qxl_cursor_plane_formats[] = { 836 DRM_FORMAT_ARGB8888, 837 }; 838 839 static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = { 840 .atomic_update = qxl_cursor_atomic_update, 841 .atomic_disable = qxl_cursor_atomic_disable, 842 .prepare_fb = qxl_plane_prepare_fb, 843 .cleanup_fb = qxl_plane_cleanup_fb, 844 }; 845 846 static const struct drm_plane_funcs qxl_cursor_plane_funcs = { 847 .update_plane = drm_atomic_helper_update_plane, 848 .disable_plane = drm_atomic_helper_disable_plane, 849 .destroy = drm_primary_helper_destroy, 850 .reset = drm_atomic_helper_plane_reset, 851 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 852 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 853 }; 854 855 static const uint32_t qxl_primary_plane_formats[] = { 856 DRM_FORMAT_XRGB8888, 857 DRM_FORMAT_ARGB8888, 858 }; 859 860 static const struct drm_plane_helper_funcs primary_helper_funcs = { 861 .atomic_check = qxl_primary_atomic_check, 862 .atomic_update = qxl_primary_atomic_update, 863 .atomic_disable = qxl_primary_atomic_disable, 864 .prepare_fb = qxl_plane_prepare_fb, 865 .cleanup_fb = qxl_plane_cleanup_fb, 866 }; 867 868 static const struct drm_plane_funcs qxl_primary_plane_funcs = { 869 .update_plane = drm_atomic_helper_update_plane, 870 .disable_plane = drm_atomic_helper_disable_plane, 871 .destroy = drm_primary_helper_destroy, 872 .reset = drm_atomic_helper_plane_reset, 873 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 874 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 875 }; 876 877 static struct drm_plane *qxl_create_plane(struct qxl_device *qdev, 878 unsigned int possible_crtcs, 879 enum drm_plane_type type) 880 { 881 const struct drm_plane_helper_funcs *helper_funcs = NULL; 882 struct drm_plane *plane; 883 const struct drm_plane_funcs *funcs; 884 const uint32_t *formats; 885 int num_formats; 886 int err; 887 888 if (type == DRM_PLANE_TYPE_PRIMARY) { 889 funcs = &qxl_primary_plane_funcs; 890 formats = qxl_primary_plane_formats; 891 num_formats = ARRAY_SIZE(qxl_primary_plane_formats); 892 helper_funcs = &primary_helper_funcs; 893 } else if (type == DRM_PLANE_TYPE_CURSOR) { 894 funcs = &qxl_cursor_plane_funcs; 895 formats = qxl_cursor_plane_formats; 896 helper_funcs = &qxl_cursor_helper_funcs; 897 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats); 898 } else { 899 return ERR_PTR(-EINVAL); 900 } 901 902 plane = kzalloc(sizeof(*plane), GFP_KERNEL); 903 if (!plane) 904 return ERR_PTR(-ENOMEM); 905 906 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs, 907 funcs, formats, num_formats, 908 NULL, type, NULL); 909 if (err) 910 goto free_plane; 911 912 drm_plane_helper_add(plane, helper_funcs); 913 914 return plane; 915 916 free_plane: 917 kfree(plane); 918 return ERR_PTR(-EINVAL); 919 } 920 921 static int qdev_crtc_init(struct drm_device *dev, int crtc_id) 922 { 923 struct qxl_crtc *qxl_crtc; 924 struct drm_plane *primary, *cursor; 925 struct qxl_device *qdev = dev->dev_private; 926 int r; 927 928 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL); 929 if (!qxl_crtc) 930 return -ENOMEM; 931 932 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY); 933 if (IS_ERR(primary)) { 934 r = -ENOMEM; 935 goto free_mem; 936 } 937 938 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR); 939 if (IS_ERR(cursor)) { 940 r = -ENOMEM; 941 goto clean_primary; 942 } 943 944 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor, 945 &qxl_crtc_funcs, NULL); 946 if (r) 947 goto clean_cursor; 948 949 qxl_crtc->index = crtc_id; 950 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs); 951 return 0; 952 953 clean_cursor: 954 drm_plane_cleanup(cursor); 955 kfree(cursor); 956 clean_primary: 957 drm_plane_cleanup(primary); 958 kfree(primary); 959 free_mem: 960 kfree(qxl_crtc); 961 return r; 962 } 963 964 static int qxl_conn_get_modes(struct drm_connector *connector) 965 { 966 struct drm_device *dev = connector->dev; 967 struct qxl_device *qdev = dev->dev_private; 968 struct qxl_output *output = drm_connector_to_qxl_output(connector); 969 unsigned int pwidth = 1024; 970 unsigned int pheight = 768; 971 int ret = 0; 972 973 if (qdev->client_monitors_config) { 974 struct qxl_head *head; 975 head = &qdev->client_monitors_config->heads[output->index]; 976 if (head->width) 977 pwidth = head->width; 978 if (head->height) 979 pheight = head->height; 980 } 981 982 ret += drm_add_modes_noedid(connector, 8192, 8192); 983 ret += qxl_add_extra_modes(connector); 984 ret += qxl_add_monitors_config_modes(connector); 985 drm_set_preferred_mode(connector, pwidth, pheight); 986 return ret; 987 } 988 989 static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector, 990 struct drm_display_mode *mode) 991 { 992 struct drm_device *ddev = connector->dev; 993 struct qxl_device *qdev = ddev->dev_private; 994 995 if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0) 996 return MODE_BAD; 997 998 return MODE_OK; 999 } 1000 1001 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector) 1002 { 1003 struct qxl_output *qxl_output = 1004 drm_connector_to_qxl_output(connector); 1005 1006 DRM_DEBUG("\n"); 1007 return &qxl_output->enc; 1008 } 1009 1010 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = { 1011 }; 1012 1013 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = { 1014 .get_modes = qxl_conn_get_modes, 1015 .mode_valid = qxl_conn_mode_valid, 1016 .best_encoder = qxl_best_encoder, 1017 }; 1018 1019 static enum drm_connector_status qxl_conn_detect( 1020 struct drm_connector *connector, 1021 bool force) 1022 { 1023 struct qxl_output *output = 1024 drm_connector_to_qxl_output(connector); 1025 struct drm_device *ddev = connector->dev; 1026 struct qxl_device *qdev = ddev->dev_private; 1027 bool connected = false; 1028 1029 /* The first monitor is always connected */ 1030 if (!qdev->client_monitors_config) { 1031 if (output->index == 0) 1032 connected = true; 1033 } else 1034 connected = qdev->client_monitors_config->count > output->index && 1035 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]); 1036 1037 DRM_DEBUG("#%d connected: %d\n", output->index, connected); 1038 1039 return connected ? connector_status_connected 1040 : connector_status_disconnected; 1041 } 1042 1043 static void qxl_conn_destroy(struct drm_connector *connector) 1044 { 1045 struct qxl_output *qxl_output = 1046 drm_connector_to_qxl_output(connector); 1047 1048 drm_connector_unregister(connector); 1049 drm_connector_cleanup(connector); 1050 kfree(qxl_output); 1051 } 1052 1053 static const struct drm_connector_funcs qxl_connector_funcs = { 1054 .detect = qxl_conn_detect, 1055 .fill_modes = drm_helper_probe_single_connector_modes, 1056 .destroy = qxl_conn_destroy, 1057 .reset = drm_atomic_helper_connector_reset, 1058 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 1059 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 1060 }; 1061 1062 static void qxl_enc_destroy(struct drm_encoder *encoder) 1063 { 1064 drm_encoder_cleanup(encoder); 1065 } 1066 1067 static const struct drm_encoder_funcs qxl_enc_funcs = { 1068 .destroy = qxl_enc_destroy, 1069 }; 1070 1071 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev) 1072 { 1073 if (qdev->hotplug_mode_update_property) 1074 return 0; 1075 1076 qdev->hotplug_mode_update_property = 1077 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE, 1078 "hotplug_mode_update", 0, 1); 1079 1080 return 0; 1081 } 1082 1083 static int qdev_output_init(struct drm_device *dev, int num_output) 1084 { 1085 struct qxl_device *qdev = dev->dev_private; 1086 struct qxl_output *qxl_output; 1087 struct drm_connector *connector; 1088 struct drm_encoder *encoder; 1089 1090 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL); 1091 if (!qxl_output) 1092 return -ENOMEM; 1093 1094 qxl_output->index = num_output; 1095 1096 connector = &qxl_output->base; 1097 encoder = &qxl_output->enc; 1098 drm_connector_init(dev, &qxl_output->base, 1099 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL); 1100 1101 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs, 1102 DRM_MODE_ENCODER_VIRTUAL, NULL); 1103 1104 /* we get HPD via client monitors config */ 1105 connector->polled = DRM_CONNECTOR_POLL_HPD; 1106 encoder->possible_crtcs = 1 << num_output; 1107 drm_connector_attach_encoder(&qxl_output->base, 1108 &qxl_output->enc); 1109 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs); 1110 drm_connector_helper_add(connector, &qxl_connector_helper_funcs); 1111 1112 drm_object_attach_property(&connector->base, 1113 qdev->hotplug_mode_update_property, 0); 1114 drm_object_attach_property(&connector->base, 1115 dev->mode_config.suggested_x_property, 0); 1116 drm_object_attach_property(&connector->base, 1117 dev->mode_config.suggested_y_property, 0); 1118 return 0; 1119 } 1120 1121 static struct drm_framebuffer * 1122 qxl_user_framebuffer_create(struct drm_device *dev, 1123 struct drm_file *file_priv, 1124 const struct drm_mode_fb_cmd2 *mode_cmd) 1125 { 1126 return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd, 1127 &qxl_fb_funcs); 1128 } 1129 1130 static const struct drm_mode_config_funcs qxl_mode_funcs = { 1131 .fb_create = qxl_user_framebuffer_create, 1132 .atomic_check = drm_atomic_helper_check, 1133 .atomic_commit = drm_atomic_helper_commit, 1134 }; 1135 1136 int qxl_create_monitors_object(struct qxl_device *qdev) 1137 { 1138 int ret; 1139 struct drm_gem_object *gobj; 1140 int monitors_config_size = sizeof(struct qxl_monitors_config) + 1141 qxl_num_crtc * sizeof(struct qxl_head); 1142 1143 ret = qxl_gem_object_create(qdev, monitors_config_size, 0, 1144 QXL_GEM_DOMAIN_VRAM, 1145 false, false, NULL, &gobj); 1146 if (ret) { 1147 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret); 1148 return -ENOMEM; 1149 } 1150 qdev->monitors_config_bo = gem_to_qxl_bo(gobj); 1151 1152 ret = qxl_bo_pin(qdev->monitors_config_bo); 1153 if (ret) 1154 return ret; 1155 1156 qxl_bo_kmap(qdev->monitors_config_bo, NULL); 1157 1158 qdev->monitors_config = qdev->monitors_config_bo->kptr; 1159 qdev->ram_header->monitors_config = 1160 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0); 1161 1162 memset(qdev->monitors_config, 0, monitors_config_size); 1163 qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]), 1164 GFP_KERNEL); 1165 if (!qdev->dumb_heads) { 1166 qxl_destroy_monitors_object(qdev); 1167 return -ENOMEM; 1168 } 1169 return 0; 1170 } 1171 1172 int qxl_destroy_monitors_object(struct qxl_device *qdev) 1173 { 1174 int ret; 1175 1176 qdev->monitors_config = NULL; 1177 qdev->ram_header->monitors_config = 0; 1178 1179 qxl_bo_kunmap(qdev->monitors_config_bo); 1180 ret = qxl_bo_unpin(qdev->monitors_config_bo); 1181 if (ret) 1182 return ret; 1183 1184 qxl_bo_unref(&qdev->monitors_config_bo); 1185 return 0; 1186 } 1187 1188 int qxl_modeset_init(struct qxl_device *qdev) 1189 { 1190 int i; 1191 int ret; 1192 1193 drm_mode_config_init(&qdev->ddev); 1194 1195 ret = qxl_create_monitors_object(qdev); 1196 if (ret) 1197 return ret; 1198 1199 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs; 1200 1201 /* modes will be validated against the framebuffer size */ 1202 qdev->ddev.mode_config.min_width = 0; 1203 qdev->ddev.mode_config.min_height = 0; 1204 qdev->ddev.mode_config.max_width = 8192; 1205 qdev->ddev.mode_config.max_height = 8192; 1206 1207 qdev->ddev.mode_config.fb_base = qdev->vram_base; 1208 1209 drm_mode_create_suggested_offset_properties(&qdev->ddev); 1210 qxl_mode_create_hotplug_mode_update_property(qdev); 1211 1212 for (i = 0 ; i < qxl_num_crtc; ++i) { 1213 qdev_crtc_init(&qdev->ddev, i); 1214 qdev_output_init(&qdev->ddev, i); 1215 } 1216 1217 qxl_display_read_client_monitors_config(qdev); 1218 1219 drm_mode_config_reset(&qdev->ddev); 1220 return 0; 1221 } 1222 1223 void qxl_modeset_fini(struct qxl_device *qdev) 1224 { 1225 qxl_destroy_monitors_object(qdev); 1226 drm_mode_config_cleanup(&qdev->ddev); 1227 } 1228