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