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