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