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