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