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