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; 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 qxl_bo_unref(&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 qxl_bo_unref(&cursor_bo); 701 702 return; 703 704 out_backoff: 705 qxl_release_backoff_reserve_list(release); 706 out_free_bo: 707 qxl_bo_unref(&cursor_bo); 708 out_kunmap: 709 qxl_bo_kunmap(user_bo); 710 out_free_release: 711 qxl_release_free(qdev, release); 712 return; 713 714 } 715 716 static void qxl_cursor_atomic_disable(struct drm_plane *plane, 717 struct drm_plane_state *old_state) 718 { 719 struct qxl_device *qdev = plane->dev->dev_private; 720 struct qxl_release *release; 721 struct qxl_cursor_cmd *cmd; 722 int ret; 723 724 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), 725 QXL_RELEASE_CURSOR_CMD, 726 &release, NULL); 727 if (ret) 728 return; 729 730 ret = qxl_release_reserve_list(release, true); 731 if (ret) { 732 qxl_release_free(qdev, release); 733 return; 734 } 735 736 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release); 737 cmd->type = QXL_CURSOR_HIDE; 738 qxl_release_unmap(qdev, release, &cmd->release_info); 739 740 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); 741 qxl_release_fence_buffer_objects(release); 742 } 743 744 static int qxl_plane_prepare_fb(struct drm_plane *plane, 745 struct drm_plane_state *new_state) 746 { 747 struct qxl_device *qdev = plane->dev->dev_private; 748 struct drm_gem_object *obj; 749 struct qxl_bo *user_bo, *old_bo = NULL; 750 int ret; 751 752 if (!new_state->fb) 753 return 0; 754 755 obj = to_qxl_framebuffer(new_state->fb)->obj; 756 user_bo = gem_to_qxl_bo(obj); 757 758 if (plane->type == DRM_PLANE_TYPE_PRIMARY && 759 user_bo->is_dumb && !user_bo->shadow) { 760 if (plane->state->fb) { 761 obj = to_qxl_framebuffer(plane->state->fb)->obj; 762 old_bo = gem_to_qxl_bo(obj); 763 } 764 if (old_bo && old_bo->shadow && 765 user_bo->gem_base.size == old_bo->gem_base.size && 766 plane->state->crtc == new_state->crtc && 767 plane->state->crtc_w == new_state->crtc_w && 768 plane->state->crtc_h == new_state->crtc_h && 769 plane->state->src_x == new_state->src_x && 770 plane->state->src_y == new_state->src_y && 771 plane->state->src_w == new_state->src_w && 772 plane->state->src_h == new_state->src_h && 773 plane->state->rotation == new_state->rotation && 774 plane->state->zpos == new_state->zpos) { 775 drm_gem_object_get(&old_bo->shadow->gem_base); 776 user_bo->shadow = old_bo->shadow; 777 } else { 778 qxl_bo_create(qdev, user_bo->gem_base.size, 779 true, true, QXL_GEM_DOMAIN_VRAM, NULL, 780 &user_bo->shadow); 781 } 782 } 783 784 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL); 785 if (ret) 786 return ret; 787 788 return 0; 789 } 790 791 static void qxl_plane_cleanup_fb(struct drm_plane *plane, 792 struct drm_plane_state *old_state) 793 { 794 struct drm_gem_object *obj; 795 struct qxl_bo *user_bo; 796 797 if (!old_state->fb) { 798 /* 799 * we never executed prepare_fb, so there's nothing to 800 * unpin. 801 */ 802 return; 803 } 804 805 obj = to_qxl_framebuffer(old_state->fb)->obj; 806 user_bo = gem_to_qxl_bo(obj); 807 qxl_bo_unpin(user_bo); 808 809 if (user_bo->shadow && !user_bo->is_primary) { 810 drm_gem_object_put_unlocked(&user_bo->shadow->gem_base); 811 user_bo->shadow = NULL; 812 } 813 } 814 815 static const uint32_t qxl_cursor_plane_formats[] = { 816 DRM_FORMAT_ARGB8888, 817 }; 818 819 static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = { 820 .atomic_update = qxl_cursor_atomic_update, 821 .atomic_disable = qxl_cursor_atomic_disable, 822 .prepare_fb = qxl_plane_prepare_fb, 823 .cleanup_fb = qxl_plane_cleanup_fb, 824 }; 825 826 static const struct drm_plane_funcs qxl_cursor_plane_funcs = { 827 .update_plane = drm_atomic_helper_update_plane, 828 .disable_plane = drm_atomic_helper_disable_plane, 829 .destroy = drm_primary_helper_destroy, 830 .reset = drm_atomic_helper_plane_reset, 831 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 832 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 833 }; 834 835 static const uint32_t qxl_primary_plane_formats[] = { 836 DRM_FORMAT_XRGB8888, 837 DRM_FORMAT_ARGB8888, 838 }; 839 840 static const struct drm_plane_helper_funcs primary_helper_funcs = { 841 .atomic_check = qxl_primary_atomic_check, 842 .atomic_update = qxl_primary_atomic_update, 843 .atomic_disable = qxl_primary_atomic_disable, 844 .prepare_fb = qxl_plane_prepare_fb, 845 .cleanup_fb = qxl_plane_cleanup_fb, 846 }; 847 848 static const struct drm_plane_funcs qxl_primary_plane_funcs = { 849 .update_plane = drm_atomic_helper_update_plane, 850 .disable_plane = drm_atomic_helper_disable_plane, 851 .destroy = drm_primary_helper_destroy, 852 .reset = drm_atomic_helper_plane_reset, 853 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 854 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 855 }; 856 857 static struct drm_plane *qxl_create_plane(struct qxl_device *qdev, 858 unsigned int possible_crtcs, 859 enum drm_plane_type type) 860 { 861 const struct drm_plane_helper_funcs *helper_funcs = NULL; 862 struct drm_plane *plane; 863 const struct drm_plane_funcs *funcs; 864 const uint32_t *formats; 865 int num_formats; 866 int err; 867 868 if (type == DRM_PLANE_TYPE_PRIMARY) { 869 funcs = &qxl_primary_plane_funcs; 870 formats = qxl_primary_plane_formats; 871 num_formats = ARRAY_SIZE(qxl_primary_plane_formats); 872 helper_funcs = &primary_helper_funcs; 873 } else if (type == DRM_PLANE_TYPE_CURSOR) { 874 funcs = &qxl_cursor_plane_funcs; 875 formats = qxl_cursor_plane_formats; 876 helper_funcs = &qxl_cursor_helper_funcs; 877 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats); 878 } else { 879 return ERR_PTR(-EINVAL); 880 } 881 882 plane = kzalloc(sizeof(*plane), GFP_KERNEL); 883 if (!plane) 884 return ERR_PTR(-ENOMEM); 885 886 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs, 887 funcs, formats, num_formats, 888 NULL, type, NULL); 889 if (err) 890 goto free_plane; 891 892 drm_plane_helper_add(plane, helper_funcs); 893 894 return plane; 895 896 free_plane: 897 kfree(plane); 898 return ERR_PTR(-EINVAL); 899 } 900 901 static int qdev_crtc_init(struct drm_device *dev, int crtc_id) 902 { 903 struct qxl_crtc *qxl_crtc; 904 struct drm_plane *primary, *cursor; 905 struct qxl_device *qdev = dev->dev_private; 906 int r; 907 908 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL); 909 if (!qxl_crtc) 910 return -ENOMEM; 911 912 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY); 913 if (IS_ERR(primary)) { 914 r = -ENOMEM; 915 goto free_mem; 916 } 917 918 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR); 919 if (IS_ERR(cursor)) { 920 r = -ENOMEM; 921 goto clean_primary; 922 } 923 924 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor, 925 &qxl_crtc_funcs, NULL); 926 if (r) 927 goto clean_cursor; 928 929 qxl_crtc->index = crtc_id; 930 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs); 931 return 0; 932 933 clean_cursor: 934 drm_plane_cleanup(cursor); 935 kfree(cursor); 936 clean_primary: 937 drm_plane_cleanup(primary); 938 kfree(primary); 939 free_mem: 940 kfree(qxl_crtc); 941 return r; 942 } 943 944 static int qxl_conn_get_modes(struct drm_connector *connector) 945 { 946 unsigned pwidth = 1024; 947 unsigned pheight = 768; 948 int ret = 0; 949 950 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight); 951 if (ret < 0) 952 return ret; 953 ret += qxl_add_common_modes(connector, pwidth, pheight); 954 return ret; 955 } 956 957 static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector, 958 struct drm_display_mode *mode) 959 { 960 struct drm_device *ddev = connector->dev; 961 struct qxl_device *qdev = ddev->dev_private; 962 int i; 963 964 /* TODO: is this called for user defined modes? (xrandr --add-mode) 965 * TODO: check that the mode fits in the framebuffer */ 966 967 if(qdev->monitors_config_width == mode->hdisplay && 968 qdev->monitors_config_height == mode->vdisplay) 969 return MODE_OK; 970 971 for (i = 0; i < ARRAY_SIZE(common_modes); i++) { 972 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay) 973 return MODE_OK; 974 } 975 return MODE_BAD; 976 } 977 978 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector) 979 { 980 struct qxl_output *qxl_output = 981 drm_connector_to_qxl_output(connector); 982 983 DRM_DEBUG("\n"); 984 return &qxl_output->enc; 985 } 986 987 988 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = { 989 }; 990 991 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = { 992 .get_modes = qxl_conn_get_modes, 993 .mode_valid = qxl_conn_mode_valid, 994 .best_encoder = qxl_best_encoder, 995 }; 996 997 static enum drm_connector_status qxl_conn_detect( 998 struct drm_connector *connector, 999 bool force) 1000 { 1001 struct qxl_output *output = 1002 drm_connector_to_qxl_output(connector); 1003 struct drm_device *ddev = connector->dev; 1004 struct qxl_device *qdev = ddev->dev_private; 1005 bool connected = false; 1006 1007 /* The first monitor is always connected */ 1008 if (!qdev->client_monitors_config) { 1009 if (output->index == 0) 1010 connected = true; 1011 } else 1012 connected = qdev->client_monitors_config->count > output->index && 1013 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]); 1014 1015 DRM_DEBUG("#%d connected: %d\n", output->index, connected); 1016 1017 return connected ? connector_status_connected 1018 : connector_status_disconnected; 1019 } 1020 1021 static void qxl_conn_destroy(struct drm_connector *connector) 1022 { 1023 struct qxl_output *qxl_output = 1024 drm_connector_to_qxl_output(connector); 1025 1026 drm_connector_unregister(connector); 1027 drm_connector_cleanup(connector); 1028 kfree(qxl_output); 1029 } 1030 1031 static const struct drm_connector_funcs qxl_connector_funcs = { 1032 .dpms = drm_helper_connector_dpms, 1033 .detect = qxl_conn_detect, 1034 .fill_modes = drm_helper_probe_single_connector_modes, 1035 .destroy = qxl_conn_destroy, 1036 .reset = drm_atomic_helper_connector_reset, 1037 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 1038 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 1039 }; 1040 1041 static void qxl_enc_destroy(struct drm_encoder *encoder) 1042 { 1043 drm_encoder_cleanup(encoder); 1044 } 1045 1046 static const struct drm_encoder_funcs qxl_enc_funcs = { 1047 .destroy = qxl_enc_destroy, 1048 }; 1049 1050 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev) 1051 { 1052 if (qdev->hotplug_mode_update_property) 1053 return 0; 1054 1055 qdev->hotplug_mode_update_property = 1056 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE, 1057 "hotplug_mode_update", 0, 1); 1058 1059 return 0; 1060 } 1061 1062 static int qdev_output_init(struct drm_device *dev, int num_output) 1063 { 1064 struct qxl_device *qdev = dev->dev_private; 1065 struct qxl_output *qxl_output; 1066 struct drm_connector *connector; 1067 struct drm_encoder *encoder; 1068 1069 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL); 1070 if (!qxl_output) 1071 return -ENOMEM; 1072 1073 qxl_output->index = num_output; 1074 1075 connector = &qxl_output->base; 1076 encoder = &qxl_output->enc; 1077 drm_connector_init(dev, &qxl_output->base, 1078 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL); 1079 1080 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs, 1081 DRM_MODE_ENCODER_VIRTUAL, NULL); 1082 1083 /* we get HPD via client monitors config */ 1084 connector->polled = DRM_CONNECTOR_POLL_HPD; 1085 encoder->possible_crtcs = 1 << num_output; 1086 drm_mode_connector_attach_encoder(&qxl_output->base, 1087 &qxl_output->enc); 1088 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs); 1089 drm_connector_helper_add(connector, &qxl_connector_helper_funcs); 1090 1091 drm_object_attach_property(&connector->base, 1092 qdev->hotplug_mode_update_property, 0); 1093 drm_object_attach_property(&connector->base, 1094 dev->mode_config.suggested_x_property, 0); 1095 drm_object_attach_property(&connector->base, 1096 dev->mode_config.suggested_y_property, 0); 1097 return 0; 1098 } 1099 1100 static struct drm_framebuffer * 1101 qxl_user_framebuffer_create(struct drm_device *dev, 1102 struct drm_file *file_priv, 1103 const struct drm_mode_fb_cmd2 *mode_cmd) 1104 { 1105 struct drm_gem_object *obj; 1106 struct qxl_framebuffer *qxl_fb; 1107 int ret; 1108 1109 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]); 1110 if (!obj) 1111 return NULL; 1112 1113 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL); 1114 if (qxl_fb == NULL) 1115 return NULL; 1116 1117 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs); 1118 if (ret) { 1119 kfree(qxl_fb); 1120 drm_gem_object_put_unlocked(obj); 1121 return NULL; 1122 } 1123 1124 return &qxl_fb->base; 1125 } 1126 1127 static const struct drm_mode_config_funcs qxl_mode_funcs = { 1128 .fb_create = qxl_user_framebuffer_create, 1129 .atomic_check = drm_atomic_helper_check, 1130 .atomic_commit = drm_atomic_helper_commit, 1131 }; 1132 1133 int qxl_create_monitors_object(struct qxl_device *qdev) 1134 { 1135 int ret; 1136 struct drm_gem_object *gobj; 1137 int max_allowed = qxl_num_crtc; 1138 int monitors_config_size = sizeof(struct qxl_monitors_config) + 1139 max_allowed * sizeof(struct qxl_head); 1140 1141 ret = qxl_gem_object_create(qdev, monitors_config_size, 0, 1142 QXL_GEM_DOMAIN_VRAM, 1143 false, false, NULL, &gobj); 1144 if (ret) { 1145 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret); 1146 return -ENOMEM; 1147 } 1148 qdev->monitors_config_bo = gem_to_qxl_bo(gobj); 1149 1150 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL); 1151 if (ret) 1152 return ret; 1153 1154 qxl_bo_kmap(qdev->monitors_config_bo, NULL); 1155 1156 qdev->monitors_config = qdev->monitors_config_bo->kptr; 1157 qdev->ram_header->monitors_config = 1158 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0); 1159 1160 memset(qdev->monitors_config, 0, monitors_config_size); 1161 qdev->monitors_config->max_allowed = max_allowed; 1162 return 0; 1163 } 1164 1165 int qxl_destroy_monitors_object(struct qxl_device *qdev) 1166 { 1167 int ret; 1168 1169 qdev->monitors_config = NULL; 1170 qdev->ram_header->monitors_config = 0; 1171 1172 qxl_bo_kunmap(qdev->monitors_config_bo); 1173 ret = qxl_bo_unpin(qdev->monitors_config_bo); 1174 if (ret) 1175 return ret; 1176 1177 qxl_bo_unref(&qdev->monitors_config_bo); 1178 return 0; 1179 } 1180 1181 int qxl_modeset_init(struct qxl_device *qdev) 1182 { 1183 int i; 1184 int ret; 1185 1186 drm_mode_config_init(&qdev->ddev); 1187 1188 ret = qxl_create_monitors_object(qdev); 1189 if (ret) 1190 return ret; 1191 1192 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs; 1193 1194 /* modes will be validated against the framebuffer size */ 1195 qdev->ddev.mode_config.min_width = 0; 1196 qdev->ddev.mode_config.min_height = 0; 1197 qdev->ddev.mode_config.max_width = 8192; 1198 qdev->ddev.mode_config.max_height = 8192; 1199 1200 qdev->ddev.mode_config.fb_base = qdev->vram_base; 1201 1202 drm_mode_create_suggested_offset_properties(&qdev->ddev); 1203 qxl_mode_create_hotplug_mode_update_property(qdev); 1204 1205 for (i = 0 ; i < qxl_num_crtc; ++i) { 1206 qdev_crtc_init(&qdev->ddev, i); 1207 qdev_output_init(&qdev->ddev, i); 1208 } 1209 1210 qxl_display_read_client_monitors_config(qdev); 1211 qdev->mode_info.mode_config_initialized = true; 1212 1213 drm_mode_config_reset(&qdev->ddev); 1214 1215 /* primary surface must be created by this point, to allow 1216 * issuing command queue commands and having them read by 1217 * spice server. */ 1218 qxl_fbdev_init(qdev); 1219 return 0; 1220 } 1221 1222 void qxl_modeset_fini(struct qxl_device *qdev) 1223 { 1224 qxl_fbdev_fini(qdev); 1225 1226 qxl_destroy_monitors_object(qdev); 1227 if (qdev->mode_info.mode_config_initialized) { 1228 drm_mode_config_cleanup(&qdev->ddev); 1229 qdev->mode_info.mode_config_initialized = false; 1230 } 1231 } 1232