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 void qxl_crtc_dpms(struct drm_crtc *crtc, int mode) 382 { 383 } 384 385 static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc, 386 const struct drm_display_mode *mode, 387 struct drm_display_mode *adjusted_mode) 388 { 389 struct drm_device *dev = crtc->dev; 390 struct qxl_device *qdev = dev->dev_private; 391 392 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n", 393 __func__, 394 mode->hdisplay, mode->vdisplay, 395 adjusted_mode->hdisplay, 396 adjusted_mode->vdisplay); 397 return true; 398 } 399 400 static void 401 qxl_send_monitors_config(struct qxl_device *qdev) 402 { 403 int i; 404 405 BUG_ON(!qdev->ram_header->monitors_config); 406 407 if (qdev->monitors_config->count == 0) { 408 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__); 409 return; 410 } 411 for (i = 0 ; i < qdev->monitors_config->count ; ++i) { 412 struct qxl_head *head = &qdev->monitors_config->heads[i]; 413 414 if (head->y > 8192 || head->x > 8192 || 415 head->width > 8192 || head->height > 8192) { 416 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n", 417 i, head->width, head->height, 418 head->x, head->y); 419 return; 420 } 421 } 422 qxl_io_monitors_config(qdev); 423 } 424 425 static void qxl_monitors_config_set(struct qxl_device *qdev, 426 int index, 427 unsigned x, unsigned y, 428 unsigned width, unsigned height, 429 unsigned surf_id) 430 { 431 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y); 432 qdev->monitors_config->heads[index].x = x; 433 qdev->monitors_config->heads[index].y = y; 434 qdev->monitors_config->heads[index].width = width; 435 qdev->monitors_config->heads[index].height = height; 436 qdev->monitors_config->heads[index].surface_id = surf_id; 437 438 } 439 440 void qxl_mode_set_nofb(struct drm_crtc *crtc) 441 { 442 struct qxl_device *qdev = crtc->dev->dev_private; 443 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); 444 struct drm_display_mode *mode = &crtc->mode; 445 446 DRM_DEBUG("Mode set (%d,%d)\n", 447 mode->hdisplay, mode->vdisplay); 448 449 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 450 mode->hdisplay, mode->vdisplay, 0); 451 452 } 453 454 static void qxl_crtc_commit(struct drm_crtc *crtc) 455 { 456 DRM_DEBUG("\n"); 457 } 458 459 static void qxl_crtc_disable(struct drm_crtc *crtc) 460 { 461 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); 462 struct qxl_device *qdev = crtc->dev->dev_private; 463 464 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0); 465 466 qxl_send_monitors_config(qdev); 467 } 468 469 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = { 470 .dpms = qxl_crtc_dpms, 471 .disable = qxl_crtc_disable, 472 .mode_fixup = qxl_crtc_mode_fixup, 473 .mode_set_nofb = qxl_mode_set_nofb, 474 .commit = qxl_crtc_commit, 475 .atomic_flush = qxl_crtc_atomic_flush, 476 }; 477 478 int qxl_primary_atomic_check(struct drm_plane *plane, 479 struct drm_plane_state *state) 480 { 481 struct qxl_device *qdev = plane->dev->dev_private; 482 struct qxl_framebuffer *qfb; 483 struct qxl_bo *bo; 484 485 if (!state->crtc || !state->fb) 486 return 0; 487 488 qfb = to_qxl_framebuffer(state->fb); 489 bo = gem_to_qxl_bo(qfb->obj); 490 491 if (bo->surf.stride * bo->surf.height > qdev->vram_size) { 492 DRM_ERROR("Mode doesn't fit in vram size (vgamem)"); 493 return -EINVAL; 494 } 495 496 return 0; 497 } 498 499 static void qxl_primary_atomic_update(struct drm_plane *plane, 500 struct drm_plane_state *old_state) 501 { 502 struct qxl_device *qdev = plane->dev->dev_private; 503 struct qxl_framebuffer *qfb = 504 to_qxl_framebuffer(plane->state->fb); 505 struct qxl_framebuffer *qfb_old; 506 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj); 507 struct qxl_bo *bo_old; 508 struct drm_clip_rect norect = { 509 .x1 = 0, 510 .y1 = 0, 511 .x2 = qfb->base.width, 512 .y2 = qfb->base.height 513 }; 514 515 if (!old_state->fb) { 516 qxl_io_log(qdev, 517 "create primary fb: %dx%d,%d,%d\n", 518 bo->surf.width, bo->surf.height, 519 bo->surf.stride, bo->surf.format); 520 521 qxl_io_create_primary(qdev, 0, bo); 522 bo->is_primary = true; 523 return; 524 525 } else { 526 qfb_old = to_qxl_framebuffer(old_state->fb); 527 bo_old = gem_to_qxl_bo(qfb_old->obj); 528 bo_old->is_primary = false; 529 } 530 531 bo->is_primary = true; 532 qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1); 533 } 534 535 static void qxl_primary_atomic_disable(struct drm_plane *plane, 536 struct drm_plane_state *old_state) 537 { 538 struct qxl_device *qdev = plane->dev->dev_private; 539 540 if (old_state->fb) 541 { struct qxl_framebuffer *qfb = 542 to_qxl_framebuffer(old_state->fb); 543 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj); 544 545 qxl_io_destroy_primary(qdev); 546 bo->is_primary = false; 547 } 548 } 549 550 int qxl_plane_atomic_check(struct drm_plane *plane, 551 struct drm_plane_state *state) 552 { 553 return 0; 554 } 555 556 static void qxl_cursor_atomic_update(struct drm_plane *plane, 557 struct drm_plane_state *old_state) 558 { 559 struct drm_device *dev = plane->dev; 560 struct qxl_device *qdev = dev->dev_private; 561 struct drm_framebuffer *fb = plane->state->fb; 562 struct qxl_release *release; 563 struct qxl_cursor_cmd *cmd; 564 struct qxl_cursor *cursor; 565 struct drm_gem_object *obj; 566 struct qxl_bo *cursor_bo, *user_bo = NULL; 567 int ret; 568 void *user_ptr; 569 int size = 64*64*4; 570 571 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), 572 QXL_RELEASE_CURSOR_CMD, 573 &release, NULL); 574 if (ret) 575 return; 576 577 if (fb != old_state->fb) { 578 obj = to_qxl_framebuffer(fb)->obj; 579 user_bo = gem_to_qxl_bo(obj); 580 581 /* pinning is done in the prepare/cleanup framevbuffer */ 582 ret = qxl_bo_kmap(user_bo, &user_ptr); 583 if (ret) 584 goto out_free_release; 585 586 ret = qxl_alloc_bo_reserved(qdev, release, 587 sizeof(struct qxl_cursor) + size, 588 &cursor_bo); 589 if (ret) 590 goto out_kunmap; 591 592 ret = qxl_release_reserve_list(release, true); 593 if (ret) 594 goto out_free_bo; 595 596 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor); 597 if (ret) 598 goto out_backoff; 599 600 cursor->header.unique = 0; 601 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA; 602 cursor->header.width = 64; 603 cursor->header.height = 64; 604 cursor->header.hot_spot_x = fb->hot_x; 605 cursor->header.hot_spot_y = fb->hot_y; 606 cursor->data_size = size; 607 cursor->chunk.next_chunk = 0; 608 cursor->chunk.prev_chunk = 0; 609 cursor->chunk.data_size = size; 610 memcpy(cursor->chunk.data, user_ptr, size); 611 qxl_bo_kunmap(cursor_bo); 612 qxl_bo_kunmap(user_bo); 613 614 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release); 615 cmd->u.set.visible = 1; 616 cmd->u.set.shape = qxl_bo_physical_address(qdev, 617 cursor_bo, 0); 618 cmd->type = QXL_CURSOR_SET; 619 } else { 620 621 ret = qxl_release_reserve_list(release, true); 622 if (ret) 623 goto out_free_release; 624 625 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release); 626 cmd->type = QXL_CURSOR_MOVE; 627 } 628 629 cmd->u.position.x = plane->state->crtc_x + fb->hot_x; 630 cmd->u.position.y = plane->state->crtc_y + fb->hot_y; 631 632 qxl_release_unmap(qdev, release, &cmd->release_info); 633 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); 634 qxl_release_fence_buffer_objects(release); 635 636 return; 637 638 out_backoff: 639 qxl_release_backoff_reserve_list(release); 640 out_free_bo: 641 qxl_bo_unref(&cursor_bo); 642 out_kunmap: 643 qxl_bo_kunmap(user_bo); 644 out_free_release: 645 qxl_release_free(qdev, release); 646 return; 647 648 } 649 650 void qxl_cursor_atomic_disable(struct drm_plane *plane, 651 struct drm_plane_state *old_state) 652 { 653 struct qxl_device *qdev = plane->dev->dev_private; 654 struct qxl_release *release; 655 struct qxl_cursor_cmd *cmd; 656 int ret; 657 658 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), 659 QXL_RELEASE_CURSOR_CMD, 660 &release, NULL); 661 if (ret) 662 return; 663 664 ret = qxl_release_reserve_list(release, true); 665 if (ret) { 666 qxl_release_free(qdev, release); 667 return; 668 } 669 670 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release); 671 cmd->type = QXL_CURSOR_HIDE; 672 qxl_release_unmap(qdev, release, &cmd->release_info); 673 674 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); 675 qxl_release_fence_buffer_objects(release); 676 } 677 678 int qxl_plane_prepare_fb(struct drm_plane *plane, 679 struct drm_plane_state *new_state) 680 { 681 struct drm_gem_object *obj; 682 struct qxl_bo *user_bo; 683 int ret; 684 685 if (!new_state->fb) 686 return 0; 687 688 obj = to_qxl_framebuffer(new_state->fb)->obj; 689 user_bo = gem_to_qxl_bo(obj); 690 691 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL); 692 if (ret) 693 return ret; 694 695 return 0; 696 } 697 698 static void qxl_plane_cleanup_fb(struct drm_plane *plane, 699 struct drm_plane_state *old_state) 700 { 701 struct drm_gem_object *obj; 702 struct qxl_bo *user_bo; 703 704 if (!plane->state->fb) { 705 /* we never executed prepare_fb, so there's nothing to 706 * unpin. 707 */ 708 return; 709 } 710 711 obj = to_qxl_framebuffer(plane->state->fb)->obj; 712 user_bo = gem_to_qxl_bo(obj); 713 qxl_bo_unpin(user_bo); 714 } 715 716 static const uint32_t qxl_cursor_plane_formats[] = { 717 DRM_FORMAT_ARGB8888, 718 }; 719 720 static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = { 721 .atomic_check = qxl_plane_atomic_check, 722 .atomic_update = qxl_cursor_atomic_update, 723 .atomic_disable = qxl_cursor_atomic_disable, 724 .prepare_fb = qxl_plane_prepare_fb, 725 .cleanup_fb = qxl_plane_cleanup_fb, 726 }; 727 728 static const struct drm_plane_funcs qxl_cursor_plane_funcs = { 729 .update_plane = drm_atomic_helper_update_plane, 730 .disable_plane = drm_atomic_helper_disable_plane, 731 .destroy = drm_primary_helper_destroy, 732 .reset = drm_atomic_helper_plane_reset, 733 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 734 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 735 }; 736 737 static const uint32_t qxl_primary_plane_formats[] = { 738 DRM_FORMAT_XRGB8888, 739 DRM_FORMAT_ARGB8888, 740 }; 741 742 static const struct drm_plane_helper_funcs primary_helper_funcs = { 743 .atomic_check = qxl_primary_atomic_check, 744 .atomic_update = qxl_primary_atomic_update, 745 .atomic_disable = qxl_primary_atomic_disable, 746 .prepare_fb = qxl_plane_prepare_fb, 747 .cleanup_fb = qxl_plane_cleanup_fb, 748 }; 749 750 static const struct drm_plane_funcs qxl_primary_plane_funcs = { 751 .update_plane = drm_atomic_helper_update_plane, 752 .disable_plane = drm_atomic_helper_disable_plane, 753 .destroy = drm_primary_helper_destroy, 754 .reset = drm_atomic_helper_plane_reset, 755 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 756 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 757 }; 758 759 static struct drm_plane *qxl_create_plane(struct qxl_device *qdev, 760 unsigned int possible_crtcs, 761 enum drm_plane_type type) 762 { 763 const struct drm_plane_helper_funcs *helper_funcs = NULL; 764 struct drm_plane *plane; 765 const struct drm_plane_funcs *funcs; 766 const uint32_t *formats; 767 int num_formats; 768 int err; 769 770 if (type == DRM_PLANE_TYPE_PRIMARY) { 771 funcs = &qxl_primary_plane_funcs; 772 formats = qxl_primary_plane_formats; 773 num_formats = ARRAY_SIZE(qxl_primary_plane_formats); 774 helper_funcs = &primary_helper_funcs; 775 } else if (type == DRM_PLANE_TYPE_CURSOR) { 776 funcs = &qxl_cursor_plane_funcs; 777 formats = qxl_cursor_plane_formats; 778 helper_funcs = &qxl_cursor_helper_funcs; 779 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats); 780 } else { 781 return ERR_PTR(-EINVAL); 782 } 783 784 plane = kzalloc(sizeof(*plane), GFP_KERNEL); 785 if (!plane) 786 return ERR_PTR(-ENOMEM); 787 788 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs, 789 funcs, formats, num_formats, 790 type, NULL); 791 if (err) 792 goto free_plane; 793 794 drm_plane_helper_add(plane, helper_funcs); 795 796 return plane; 797 798 free_plane: 799 kfree(plane); 800 return ERR_PTR(-EINVAL); 801 } 802 803 static int qdev_crtc_init(struct drm_device *dev, int crtc_id) 804 { 805 struct qxl_crtc *qxl_crtc; 806 struct drm_plane *primary, *cursor; 807 struct qxl_device *qdev = dev->dev_private; 808 int r; 809 810 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL); 811 if (!qxl_crtc) 812 return -ENOMEM; 813 814 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY); 815 if (IS_ERR(primary)) { 816 r = -ENOMEM; 817 goto free_mem; 818 } 819 820 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR); 821 if (IS_ERR(cursor)) { 822 r = -ENOMEM; 823 goto clean_primary; 824 } 825 826 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor, 827 &qxl_crtc_funcs, NULL); 828 if (r) 829 goto clean_cursor; 830 831 qxl_crtc->index = crtc_id; 832 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs); 833 return 0; 834 835 clean_cursor: 836 drm_plane_cleanup(cursor); 837 kfree(cursor); 838 clean_primary: 839 drm_plane_cleanup(primary); 840 kfree(primary); 841 free_mem: 842 kfree(qxl_crtc); 843 return r; 844 } 845 846 static void qxl_enc_dpms(struct drm_encoder *encoder, int mode) 847 { 848 DRM_DEBUG("\n"); 849 } 850 851 static void qxl_enc_prepare(struct drm_encoder *encoder) 852 { 853 DRM_DEBUG("\n"); 854 } 855 856 static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev, 857 struct drm_encoder *encoder) 858 { 859 int i; 860 struct qxl_output *output = drm_encoder_to_qxl_output(encoder); 861 struct qxl_head *head; 862 struct drm_display_mode *mode; 863 864 BUG_ON(!encoder); 865 /* TODO: ugly, do better */ 866 i = output->index; 867 if (!qdev->monitors_config || 868 qdev->monitors_config->max_allowed <= i) { 869 DRM_ERROR( 870 "head number too large or missing monitors config: %p, %d", 871 qdev->monitors_config, 872 qdev->monitors_config ? 873 qdev->monitors_config->max_allowed : -1); 874 return; 875 } 876 if (!encoder->crtc) { 877 DRM_ERROR("missing crtc on encoder %p\n", encoder); 878 return; 879 } 880 if (i != 0) 881 DRM_DEBUG("missing for multiple monitors: no head holes\n"); 882 head = &qdev->monitors_config->heads[i]; 883 head->id = i; 884 if (encoder->crtc->enabled) { 885 mode = &encoder->crtc->mode; 886 head->width = mode->hdisplay; 887 head->height = mode->vdisplay; 888 head->x = encoder->crtc->x; 889 head->y = encoder->crtc->y; 890 if (qdev->monitors_config->count < i + 1) 891 qdev->monitors_config->count = i + 1; 892 } else { 893 head->width = 0; 894 head->height = 0; 895 head->x = 0; 896 head->y = 0; 897 } 898 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n", 899 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count); 900 head->flags = 0; 901 /* TODO - somewhere else to call this for multiple monitors 902 * (config_commit?) */ 903 qxl_send_monitors_config(qdev); 904 } 905 906 static void qxl_enc_commit(struct drm_encoder *encoder) 907 { 908 struct qxl_device *qdev = encoder->dev->dev_private; 909 910 qxl_write_monitors_config_for_encoder(qdev, encoder); 911 DRM_DEBUG("\n"); 912 } 913 914 static void qxl_enc_mode_set(struct drm_encoder *encoder, 915 struct drm_display_mode *mode, 916 struct drm_display_mode *adjusted_mode) 917 { 918 DRM_DEBUG("\n"); 919 } 920 921 static int qxl_conn_get_modes(struct drm_connector *connector) 922 { 923 unsigned pwidth = 1024; 924 unsigned pheight = 768; 925 int ret = 0; 926 927 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight); 928 if (ret < 0) 929 return ret; 930 ret += qxl_add_common_modes(connector, pwidth, pheight); 931 return ret; 932 } 933 934 static int qxl_conn_mode_valid(struct drm_connector *connector, 935 struct drm_display_mode *mode) 936 { 937 struct drm_device *ddev = connector->dev; 938 struct qxl_device *qdev = ddev->dev_private; 939 int i; 940 941 /* TODO: is this called for user defined modes? (xrandr --add-mode) 942 * TODO: check that the mode fits in the framebuffer */ 943 944 if(qdev->monitors_config_width == mode->hdisplay && 945 qdev->monitors_config_height == mode->vdisplay) 946 return MODE_OK; 947 948 for (i = 0; i < ARRAY_SIZE(common_modes); i++) { 949 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay) 950 return MODE_OK; 951 } 952 return MODE_BAD; 953 } 954 955 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector) 956 { 957 struct qxl_output *qxl_output = 958 drm_connector_to_qxl_output(connector); 959 960 DRM_DEBUG("\n"); 961 return &qxl_output->enc; 962 } 963 964 965 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = { 966 .dpms = qxl_enc_dpms, 967 .prepare = qxl_enc_prepare, 968 .mode_set = qxl_enc_mode_set, 969 .commit = qxl_enc_commit, 970 }; 971 972 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = { 973 .get_modes = qxl_conn_get_modes, 974 .mode_valid = qxl_conn_mode_valid, 975 .best_encoder = qxl_best_encoder, 976 }; 977 978 static enum drm_connector_status qxl_conn_detect( 979 struct drm_connector *connector, 980 bool force) 981 { 982 struct qxl_output *output = 983 drm_connector_to_qxl_output(connector); 984 struct drm_device *ddev = connector->dev; 985 struct qxl_device *qdev = ddev->dev_private; 986 bool connected = false; 987 988 /* The first monitor is always connected */ 989 if (!qdev->client_monitors_config) { 990 if (output->index == 0) 991 connected = true; 992 } else 993 connected = qdev->client_monitors_config->count > output->index && 994 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]); 995 996 DRM_DEBUG("#%d connected: %d\n", output->index, connected); 997 if (!connected) 998 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0); 999 1000 return connected ? connector_status_connected 1001 : connector_status_disconnected; 1002 } 1003 1004 static int qxl_conn_set_property(struct drm_connector *connector, 1005 struct drm_property *property, 1006 uint64_t value) 1007 { 1008 DRM_DEBUG("\n"); 1009 return 0; 1010 } 1011 1012 static void qxl_conn_destroy(struct drm_connector *connector) 1013 { 1014 struct qxl_output *qxl_output = 1015 drm_connector_to_qxl_output(connector); 1016 1017 drm_connector_unregister(connector); 1018 drm_connector_cleanup(connector); 1019 kfree(qxl_output); 1020 } 1021 1022 static const struct drm_connector_funcs qxl_connector_funcs = { 1023 .dpms = drm_helper_connector_dpms, 1024 .detect = qxl_conn_detect, 1025 .fill_modes = drm_helper_probe_single_connector_modes, 1026 .set_property = qxl_conn_set_property, 1027 .destroy = qxl_conn_destroy, 1028 .reset = drm_atomic_helper_connector_reset, 1029 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 1030 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 1031 }; 1032 1033 static void qxl_enc_destroy(struct drm_encoder *encoder) 1034 { 1035 drm_encoder_cleanup(encoder); 1036 } 1037 1038 static const struct drm_encoder_funcs qxl_enc_funcs = { 1039 .destroy = qxl_enc_destroy, 1040 }; 1041 1042 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev) 1043 { 1044 if (qdev->hotplug_mode_update_property) 1045 return 0; 1046 1047 qdev->hotplug_mode_update_property = 1048 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE, 1049 "hotplug_mode_update", 0, 1); 1050 1051 return 0; 1052 } 1053 1054 static int qdev_output_init(struct drm_device *dev, int num_output) 1055 { 1056 struct qxl_device *qdev = dev->dev_private; 1057 struct qxl_output *qxl_output; 1058 struct drm_connector *connector; 1059 struct drm_encoder *encoder; 1060 1061 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL); 1062 if (!qxl_output) 1063 return -ENOMEM; 1064 1065 qxl_output->index = num_output; 1066 1067 connector = &qxl_output->base; 1068 encoder = &qxl_output->enc; 1069 drm_connector_init(dev, &qxl_output->base, 1070 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL); 1071 1072 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs, 1073 DRM_MODE_ENCODER_VIRTUAL, NULL); 1074 1075 /* we get HPD via client monitors config */ 1076 connector->polled = DRM_CONNECTOR_POLL_HPD; 1077 encoder->possible_crtcs = 1 << num_output; 1078 drm_mode_connector_attach_encoder(&qxl_output->base, 1079 &qxl_output->enc); 1080 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs); 1081 drm_connector_helper_add(connector, &qxl_connector_helper_funcs); 1082 1083 drm_object_attach_property(&connector->base, 1084 qdev->hotplug_mode_update_property, 0); 1085 drm_object_attach_property(&connector->base, 1086 dev->mode_config.suggested_x_property, 0); 1087 drm_object_attach_property(&connector->base, 1088 dev->mode_config.suggested_y_property, 0); 1089 return 0; 1090 } 1091 1092 static struct drm_framebuffer * 1093 qxl_user_framebuffer_create(struct drm_device *dev, 1094 struct drm_file *file_priv, 1095 const struct drm_mode_fb_cmd2 *mode_cmd) 1096 { 1097 struct drm_gem_object *obj; 1098 struct qxl_framebuffer *qxl_fb; 1099 int ret; 1100 1101 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]); 1102 if (!obj) 1103 return NULL; 1104 1105 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL); 1106 if (qxl_fb == NULL) 1107 return NULL; 1108 1109 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs); 1110 if (ret) { 1111 kfree(qxl_fb); 1112 drm_gem_object_unreference_unlocked(obj); 1113 return NULL; 1114 } 1115 1116 return &qxl_fb->base; 1117 } 1118 1119 static const struct drm_mode_config_funcs qxl_mode_funcs = { 1120 .fb_create = qxl_user_framebuffer_create, 1121 .atomic_check = drm_atomic_helper_check, 1122 .atomic_commit = drm_atomic_helper_commit, 1123 }; 1124 1125 int qxl_create_monitors_object(struct qxl_device *qdev) 1126 { 1127 int ret; 1128 struct drm_gem_object *gobj; 1129 int max_allowed = qxl_num_crtc; 1130 int monitors_config_size = sizeof(struct qxl_monitors_config) + 1131 max_allowed * sizeof(struct qxl_head); 1132 1133 ret = qxl_gem_object_create(qdev, monitors_config_size, 0, 1134 QXL_GEM_DOMAIN_VRAM, 1135 false, false, NULL, &gobj); 1136 if (ret) { 1137 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret); 1138 return -ENOMEM; 1139 } 1140 qdev->monitors_config_bo = gem_to_qxl_bo(gobj); 1141 1142 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL); 1143 if (ret) 1144 return ret; 1145 1146 qxl_bo_kmap(qdev->monitors_config_bo, NULL); 1147 1148 qdev->monitors_config = qdev->monitors_config_bo->kptr; 1149 qdev->ram_header->monitors_config = 1150 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0); 1151 1152 memset(qdev->monitors_config, 0, monitors_config_size); 1153 qdev->monitors_config->max_allowed = max_allowed; 1154 return 0; 1155 } 1156 1157 int qxl_destroy_monitors_object(struct qxl_device *qdev) 1158 { 1159 int ret; 1160 1161 qdev->monitors_config = NULL; 1162 qdev->ram_header->monitors_config = 0; 1163 1164 qxl_bo_kunmap(qdev->monitors_config_bo); 1165 ret = qxl_bo_unpin(qdev->monitors_config_bo); 1166 if (ret) 1167 return ret; 1168 1169 qxl_bo_unref(&qdev->monitors_config_bo); 1170 return 0; 1171 } 1172 1173 int qxl_modeset_init(struct qxl_device *qdev) 1174 { 1175 int i; 1176 int ret; 1177 1178 drm_mode_config_init(&qdev->ddev); 1179 1180 ret = qxl_create_monitors_object(qdev); 1181 if (ret) 1182 return ret; 1183 1184 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs; 1185 1186 /* modes will be validated against the framebuffer size */ 1187 qdev->ddev.mode_config.min_width = 0; 1188 qdev->ddev.mode_config.min_height = 0; 1189 qdev->ddev.mode_config.max_width = 8192; 1190 qdev->ddev.mode_config.max_height = 8192; 1191 1192 qdev->ddev.mode_config.fb_base = qdev->vram_base; 1193 1194 drm_mode_create_suggested_offset_properties(&qdev->ddev); 1195 qxl_mode_create_hotplug_mode_update_property(qdev); 1196 1197 for (i = 0 ; i < qxl_num_crtc; ++i) { 1198 qdev_crtc_init(&qdev->ddev, i); 1199 qdev_output_init(&qdev->ddev, i); 1200 } 1201 1202 qxl_display_read_client_monitors_config(qdev); 1203 qdev->mode_info.mode_config_initialized = true; 1204 1205 drm_mode_config_reset(&qdev->ddev); 1206 1207 /* primary surface must be created by this point, to allow 1208 * issuing command queue commands and having them read by 1209 * spice server. */ 1210 qxl_fbdev_init(qdev); 1211 return 0; 1212 } 1213 1214 void qxl_modeset_fini(struct qxl_device *qdev) 1215 { 1216 qxl_fbdev_fini(qdev); 1217 1218 qxl_destroy_monitors_object(qdev); 1219 if (qdev->mode_info.mode_config_initialized) { 1220 drm_mode_config_cleanup(&qdev->ddev); 1221 qdev->mode_info.mode_config_initialized = false; 1222 } 1223 } 1224