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)
330 oldcount = qdev->monitors_config->count;
331 if (crtc->state->active) {
332 struct drm_display_mode *mode = &crtc->mode;
334 head.width = mode->hdisplay;
335 head.height = mode->vdisplay;
338 if (qdev->monitors_config->count < i + 1)
339 qdev->monitors_config->count = i + 1;
340 if (qdev->primary_bo == qdev->dumb_shadow_bo)
341 head.x += qdev->dumb_heads[i].x;
347 if (qdev->monitors_config->count == i + 1)
348 qdev->monitors_config->count = i;
350 DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
354 if (head.width == qdev->monitors_config->heads[i].width &&
355 head.height == qdev->monitors_config->heads[i].height &&
356 head.x == qdev->monitors_config->heads[i].x &&
357 head.y == qdev->monitors_config->heads[i].y &&
358 oldcount == qdev->monitors_config->count)
361 DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
362 i, head.width, head.height, head.x, head.y,
363 crtc->state->active ? "on" : "off", reason);
364 if (oldcount != qdev->monitors_config->count)
365 DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
366 oldcount, qdev->monitors_config->count,
369 qdev->monitors_config->heads[i] = head;
370 qdev->monitors_config->max_allowed = qxl_num_crtc;
371 qxl_send_monitors_config(qdev);
374 static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
375 struct drm_crtc_state *old_crtc_state)
377 qxl_crtc_update_monitors_config(crtc, "flush");
380 static void qxl_crtc_destroy(struct drm_crtc *crtc)
382 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
384 qxl_bo_unref(&qxl_crtc->cursor_bo);
385 drm_crtc_cleanup(crtc);
389 static const struct drm_crtc_funcs qxl_crtc_funcs = {
390 .set_config = drm_atomic_helper_set_config,
391 .destroy = qxl_crtc_destroy,
392 .page_flip = drm_atomic_helper_page_flip,
393 .reset = drm_atomic_helper_crtc_reset,
394 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
395 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
398 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
399 struct drm_file *file_priv,
400 unsigned int flags, unsigned int color,
401 struct drm_clip_rect *clips,
402 unsigned int num_clips)
404 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
405 struct qxl_device *qdev = to_qxl(fb->dev);
406 struct drm_clip_rect norect;
408 struct drm_modeset_acquire_ctx ctx;
412 DRM_MODESET_LOCK_ALL_BEGIN(fb->dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
414 qobj = gem_to_qxl_bo(fb->obj[0]);
415 /* if we aren't primary surface ignore this */
416 is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
423 norect.x1 = norect.y1 = 0;
424 norect.x2 = fb->width;
425 norect.y2 = fb->height;
426 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
428 inc = 2; /* skip source rects */
431 qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
432 clips, num_clips, inc, 0);
435 DRM_MODESET_LOCK_ALL_END(fb->dev, ctx, ret);
440 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
441 .destroy = drm_gem_fb_destroy,
442 .dirty = qxl_framebuffer_surface_dirty,
443 .create_handle = drm_gem_fb_create_handle,
446 static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
447 struct drm_atomic_state *state)
449 qxl_crtc_update_monitors_config(crtc, "enable");
452 static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
453 struct drm_atomic_state *state)
455 qxl_crtc_update_monitors_config(crtc, "disable");
458 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
459 .atomic_flush = qxl_crtc_atomic_flush,
460 .atomic_enable = qxl_crtc_atomic_enable,
461 .atomic_disable = qxl_crtc_atomic_disable,
464 static int qxl_primary_atomic_check(struct drm_plane *plane,
465 struct drm_plane_state *state)
467 struct qxl_device *qdev = to_qxl(plane->dev);
470 if (!state->crtc || !state->fb)
473 bo = gem_to_qxl_bo(state->fb->obj[0]);
475 return qxl_check_framebuffer(qdev, bo);
478 static int qxl_primary_apply_cursor(struct drm_plane *plane)
480 struct drm_device *dev = plane->dev;
481 struct qxl_device *qdev = to_qxl(dev);
482 struct drm_framebuffer *fb = plane->state->fb;
483 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
484 struct qxl_cursor_cmd *cmd;
485 struct qxl_release *release;
488 if (!qcrtc->cursor_bo)
491 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
492 QXL_RELEASE_CURSOR_CMD,
497 ret = qxl_release_list_add(release, qcrtc->cursor_bo);
499 goto out_free_release;
501 ret = qxl_release_reserve_list(release, false);
503 goto out_free_release;
505 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
506 cmd->type = QXL_CURSOR_SET;
507 cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
508 cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
510 cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
512 cmd->u.set.visible = 1;
513 qxl_release_unmap(qdev, release, &cmd->release_info);
515 qxl_release_fence_buffer_objects(release);
516 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
521 qxl_release_free(qdev, release);
525 static void qxl_primary_atomic_update(struct drm_plane *plane,
526 struct drm_plane_state *old_state)
528 struct qxl_device *qdev = to_qxl(plane->dev);
529 struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]);
530 struct qxl_bo *primary;
531 struct drm_clip_rect norect = {
534 .x2 = plane->state->fb->width,
535 .y2 = plane->state->fb->height
537 uint32_t dumb_shadow_offset = 0;
539 primary = bo->shadow ? bo->shadow : bo;
541 if (!primary->is_primary) {
542 if (qdev->primary_bo)
543 qxl_io_destroy_primary(qdev);
544 qxl_io_create_primary(qdev, primary);
545 qxl_primary_apply_cursor(plane);
550 qdev->dumb_heads[plane->state->crtc->index].x;
552 qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1,
556 static void qxl_primary_atomic_disable(struct drm_plane *plane,
557 struct drm_plane_state *old_state)
559 struct qxl_device *qdev = to_qxl(plane->dev);
562 struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
564 if (bo->is_primary) {
565 qxl_io_destroy_primary(qdev);
566 bo->is_primary = false;
571 static void qxl_cursor_atomic_update(struct drm_plane *plane,
572 struct drm_plane_state *old_state)
574 struct drm_device *dev = plane->dev;
575 struct qxl_device *qdev = to_qxl(dev);
576 struct drm_framebuffer *fb = plane->state->fb;
577 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
578 struct qxl_release *release;
579 struct qxl_cursor_cmd *cmd;
580 struct qxl_cursor *cursor;
581 struct drm_gem_object *obj;
582 struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
587 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
588 QXL_RELEASE_CURSOR_CMD,
593 if (fb != old_state->fb) {
595 user_bo = gem_to_qxl_bo(obj);
597 /* pinning is done in the prepare/cleanup framevbuffer */
598 ret = qxl_bo_kmap(user_bo, &user_ptr);
600 goto out_free_release;
602 ret = qxl_alloc_bo_reserved(qdev, release,
603 sizeof(struct qxl_cursor) + size,
608 ret = qxl_bo_pin(cursor_bo);
612 ret = qxl_release_reserve_list(release, true);
616 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
620 cursor->header.unique = 0;
621 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
622 cursor->header.width = 64;
623 cursor->header.height = 64;
624 cursor->header.hot_spot_x = fb->hot_x;
625 cursor->header.hot_spot_y = fb->hot_y;
626 cursor->data_size = size;
627 cursor->chunk.next_chunk = 0;
628 cursor->chunk.prev_chunk = 0;
629 cursor->chunk.data_size = size;
630 memcpy(cursor->chunk.data, user_ptr, size);
631 qxl_bo_kunmap(cursor_bo);
632 qxl_bo_kunmap(user_bo);
634 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
635 cmd->u.set.visible = 1;
636 cmd->u.set.shape = qxl_bo_physical_address(qdev,
638 cmd->type = QXL_CURSOR_SET;
640 old_cursor_bo = qcrtc->cursor_bo;
641 qcrtc->cursor_bo = cursor_bo;
645 ret = qxl_release_reserve_list(release, true);
647 goto out_free_release;
649 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
650 cmd->type = QXL_CURSOR_MOVE;
653 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
654 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
656 qxl_release_unmap(qdev, release, &cmd->release_info);
657 qxl_release_fence_buffer_objects(release);
658 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
660 if (old_cursor_bo != NULL)
661 qxl_bo_unpin(old_cursor_bo);
662 qxl_bo_unref(&old_cursor_bo);
663 qxl_bo_unref(&cursor_bo);
668 qxl_release_backoff_reserve_list(release);
670 qxl_bo_unpin(cursor_bo);
672 qxl_bo_unref(&cursor_bo);
674 qxl_bo_kunmap(user_bo);
676 qxl_release_free(qdev, release);
681 static void qxl_cursor_atomic_disable(struct drm_plane *plane,
682 struct drm_plane_state *old_state)
684 struct qxl_device *qdev = to_qxl(plane->dev);
685 struct qxl_release *release;
686 struct qxl_cursor_cmd *cmd;
689 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
690 QXL_RELEASE_CURSOR_CMD,
695 ret = qxl_release_reserve_list(release, true);
697 qxl_release_free(qdev, release);
701 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
702 cmd->type = QXL_CURSOR_HIDE;
703 qxl_release_unmap(qdev, release, &cmd->release_info);
705 qxl_release_fence_buffer_objects(release);
706 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
709 static void qxl_update_dumb_head(struct qxl_device *qdev,
710 int index, struct qxl_bo *bo)
712 uint32_t width, height;
714 if (index >= qdev->monitors_config->max_allowed)
717 if (bo && bo->is_dumb) {
718 width = bo->surf.width;
719 height = bo->surf.height;
725 if (qdev->dumb_heads[index].width == width &&
726 qdev->dumb_heads[index].height == height)
729 DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
730 qdev->dumb_heads[index].width,
731 qdev->dumb_heads[index].height,
733 qdev->dumb_heads[index].width = width;
734 qdev->dumb_heads[index].height = height;
737 static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
738 struct qxl_surface *surf)
740 struct qxl_head *head;
743 memset(surf, 0, sizeof(*surf));
744 for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
745 head = qdev->dumb_heads + i;
746 head->x = surf->width;
747 surf->width += head->width;
748 if (surf->height < head->height)
749 surf->height = head->height;
751 if (surf->width < 64)
753 if (surf->height < 64)
755 surf->format = SPICE_SURFACE_FMT_32_xRGB;
756 surf->stride = surf->width * 4;
758 if (!qdev->dumb_shadow_bo ||
759 qdev->dumb_shadow_bo->surf.width != surf->width ||
760 qdev->dumb_shadow_bo->surf.height != surf->height)
761 DRM_DEBUG("%dx%d\n", surf->width, surf->height);
764 static int qxl_plane_prepare_fb(struct drm_plane *plane,
765 struct drm_plane_state *new_state)
767 struct qxl_device *qdev = to_qxl(plane->dev);
768 struct drm_gem_object *obj;
769 struct qxl_bo *user_bo;
770 struct qxl_surface surf;
775 obj = new_state->fb->obj[0];
776 user_bo = gem_to_qxl_bo(obj);
778 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
780 qxl_update_dumb_head(qdev, new_state->crtc->index,
782 qxl_calc_dumb_shadow(qdev, &surf);
783 if (!qdev->dumb_shadow_bo ||
784 qdev->dumb_shadow_bo->surf.width != surf.width ||
785 qdev->dumb_shadow_bo->surf.height != surf.height) {
786 if (qdev->dumb_shadow_bo) {
788 (&qdev->dumb_shadow_bo->tbo.base);
789 qdev->dumb_shadow_bo = NULL;
791 qxl_bo_create(qdev, surf.height * surf.stride,
792 true, true, QXL_GEM_DOMAIN_SURFACE, &surf,
793 &qdev->dumb_shadow_bo);
795 if (user_bo->shadow != qdev->dumb_shadow_bo) {
796 if (user_bo->shadow) {
798 (&user_bo->shadow->tbo.base);
799 user_bo->shadow = NULL;
801 drm_gem_object_get(&qdev->dumb_shadow_bo->tbo.base);
802 user_bo->shadow = qdev->dumb_shadow_bo;
806 return qxl_bo_pin(user_bo);
809 static void qxl_plane_cleanup_fb(struct drm_plane *plane,
810 struct drm_plane_state *old_state)
812 struct drm_gem_object *obj;
813 struct qxl_bo *user_bo;
815 if (!old_state->fb) {
817 * we never executed prepare_fb, so there's nothing to
823 obj = old_state->fb->obj[0];
824 user_bo = gem_to_qxl_bo(obj);
825 qxl_bo_unpin(user_bo);
827 if (old_state->fb != plane->state->fb && user_bo->shadow) {
828 drm_gem_object_put(&user_bo->shadow->tbo.base);
829 user_bo->shadow = NULL;
833 static const uint32_t qxl_cursor_plane_formats[] = {
837 static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
838 .atomic_update = qxl_cursor_atomic_update,
839 .atomic_disable = qxl_cursor_atomic_disable,
840 .prepare_fb = qxl_plane_prepare_fb,
841 .cleanup_fb = qxl_plane_cleanup_fb,
844 static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
845 .update_plane = drm_atomic_helper_update_plane,
846 .disable_plane = drm_atomic_helper_disable_plane,
847 .destroy = drm_primary_helper_destroy,
848 .reset = drm_atomic_helper_plane_reset,
849 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
850 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
853 static const uint32_t qxl_primary_plane_formats[] = {
858 static const struct drm_plane_helper_funcs primary_helper_funcs = {
859 .atomic_check = qxl_primary_atomic_check,
860 .atomic_update = qxl_primary_atomic_update,
861 .atomic_disable = qxl_primary_atomic_disable,
862 .prepare_fb = qxl_plane_prepare_fb,
863 .cleanup_fb = qxl_plane_cleanup_fb,
866 static const struct drm_plane_funcs qxl_primary_plane_funcs = {
867 .update_plane = drm_atomic_helper_update_plane,
868 .disable_plane = drm_atomic_helper_disable_plane,
869 .destroy = drm_primary_helper_destroy,
870 .reset = drm_atomic_helper_plane_reset,
871 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
872 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
875 static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
876 unsigned int possible_crtcs,
877 enum drm_plane_type type)
879 const struct drm_plane_helper_funcs *helper_funcs = NULL;
880 struct drm_plane *plane;
881 const struct drm_plane_funcs *funcs;
882 const uint32_t *formats;
886 if (type == DRM_PLANE_TYPE_PRIMARY) {
887 funcs = &qxl_primary_plane_funcs;
888 formats = qxl_primary_plane_formats;
889 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
890 helper_funcs = &primary_helper_funcs;
891 } else if (type == DRM_PLANE_TYPE_CURSOR) {
892 funcs = &qxl_cursor_plane_funcs;
893 formats = qxl_cursor_plane_formats;
894 helper_funcs = &qxl_cursor_helper_funcs;
895 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
897 return ERR_PTR(-EINVAL);
900 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
902 return ERR_PTR(-ENOMEM);
904 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
905 funcs, formats, num_formats,
910 drm_plane_helper_add(plane, helper_funcs);
916 return ERR_PTR(-EINVAL);
919 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
921 struct qxl_crtc *qxl_crtc;
922 struct drm_plane *primary, *cursor;
923 struct qxl_device *qdev = to_qxl(dev);
926 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
930 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
931 if (IS_ERR(primary)) {
936 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
937 if (IS_ERR(cursor)) {
942 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
943 &qxl_crtc_funcs, NULL);
947 qxl_crtc->index = crtc_id;
948 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
952 drm_plane_cleanup(cursor);
955 drm_plane_cleanup(primary);
962 static int qxl_conn_get_modes(struct drm_connector *connector)
964 struct drm_device *dev = connector->dev;
965 struct qxl_device *qdev = to_qxl(dev);
966 struct qxl_output *output = drm_connector_to_qxl_output(connector);
967 unsigned int pwidth = 1024;
968 unsigned int pheight = 768;
971 if (qdev->client_monitors_config) {
972 struct qxl_head *head;
973 head = &qdev->client_monitors_config->heads[output->index];
975 pwidth = head->width;
977 pheight = head->height;
980 ret += drm_add_modes_noedid(connector, 8192, 8192);
981 ret += qxl_add_extra_modes(connector);
982 ret += qxl_add_monitors_config_modes(connector);
983 drm_set_preferred_mode(connector, pwidth, pheight);
987 static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
988 struct drm_display_mode *mode)
990 struct drm_device *ddev = connector->dev;
991 struct qxl_device *qdev = to_qxl(ddev);
993 if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
999 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1001 struct qxl_output *qxl_output =
1002 drm_connector_to_qxl_output(connector);
1005 return &qxl_output->enc;
1008 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1009 .get_modes = qxl_conn_get_modes,
1010 .mode_valid = qxl_conn_mode_valid,
1011 .best_encoder = qxl_best_encoder,
1014 static enum drm_connector_status qxl_conn_detect(
1015 struct drm_connector *connector,
1018 struct qxl_output *output =
1019 drm_connector_to_qxl_output(connector);
1020 struct drm_device *ddev = connector->dev;
1021 struct qxl_device *qdev = to_qxl(ddev);
1022 bool connected = false;
1024 /* The first monitor is always connected */
1025 if (!qdev->client_monitors_config) {
1026 if (output->index == 0)
1029 connected = qdev->client_monitors_config->count > output->index &&
1030 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1032 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1034 return connected ? connector_status_connected
1035 : connector_status_disconnected;
1038 static void qxl_conn_destroy(struct drm_connector *connector)
1040 struct qxl_output *qxl_output =
1041 drm_connector_to_qxl_output(connector);
1043 drm_connector_unregister(connector);
1044 drm_connector_cleanup(connector);
1048 static const struct drm_connector_funcs qxl_connector_funcs = {
1049 .detect = qxl_conn_detect,
1050 .fill_modes = drm_helper_probe_single_connector_modes,
1051 .destroy = qxl_conn_destroy,
1052 .reset = drm_atomic_helper_connector_reset,
1053 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1054 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1057 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1059 if (qdev->hotplug_mode_update_property)
1062 qdev->hotplug_mode_update_property =
1063 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1064 "hotplug_mode_update", 0, 1);
1069 static int qdev_output_init(struct drm_device *dev, int num_output)
1071 struct qxl_device *qdev = to_qxl(dev);
1072 struct qxl_output *qxl_output;
1073 struct drm_connector *connector;
1074 struct drm_encoder *encoder;
1077 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1081 qxl_output->index = num_output;
1083 connector = &qxl_output->base;
1084 encoder = &qxl_output->enc;
1085 drm_connector_init(dev, &qxl_output->base,
1086 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1088 ret = drm_simple_encoder_init(dev, &qxl_output->enc,
1089 DRM_MODE_ENCODER_VIRTUAL);
1091 drm_err(dev, "drm_simple_encoder_init() failed, error %d\n",
1093 goto err_drm_connector_cleanup;
1096 /* we get HPD via client monitors config */
1097 connector->polled = DRM_CONNECTOR_POLL_HPD;
1098 encoder->possible_crtcs = 1 << num_output;
1099 drm_connector_attach_encoder(&qxl_output->base,
1101 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1103 drm_object_attach_property(&connector->base,
1104 qdev->hotplug_mode_update_property, 0);
1105 drm_object_attach_property(&connector->base,
1106 dev->mode_config.suggested_x_property, 0);
1107 drm_object_attach_property(&connector->base,
1108 dev->mode_config.suggested_y_property, 0);
1111 err_drm_connector_cleanup:
1112 drm_connector_cleanup(&qxl_output->base);
1117 static struct drm_framebuffer *
1118 qxl_user_framebuffer_create(struct drm_device *dev,
1119 struct drm_file *file_priv,
1120 const struct drm_mode_fb_cmd2 *mode_cmd)
1122 return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
1126 static const struct drm_mode_config_funcs qxl_mode_funcs = {
1127 .fb_create = qxl_user_framebuffer_create,
1128 .atomic_check = drm_atomic_helper_check,
1129 .atomic_commit = drm_atomic_helper_commit,
1132 int qxl_create_monitors_object(struct qxl_device *qdev)
1135 struct drm_gem_object *gobj;
1136 int monitors_config_size = sizeof(struct qxl_monitors_config) +
1137 qxl_num_crtc * sizeof(struct qxl_head);
1139 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1140 QXL_GEM_DOMAIN_VRAM,
1141 false, false, NULL, &gobj);
1143 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1146 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1148 ret = qxl_bo_pin(qdev->monitors_config_bo);
1152 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
1154 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1155 qdev->ram_header->monitors_config =
1156 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1158 memset(qdev->monitors_config, 0, monitors_config_size);
1159 qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
1161 if (!qdev->dumb_heads) {
1162 qxl_destroy_monitors_object(qdev);
1168 int qxl_destroy_monitors_object(struct qxl_device *qdev)
1172 qdev->monitors_config = NULL;
1173 qdev->ram_header->monitors_config = 0;
1175 qxl_bo_kunmap(qdev->monitors_config_bo);
1176 ret = qxl_bo_unpin(qdev->monitors_config_bo);
1180 qxl_bo_unref(&qdev->monitors_config_bo);
1184 int qxl_modeset_init(struct qxl_device *qdev)
1189 drm_mode_config_init(&qdev->ddev);
1191 ret = qxl_create_monitors_object(qdev);
1195 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1197 /* modes will be validated against the framebuffer size */
1198 qdev->ddev.mode_config.min_width = 0;
1199 qdev->ddev.mode_config.min_height = 0;
1200 qdev->ddev.mode_config.max_width = 8192;
1201 qdev->ddev.mode_config.max_height = 8192;
1203 qdev->ddev.mode_config.fb_base = qdev->vram_base;
1205 drm_mode_create_suggested_offset_properties(&qdev->ddev);
1206 qxl_mode_create_hotplug_mode_update_property(qdev);
1208 for (i = 0 ; i < qxl_num_crtc; ++i) {
1209 qdev_crtc_init(&qdev->ddev, i);
1210 qdev_output_init(&qdev->ddev, i);
1213 qxl_display_read_client_monitors_config(qdev);
1215 drm_mode_config_reset(&qdev->ddev);
1219 void qxl_modeset_fini(struct qxl_device *qdev)
1221 qxl_destroy_monitors_object(qdev);
1222 drm_mode_config_cleanup(&qdev->ddev);