2 * Copyright 2013 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Dave Airlie
26 #include <linux/crc32.h>
27 #include <linux/delay.h>
29 #include <drm/drm_drv.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/drm_gem_framebuffer_helper.h>
33 #include <drm/drm_plane_helper.h>
34 #include <drm/drm_probe_helper.h>
35 #include <drm/drm_simple_kms_helper.h>
38 #include "qxl_object.h"
40 static bool qxl_head_enabled(struct qxl_head *head)
42 return head->width && head->height;
45 static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
48 if (qdev->client_monitors_config &&
49 count > qdev->client_monitors_config->count) {
50 kfree(qdev->client_monitors_config);
51 qdev->client_monitors_config = NULL;
53 if (!qdev->client_monitors_config) {
54 qdev->client_monitors_config = kzalloc(
55 struct_size(qdev->client_monitors_config,
56 heads, count), GFP_KERNEL);
57 if (!qdev->client_monitors_config)
60 qdev->client_monitors_config->count = count;
65 MONITORS_CONFIG_MODIFIED,
66 MONITORS_CONFIG_UNCHANGED,
67 MONITORS_CONFIG_BAD_CRC,
68 MONITORS_CONFIG_ERROR,
71 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
76 int status = MONITORS_CONFIG_UNCHANGED;
78 num_monitors = qdev->rom->client_monitors_config.count;
79 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
80 sizeof(qdev->rom->client_monitors_config));
81 if (crc != qdev->rom->client_monitors_config_crc)
82 return MONITORS_CONFIG_BAD_CRC;
84 DRM_DEBUG_KMS("no client monitors configured\n");
87 if (num_monitors > qxl_num_crtc) {
88 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
89 qxl_num_crtc, num_monitors);
90 num_monitors = qxl_num_crtc;
92 num_monitors = qdev->rom->client_monitors_config.count;
94 if (qdev->client_monitors_config
95 && (num_monitors != qdev->client_monitors_config->count)) {
96 status = MONITORS_CONFIG_MODIFIED;
98 if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
99 status = MONITORS_CONFIG_ERROR;
102 /* we copy max from the client but it isn't used */
103 qdev->client_monitors_config->max_allowed = qxl_num_crtc;
104 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
105 struct qxl_urect *c_rect =
106 &qdev->rom->client_monitors_config.heads[i];
107 struct qxl_head *client_head =
108 &qdev->client_monitors_config->heads[i];
109 if (client_head->x != c_rect->left) {
110 client_head->x = c_rect->left;
111 status = MONITORS_CONFIG_MODIFIED;
113 if (client_head->y != c_rect->top) {
114 client_head->y = c_rect->top;
115 status = MONITORS_CONFIG_MODIFIED;
117 if (client_head->width != c_rect->right - c_rect->left) {
118 client_head->width = c_rect->right - c_rect->left;
119 status = MONITORS_CONFIG_MODIFIED;
121 if (client_head->height != c_rect->bottom - c_rect->top) {
122 client_head->height = c_rect->bottom - c_rect->top;
123 status = MONITORS_CONFIG_MODIFIED;
125 if (client_head->surface_id != 0) {
126 client_head->surface_id = 0;
127 status = MONITORS_CONFIG_MODIFIED;
129 if (client_head->id != i) {
131 status = MONITORS_CONFIG_MODIFIED;
133 if (client_head->flags != 0) {
134 client_head->flags = 0;
135 status = MONITORS_CONFIG_MODIFIED;
137 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
138 client_head->x, client_head->y);
144 static void qxl_update_offset_props(struct qxl_device *qdev)
146 struct drm_device *dev = &qdev->ddev;
147 struct drm_connector *connector;
148 struct qxl_output *output;
149 struct qxl_head *head;
151 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
152 output = drm_connector_to_qxl_output(connector);
154 head = &qdev->client_monitors_config->heads[output->index];
156 drm_object_property_set_value(&connector->base,
157 dev->mode_config.suggested_x_property, head->x);
158 drm_object_property_set_value(&connector->base,
159 dev->mode_config.suggested_y_property, head->y);
163 void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
165 struct drm_device *dev = &qdev->ddev;
166 struct drm_modeset_acquire_ctx ctx;
167 int status, retries, ret;
169 for (retries = 0; retries < 10; retries++) {
170 status = qxl_display_copy_rom_client_monitors_config(qdev);
171 if (status != MONITORS_CONFIG_BAD_CRC)
175 if (status == MONITORS_CONFIG_ERROR) {
176 DRM_DEBUG_KMS("ignoring client monitors config: error");
179 if (status == MONITORS_CONFIG_BAD_CRC) {
180 DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
183 if (status == MONITORS_CONFIG_UNCHANGED) {
184 DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
188 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
189 qxl_update_offset_props(qdev);
190 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
191 if (!drm_helper_hpd_irq_event(dev)) {
192 /* notify that the monitor configuration changed, to
193 adjust at the arbitrary resolution */
194 drm_kms_helper_hotplug_event(dev);
198 static int qxl_check_mode(struct qxl_device *qdev,
205 if (check_mul_overflow(width, 4u, &stride))
207 if (check_mul_overflow(stride, height, &size))
209 if (size > qdev->vram_size)
214 static int qxl_check_framebuffer(struct qxl_device *qdev,
217 return qxl_check_mode(qdev, bo->surf.width, bo->surf.height);
220 static int qxl_add_mode(struct drm_connector *connector,
225 struct drm_device *dev = connector->dev;
226 struct qxl_device *qdev = to_qxl(dev);
227 struct drm_display_mode *mode = NULL;
230 rc = qxl_check_mode(qdev, width, height);
234 mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
236 mode->type |= DRM_MODE_TYPE_PREFERRED;
237 mode->hdisplay = width;
238 mode->vdisplay = height;
239 drm_mode_set_name(mode);
240 drm_mode_probed_add(connector, mode);
244 static int qxl_add_monitors_config_modes(struct drm_connector *connector)
246 struct drm_device *dev = connector->dev;
247 struct qxl_device *qdev = to_qxl(dev);
248 struct qxl_output *output = drm_connector_to_qxl_output(connector);
249 int h = output->index;
250 struct qxl_head *head;
252 if (!qdev->monitors_config)
254 if (h >= qxl_num_crtc)
256 if (!qdev->client_monitors_config)
258 if (h >= qdev->client_monitors_config->count)
261 head = &qdev->client_monitors_config->heads[h];
262 DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
264 return qxl_add_mode(connector, head->width, head->height, true);
267 static struct mode_size {
276 static int qxl_add_extra_modes(struct drm_connector *connector)
280 for (i = 0; i < ARRAY_SIZE(extra_modes); i++)
281 ret += qxl_add_mode(connector,
288 static void qxl_send_monitors_config(struct qxl_device *qdev)
292 BUG_ON(!qdev->ram_header->monitors_config);
294 if (qdev->monitors_config->count == 0)
297 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
298 struct qxl_head *head = &qdev->monitors_config->heads[i];
300 if (head->y > 8192 || head->x > 8192 ||
301 head->width > 8192 || head->height > 8192) {
302 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
303 i, head->width, head->height,
308 qxl_io_monitors_config(qdev);
311 static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
314 struct drm_device *dev = crtc->dev;
315 struct qxl_device *qdev = to_qxl(dev);
316 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
317 struct qxl_head head;
318 int oldcount, i = qcrtc->index;
320 if (!qdev->primary_bo) {
321 DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
325 if (!qdev->monitors_config || qxl_num_crtc <= i)
331 oldcount = qdev->monitors_config->count;
332 if (crtc->state->active) {
333 struct drm_display_mode *mode = &crtc->mode;
335 head.width = mode->hdisplay;
336 head.height = mode->vdisplay;
339 if (qdev->monitors_config->count < i + 1)
340 qdev->monitors_config->count = i + 1;
341 if (qdev->primary_bo == qdev->dumb_shadow_bo)
342 head.x += qdev->dumb_heads[i].x;
348 if (qdev->monitors_config->count == i + 1)
349 qdev->monitors_config->count = i;
351 DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
355 if (head.width == qdev->monitors_config->heads[i].width &&
356 head.height == qdev->monitors_config->heads[i].height &&
357 head.x == qdev->monitors_config->heads[i].x &&
358 head.y == qdev->monitors_config->heads[i].y &&
359 oldcount == qdev->monitors_config->count)
362 DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
363 i, head.width, head.height, head.x, head.y,
364 crtc->state->active ? "on" : "off", reason);
365 if (oldcount != qdev->monitors_config->count)
366 DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
367 oldcount, qdev->monitors_config->count,
370 qdev->monitors_config->heads[i] = head;
371 qdev->monitors_config->max_allowed = qxl_num_crtc;
372 qxl_send_monitors_config(qdev);
375 static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
376 struct drm_crtc_state *old_crtc_state)
378 qxl_crtc_update_monitors_config(crtc, "flush");
381 static void qxl_crtc_destroy(struct drm_crtc *crtc)
383 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
385 qxl_bo_unref(&qxl_crtc->cursor_bo);
386 drm_crtc_cleanup(crtc);
390 static const struct drm_crtc_funcs qxl_crtc_funcs = {
391 .set_config = drm_atomic_helper_set_config,
392 .destroy = qxl_crtc_destroy,
393 .page_flip = drm_atomic_helper_page_flip,
394 .reset = drm_atomic_helper_crtc_reset,
395 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
396 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
399 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
400 struct drm_file *file_priv,
401 unsigned int flags, unsigned int color,
402 struct drm_clip_rect *clips,
403 unsigned int num_clips)
405 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
406 struct qxl_device *qdev = to_qxl(fb->dev);
407 struct drm_clip_rect norect;
409 struct drm_modeset_acquire_ctx ctx;
413 DRM_MODESET_LOCK_ALL_BEGIN(fb->dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
415 qobj = gem_to_qxl_bo(fb->obj[0]);
416 /* if we aren't primary surface ignore this */
417 is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
424 norect.x1 = norect.y1 = 0;
425 norect.x2 = fb->width;
426 norect.y2 = fb->height;
427 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
429 inc = 2; /* skip source rects */
432 qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
433 clips, num_clips, inc, 0);
436 DRM_MODESET_LOCK_ALL_END(fb->dev, ctx, ret);
441 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
442 .destroy = drm_gem_fb_destroy,
443 .dirty = qxl_framebuffer_surface_dirty,
444 .create_handle = drm_gem_fb_create_handle,
447 static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
448 struct drm_crtc_state *old_state)
450 qxl_crtc_update_monitors_config(crtc, "enable");
453 static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
454 struct drm_crtc_state *old_state)
456 qxl_crtc_update_monitors_config(crtc, "disable");
459 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
460 .atomic_flush = qxl_crtc_atomic_flush,
461 .atomic_enable = qxl_crtc_atomic_enable,
462 .atomic_disable = qxl_crtc_atomic_disable,
465 static int qxl_primary_atomic_check(struct drm_plane *plane,
466 struct drm_plane_state *state)
468 struct qxl_device *qdev = to_qxl(plane->dev);
471 if (!state->crtc || !state->fb)
474 bo = gem_to_qxl_bo(state->fb->obj[0]);
476 return qxl_check_framebuffer(qdev, bo);
479 static int qxl_primary_apply_cursor(struct drm_plane *plane)
481 struct drm_device *dev = plane->dev;
482 struct qxl_device *qdev = to_qxl(dev);
483 struct drm_framebuffer *fb = plane->state->fb;
484 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
485 struct qxl_cursor_cmd *cmd;
486 struct qxl_release *release;
489 if (!qcrtc->cursor_bo)
492 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
493 QXL_RELEASE_CURSOR_CMD,
498 ret = qxl_release_list_add(release, qcrtc->cursor_bo);
500 goto out_free_release;
502 ret = qxl_release_reserve_list(release, false);
504 goto out_free_release;
506 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
507 cmd->type = QXL_CURSOR_SET;
508 cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
509 cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
511 cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
513 cmd->u.set.visible = 1;
514 qxl_release_unmap(qdev, release, &cmd->release_info);
516 qxl_release_fence_buffer_objects(release);
517 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
522 qxl_release_free(qdev, release);
526 static void qxl_primary_atomic_update(struct drm_plane *plane,
527 struct drm_plane_state *old_state)
529 struct qxl_device *qdev = to_qxl(plane->dev);
530 struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]);
531 struct qxl_bo *primary;
532 struct drm_clip_rect norect = {
535 .x2 = plane->state->fb->width,
536 .y2 = plane->state->fb->height
538 uint32_t dumb_shadow_offset = 0;
540 primary = bo->shadow ? bo->shadow : bo;
542 if (!primary->is_primary) {
543 if (qdev->primary_bo)
544 qxl_io_destroy_primary(qdev);
545 qxl_io_create_primary(qdev, primary);
546 qxl_primary_apply_cursor(plane);
551 qdev->dumb_heads[plane->state->crtc->index].x;
553 qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1,
557 static void qxl_primary_atomic_disable(struct drm_plane *plane,
558 struct drm_plane_state *old_state)
560 struct qxl_device *qdev = to_qxl(plane->dev);
563 struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
565 if (bo->is_primary) {
566 qxl_io_destroy_primary(qdev);
567 bo->is_primary = false;
572 static void qxl_cursor_atomic_update(struct drm_plane *plane,
573 struct drm_plane_state *old_state)
575 struct drm_device *dev = plane->dev;
576 struct qxl_device *qdev = to_qxl(dev);
577 struct drm_framebuffer *fb = plane->state->fb;
578 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
579 struct qxl_release *release;
580 struct qxl_cursor_cmd *cmd;
581 struct qxl_cursor *cursor;
582 struct drm_gem_object *obj;
583 struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
588 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
589 QXL_RELEASE_CURSOR_CMD,
594 if (fb != old_state->fb) {
596 user_bo = gem_to_qxl_bo(obj);
598 /* pinning is done in the prepare/cleanup framevbuffer */
599 ret = qxl_bo_kmap(user_bo, &user_ptr);
601 goto out_free_release;
603 ret = qxl_alloc_bo_reserved(qdev, release,
604 sizeof(struct qxl_cursor) + size,
609 ret = qxl_bo_pin(cursor_bo);
613 ret = qxl_release_reserve_list(release, true);
617 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
621 cursor->header.unique = 0;
622 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
623 cursor->header.width = 64;
624 cursor->header.height = 64;
625 cursor->header.hot_spot_x = fb->hot_x;
626 cursor->header.hot_spot_y = fb->hot_y;
627 cursor->data_size = size;
628 cursor->chunk.next_chunk = 0;
629 cursor->chunk.prev_chunk = 0;
630 cursor->chunk.data_size = size;
631 memcpy(cursor->chunk.data, user_ptr, size);
632 qxl_bo_kunmap(cursor_bo);
633 qxl_bo_kunmap(user_bo);
635 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
636 cmd->u.set.visible = 1;
637 cmd->u.set.shape = qxl_bo_physical_address(qdev,
639 cmd->type = QXL_CURSOR_SET;
641 old_cursor_bo = qcrtc->cursor_bo;
642 qcrtc->cursor_bo = cursor_bo;
646 ret = qxl_release_reserve_list(release, true);
648 goto out_free_release;
650 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
651 cmd->type = QXL_CURSOR_MOVE;
654 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
655 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
657 qxl_release_unmap(qdev, release, &cmd->release_info);
658 qxl_release_fence_buffer_objects(release);
659 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
661 if (old_cursor_bo != NULL)
662 qxl_bo_unpin(old_cursor_bo);
663 qxl_bo_unref(&old_cursor_bo);
664 qxl_bo_unref(&cursor_bo);
669 qxl_release_backoff_reserve_list(release);
671 qxl_bo_unpin(cursor_bo);
673 qxl_bo_unref(&cursor_bo);
675 qxl_bo_kunmap(user_bo);
677 qxl_release_free(qdev, release);
682 static void qxl_cursor_atomic_disable(struct drm_plane *plane,
683 struct drm_plane_state *old_state)
685 struct qxl_device *qdev = to_qxl(plane->dev);
686 struct qxl_release *release;
687 struct qxl_cursor_cmd *cmd;
690 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
691 QXL_RELEASE_CURSOR_CMD,
696 ret = qxl_release_reserve_list(release, true);
698 qxl_release_free(qdev, release);
702 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
703 cmd->type = QXL_CURSOR_HIDE;
704 qxl_release_unmap(qdev, release, &cmd->release_info);
706 qxl_release_fence_buffer_objects(release);
707 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
710 static void qxl_update_dumb_head(struct qxl_device *qdev,
711 int index, struct qxl_bo *bo)
713 uint32_t width, height;
715 if (index >= qdev->monitors_config->max_allowed)
718 if (bo && bo->is_dumb) {
719 width = bo->surf.width;
720 height = bo->surf.height;
726 if (qdev->dumb_heads[index].width == width &&
727 qdev->dumb_heads[index].height == height)
730 DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
731 qdev->dumb_heads[index].width,
732 qdev->dumb_heads[index].height,
734 qdev->dumb_heads[index].width = width;
735 qdev->dumb_heads[index].height = height;
738 static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
739 struct qxl_surface *surf)
741 struct qxl_head *head;
744 memset(surf, 0, sizeof(*surf));
745 for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
746 head = qdev->dumb_heads + i;
747 head->x = surf->width;
748 surf->width += head->width;
749 if (surf->height < head->height)
750 surf->height = head->height;
752 if (surf->width < 64)
754 if (surf->height < 64)
756 surf->format = SPICE_SURFACE_FMT_32_xRGB;
757 surf->stride = surf->width * 4;
759 if (!qdev->dumb_shadow_bo ||
760 qdev->dumb_shadow_bo->surf.width != surf->width ||
761 qdev->dumb_shadow_bo->surf.height != surf->height)
762 DRM_DEBUG("%dx%d\n", surf->width, surf->height);
765 static int qxl_plane_prepare_fb(struct drm_plane *plane,
766 struct drm_plane_state *new_state)
768 struct qxl_device *qdev = to_qxl(plane->dev);
769 struct drm_gem_object *obj;
770 struct qxl_bo *user_bo;
771 struct qxl_surface surf;
777 obj = new_state->fb->obj[0];
778 user_bo = gem_to_qxl_bo(obj);
780 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
782 qxl_update_dumb_head(qdev, new_state->crtc->index,
784 qxl_calc_dumb_shadow(qdev, &surf);
785 if (!qdev->dumb_shadow_bo ||
786 qdev->dumb_shadow_bo->surf.width != surf.width ||
787 qdev->dumb_shadow_bo->surf.height != surf.height) {
788 if (qdev->dumb_shadow_bo) {
790 (&qdev->dumb_shadow_bo->tbo.base);
791 qdev->dumb_shadow_bo = NULL;
793 qxl_bo_create(qdev, surf.height * surf.stride,
794 true, true, QXL_GEM_DOMAIN_SURFACE, 0,
795 &surf, &qdev->dumb_shadow_bo);
797 if (user_bo->shadow != qdev->dumb_shadow_bo) {
798 if (user_bo->shadow) {
800 (&user_bo->shadow->tbo.base);
801 user_bo->shadow = NULL;
803 drm_gem_object_get(&qdev->dumb_shadow_bo->tbo.base);
804 user_bo->shadow = qdev->dumb_shadow_bo;
808 ret = qxl_bo_pin(user_bo);
815 static void qxl_plane_cleanup_fb(struct drm_plane *plane,
816 struct drm_plane_state *old_state)
818 struct drm_gem_object *obj;
819 struct qxl_bo *user_bo;
821 if (!old_state->fb) {
823 * we never executed prepare_fb, so there's nothing to
829 obj = old_state->fb->obj[0];
830 user_bo = gem_to_qxl_bo(obj);
831 qxl_bo_unpin(user_bo);
833 if (old_state->fb != plane->state->fb && user_bo->shadow) {
834 drm_gem_object_put(&user_bo->shadow->tbo.base);
835 user_bo->shadow = NULL;
839 static const uint32_t qxl_cursor_plane_formats[] = {
843 static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
844 .atomic_update = qxl_cursor_atomic_update,
845 .atomic_disable = qxl_cursor_atomic_disable,
846 .prepare_fb = qxl_plane_prepare_fb,
847 .cleanup_fb = qxl_plane_cleanup_fb,
850 static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
851 .update_plane = drm_atomic_helper_update_plane,
852 .disable_plane = drm_atomic_helper_disable_plane,
853 .destroy = drm_primary_helper_destroy,
854 .reset = drm_atomic_helper_plane_reset,
855 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
856 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
859 static const uint32_t qxl_primary_plane_formats[] = {
864 static const struct drm_plane_helper_funcs primary_helper_funcs = {
865 .atomic_check = qxl_primary_atomic_check,
866 .atomic_update = qxl_primary_atomic_update,
867 .atomic_disable = qxl_primary_atomic_disable,
868 .prepare_fb = qxl_plane_prepare_fb,
869 .cleanup_fb = qxl_plane_cleanup_fb,
872 static const struct drm_plane_funcs qxl_primary_plane_funcs = {
873 .update_plane = drm_atomic_helper_update_plane,
874 .disable_plane = drm_atomic_helper_disable_plane,
875 .destroy = drm_primary_helper_destroy,
876 .reset = drm_atomic_helper_plane_reset,
877 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
878 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
881 static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
882 unsigned int possible_crtcs,
883 enum drm_plane_type type)
885 const struct drm_plane_helper_funcs *helper_funcs = NULL;
886 struct drm_plane *plane;
887 const struct drm_plane_funcs *funcs;
888 const uint32_t *formats;
892 if (type == DRM_PLANE_TYPE_PRIMARY) {
893 funcs = &qxl_primary_plane_funcs;
894 formats = qxl_primary_plane_formats;
895 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
896 helper_funcs = &primary_helper_funcs;
897 } else if (type == DRM_PLANE_TYPE_CURSOR) {
898 funcs = &qxl_cursor_plane_funcs;
899 formats = qxl_cursor_plane_formats;
900 helper_funcs = &qxl_cursor_helper_funcs;
901 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
903 return ERR_PTR(-EINVAL);
906 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
908 return ERR_PTR(-ENOMEM);
910 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
911 funcs, formats, num_formats,
916 drm_plane_helper_add(plane, helper_funcs);
922 return ERR_PTR(-EINVAL);
925 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
927 struct qxl_crtc *qxl_crtc;
928 struct drm_plane *primary, *cursor;
929 struct qxl_device *qdev = to_qxl(dev);
932 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
936 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
937 if (IS_ERR(primary)) {
942 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
943 if (IS_ERR(cursor)) {
948 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
949 &qxl_crtc_funcs, NULL);
953 qxl_crtc->index = crtc_id;
954 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
958 drm_plane_cleanup(cursor);
961 drm_plane_cleanup(primary);
968 static int qxl_conn_get_modes(struct drm_connector *connector)
970 struct drm_device *dev = connector->dev;
971 struct qxl_device *qdev = to_qxl(dev);
972 struct qxl_output *output = drm_connector_to_qxl_output(connector);
973 unsigned int pwidth = 1024;
974 unsigned int pheight = 768;
977 if (qdev->client_monitors_config) {
978 struct qxl_head *head;
979 head = &qdev->client_monitors_config->heads[output->index];
981 pwidth = head->width;
983 pheight = head->height;
986 ret += drm_add_modes_noedid(connector, 8192, 8192);
987 ret += qxl_add_extra_modes(connector);
988 ret += qxl_add_monitors_config_modes(connector);
989 drm_set_preferred_mode(connector, pwidth, pheight);
993 static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
994 struct drm_display_mode *mode)
996 struct drm_device *ddev = connector->dev;
997 struct qxl_device *qdev = to_qxl(ddev);
999 if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
1005 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1007 struct qxl_output *qxl_output =
1008 drm_connector_to_qxl_output(connector);
1011 return &qxl_output->enc;
1014 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1015 .get_modes = qxl_conn_get_modes,
1016 .mode_valid = qxl_conn_mode_valid,
1017 .best_encoder = qxl_best_encoder,
1020 static enum drm_connector_status qxl_conn_detect(
1021 struct drm_connector *connector,
1024 struct qxl_output *output =
1025 drm_connector_to_qxl_output(connector);
1026 struct drm_device *ddev = connector->dev;
1027 struct qxl_device *qdev = to_qxl(ddev);
1028 bool connected = false;
1030 /* The first monitor is always connected */
1031 if (!qdev->client_monitors_config) {
1032 if (output->index == 0)
1035 connected = qdev->client_monitors_config->count > output->index &&
1036 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1038 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1040 return connected ? connector_status_connected
1041 : connector_status_disconnected;
1044 static void qxl_conn_destroy(struct drm_connector *connector)
1046 struct qxl_output *qxl_output =
1047 drm_connector_to_qxl_output(connector);
1049 drm_connector_unregister(connector);
1050 drm_connector_cleanup(connector);
1054 static const struct drm_connector_funcs qxl_connector_funcs = {
1055 .detect = qxl_conn_detect,
1056 .fill_modes = drm_helper_probe_single_connector_modes,
1057 .destroy = qxl_conn_destroy,
1058 .reset = drm_atomic_helper_connector_reset,
1059 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1060 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1063 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1065 if (qdev->hotplug_mode_update_property)
1068 qdev->hotplug_mode_update_property =
1069 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1070 "hotplug_mode_update", 0, 1);
1075 static int qdev_output_init(struct drm_device *dev, int num_output)
1077 struct qxl_device *qdev = to_qxl(dev);
1078 struct qxl_output *qxl_output;
1079 struct drm_connector *connector;
1080 struct drm_encoder *encoder;
1083 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1087 qxl_output->index = num_output;
1089 connector = &qxl_output->base;
1090 encoder = &qxl_output->enc;
1091 drm_connector_init(dev, &qxl_output->base,
1092 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1094 ret = drm_simple_encoder_init(dev, &qxl_output->enc,
1095 DRM_MODE_ENCODER_VIRTUAL);
1097 drm_err(dev, "drm_simple_encoder_init() failed, error %d\n",
1099 goto err_drm_connector_cleanup;
1102 /* we get HPD via client monitors config */
1103 connector->polled = DRM_CONNECTOR_POLL_HPD;
1104 encoder->possible_crtcs = 1 << num_output;
1105 drm_connector_attach_encoder(&qxl_output->base,
1107 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1109 drm_object_attach_property(&connector->base,
1110 qdev->hotplug_mode_update_property, 0);
1111 drm_object_attach_property(&connector->base,
1112 dev->mode_config.suggested_x_property, 0);
1113 drm_object_attach_property(&connector->base,
1114 dev->mode_config.suggested_y_property, 0);
1117 err_drm_connector_cleanup:
1118 drm_connector_cleanup(&qxl_output->base);
1123 static struct drm_framebuffer *
1124 qxl_user_framebuffer_create(struct drm_device *dev,
1125 struct drm_file *file_priv,
1126 const struct drm_mode_fb_cmd2 *mode_cmd)
1128 return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
1132 static const struct drm_mode_config_funcs qxl_mode_funcs = {
1133 .fb_create = qxl_user_framebuffer_create,
1134 .atomic_check = drm_atomic_helper_check,
1135 .atomic_commit = drm_atomic_helper_commit,
1138 int qxl_create_monitors_object(struct qxl_device *qdev)
1141 struct drm_gem_object *gobj;
1142 int monitors_config_size = sizeof(struct qxl_monitors_config) +
1143 qxl_num_crtc * sizeof(struct qxl_head);
1145 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1146 QXL_GEM_DOMAIN_VRAM,
1147 false, false, NULL, &gobj);
1149 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1152 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1154 ret = qxl_bo_pin(qdev->monitors_config_bo);
1158 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
1160 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1161 qdev->ram_header->monitors_config =
1162 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1164 memset(qdev->monitors_config, 0, monitors_config_size);
1165 qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
1167 if (!qdev->dumb_heads) {
1168 qxl_destroy_monitors_object(qdev);
1174 int qxl_destroy_monitors_object(struct qxl_device *qdev)
1178 qdev->monitors_config = NULL;
1179 qdev->ram_header->monitors_config = 0;
1181 qxl_bo_kunmap(qdev->monitors_config_bo);
1182 ret = qxl_bo_unpin(qdev->monitors_config_bo);
1186 qxl_bo_unref(&qdev->monitors_config_bo);
1190 int qxl_modeset_init(struct qxl_device *qdev)
1195 drm_mode_config_init(&qdev->ddev);
1197 ret = qxl_create_monitors_object(qdev);
1201 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1203 /* modes will be validated against the framebuffer size */
1204 qdev->ddev.mode_config.min_width = 0;
1205 qdev->ddev.mode_config.min_height = 0;
1206 qdev->ddev.mode_config.max_width = 8192;
1207 qdev->ddev.mode_config.max_height = 8192;
1209 qdev->ddev.mode_config.fb_base = qdev->vram_base;
1211 drm_mode_create_suggested_offset_properties(&qdev->ddev);
1212 qxl_mode_create_hotplug_mode_update_property(qdev);
1214 for (i = 0 ; i < qxl_num_crtc; ++i) {
1215 qdev_crtc_init(&qdev->ddev, i);
1216 qdev_output_init(&qdev->ddev, i);
1219 qxl_display_read_client_monitors_config(qdev);
1221 drm_mode_config_reset(&qdev->ddev);
1225 void qxl_modeset_fini(struct qxl_device *qdev)
1227 if (qdev->dumb_shadow_bo) {
1228 drm_gem_object_put(&qdev->dumb_shadow_bo->tbo.base);
1229 qdev->dumb_shadow_bo = NULL;
1231 qxl_destroy_monitors_object(qdev);
1232 drm_mode_config_cleanup(&qdev->ddev);