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 <drm/drm_atomic.h>
28 #include <drm/drm_atomic_helper.h>
29 #include <drm/drm_gem_framebuffer_helper.h>
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_probe_helper.h>
34 #include "qxl_object.h"
36 static bool qxl_head_enabled(struct qxl_head *head)
38 return head->width && head->height;
41 static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
44 if (qdev->client_monitors_config &&
45 count > qdev->client_monitors_config->count) {
46 kfree(qdev->client_monitors_config);
47 qdev->client_monitors_config = NULL;
49 if (!qdev->client_monitors_config) {
50 qdev->client_monitors_config = kzalloc(
51 struct_size(qdev->client_monitors_config,
52 heads, count), GFP_KERNEL);
53 if (!qdev->client_monitors_config)
56 qdev->client_monitors_config->count = count;
61 MONITORS_CONFIG_MODIFIED,
62 MONITORS_CONFIG_UNCHANGED,
63 MONITORS_CONFIG_BAD_CRC,
64 MONITORS_CONFIG_ERROR,
67 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
72 int status = MONITORS_CONFIG_UNCHANGED;
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 return MONITORS_CONFIG_BAD_CRC;
80 DRM_DEBUG_KMS("no client monitors configured\n");
83 if (num_monitors > qxl_num_crtc) {
84 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
85 qxl_num_crtc, num_monitors);
86 num_monitors = qxl_num_crtc;
88 num_monitors = qdev->rom->client_monitors_config.count;
90 if (qdev->client_monitors_config
91 && (num_monitors != qdev->client_monitors_config->count)) {
92 status = MONITORS_CONFIG_MODIFIED;
94 if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
95 status = MONITORS_CONFIG_ERROR;
98 /* we copy max from the client but it isn't used */
99 qdev->client_monitors_config->max_allowed = qxl_num_crtc;
100 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
101 struct qxl_urect *c_rect =
102 &qdev->rom->client_monitors_config.heads[i];
103 struct qxl_head *client_head =
104 &qdev->client_monitors_config->heads[i];
105 if (client_head->x != c_rect->left) {
106 client_head->x = c_rect->left;
107 status = MONITORS_CONFIG_MODIFIED;
109 if (client_head->y != c_rect->top) {
110 client_head->y = c_rect->top;
111 status = MONITORS_CONFIG_MODIFIED;
113 if (client_head->width != c_rect->right - c_rect->left) {
114 client_head->width = c_rect->right - c_rect->left;
115 status = MONITORS_CONFIG_MODIFIED;
117 if (client_head->height != c_rect->bottom - c_rect->top) {
118 client_head->height = c_rect->bottom - c_rect->top;
119 status = MONITORS_CONFIG_MODIFIED;
121 if (client_head->surface_id != 0) {
122 client_head->surface_id = 0;
123 status = MONITORS_CONFIG_MODIFIED;
125 if (client_head->id != i) {
127 status = MONITORS_CONFIG_MODIFIED;
129 if (client_head->flags != 0) {
130 client_head->flags = 0;
131 status = MONITORS_CONFIG_MODIFIED;
133 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
134 client_head->x, client_head->y);
140 static void qxl_update_offset_props(struct qxl_device *qdev)
142 struct drm_device *dev = &qdev->ddev;
143 struct drm_connector *connector;
144 struct qxl_output *output;
145 struct qxl_head *head;
147 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
148 output = drm_connector_to_qxl_output(connector);
150 head = &qdev->client_monitors_config->heads[output->index];
152 drm_object_property_set_value(&connector->base,
153 dev->mode_config.suggested_x_property, head->x);
154 drm_object_property_set_value(&connector->base,
155 dev->mode_config.suggested_y_property, head->y);
159 void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
161 struct drm_device *dev = &qdev->ddev;
164 for (retries = 0; retries < 10; retries++) {
165 status = qxl_display_copy_rom_client_monitors_config(qdev);
166 if (status != MONITORS_CONFIG_BAD_CRC)
170 if (status == MONITORS_CONFIG_ERROR) {
171 DRM_DEBUG_KMS("ignoring client monitors config: error");
174 if (status == MONITORS_CONFIG_BAD_CRC) {
175 DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
178 if (status == MONITORS_CONFIG_UNCHANGED) {
179 DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
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);
193 static int qxl_check_mode(struct qxl_device *qdev,
200 if (check_mul_overflow(width, 4u, &stride))
202 if (check_mul_overflow(stride, height, &size))
204 if (size > qdev->vram_size)
209 static int qxl_check_framebuffer(struct qxl_device *qdev,
212 return qxl_check_mode(qdev, bo->surf.width, bo->surf.height);
215 static int qxl_add_mode(struct drm_connector *connector,
220 struct drm_device *dev = connector->dev;
221 struct qxl_device *qdev = dev->dev_private;
222 struct drm_display_mode *mode = NULL;
225 rc = qxl_check_mode(qdev, width, height);
229 mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
231 mode->type |= DRM_MODE_TYPE_PREFERRED;
232 mode->hdisplay = width;
233 mode->vdisplay = height;
234 drm_mode_set_name(mode);
235 drm_mode_probed_add(connector, mode);
239 static int qxl_add_monitors_config_modes(struct drm_connector *connector)
241 struct drm_device *dev = connector->dev;
242 struct qxl_device *qdev = dev->dev_private;
243 struct qxl_output *output = drm_connector_to_qxl_output(connector);
244 int h = output->index;
245 struct qxl_head *head;
247 if (!qdev->monitors_config)
249 if (h >= qxl_num_crtc)
251 if (!qdev->client_monitors_config)
253 if (h >= qdev->client_monitors_config->count)
256 head = &qdev->client_monitors_config->heads[h];
257 DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
259 return qxl_add_mode(connector, head->width, head->height, true);
262 static struct mode_size {
271 static int qxl_add_extra_modes(struct drm_connector *connector)
275 for (i = 0; i < ARRAY_SIZE(extra_modes); i++)
276 ret += qxl_add_mode(connector,
283 static void qxl_send_monitors_config(struct qxl_device *qdev)
287 BUG_ON(!qdev->ram_header->monitors_config);
289 if (qdev->monitors_config->count == 0)
292 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
293 struct qxl_head *head = &qdev->monitors_config->heads[i];
295 if (head->y > 8192 || head->x > 8192 ||
296 head->width > 8192 || head->height > 8192) {
297 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
298 i, head->width, head->height,
303 qxl_io_monitors_config(qdev);
306 static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
309 struct drm_device *dev = crtc->dev;
310 struct qxl_device *qdev = dev->dev_private;
311 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
312 struct qxl_head head;
313 int oldcount, i = qcrtc->index;
315 if (!qdev->primary_bo) {
316 DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
320 if (!qdev->monitors_config || qxl_num_crtc <= i)
325 oldcount = qdev->monitors_config->count;
326 if (crtc->state->active) {
327 struct drm_display_mode *mode = &crtc->mode;
329 head.width = mode->hdisplay;
330 head.height = mode->vdisplay;
333 if (qdev->monitors_config->count < i + 1)
334 qdev->monitors_config->count = i + 1;
335 if (qdev->primary_bo == qdev->dumb_shadow_bo)
336 head.x += qdev->dumb_heads[i].x;
342 if (qdev->monitors_config->count == i + 1)
343 qdev->monitors_config->count = i;
345 DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
349 if (head.width == qdev->monitors_config->heads[i].width &&
350 head.height == qdev->monitors_config->heads[i].height &&
351 head.x == qdev->monitors_config->heads[i].x &&
352 head.y == qdev->monitors_config->heads[i].y &&
353 oldcount == qdev->monitors_config->count)
356 DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
357 i, head.width, head.height, head.x, head.y,
358 crtc->state->active ? "on" : "off", reason);
359 if (oldcount != qdev->monitors_config->count)
360 DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
361 oldcount, qdev->monitors_config->count,
364 qdev->monitors_config->heads[i] = head;
365 qdev->monitors_config->max_allowed = qxl_num_crtc;
366 qxl_send_monitors_config(qdev);
369 static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
370 struct drm_crtc_state *old_crtc_state)
372 struct drm_device *dev = crtc->dev;
373 struct drm_pending_vblank_event *event;
376 if (crtc->state && crtc->state->event) {
377 event = crtc->state->event;
378 crtc->state->event = NULL;
380 spin_lock_irqsave(&dev->event_lock, flags);
381 drm_crtc_send_vblank_event(crtc, event);
382 spin_unlock_irqrestore(&dev->event_lock, flags);
385 qxl_crtc_update_monitors_config(crtc, "flush");
388 static void qxl_crtc_destroy(struct drm_crtc *crtc)
390 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
392 qxl_bo_unref(&qxl_crtc->cursor_bo);
393 drm_crtc_cleanup(crtc);
397 static const struct drm_crtc_funcs qxl_crtc_funcs = {
398 .set_config = drm_atomic_helper_set_config,
399 .destroy = qxl_crtc_destroy,
400 .page_flip = drm_atomic_helper_page_flip,
401 .reset = drm_atomic_helper_crtc_reset,
402 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
403 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
406 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
407 struct drm_file *file_priv,
408 unsigned int flags, unsigned int color,
409 struct drm_clip_rect *clips,
410 unsigned int num_clips)
412 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
413 struct qxl_device *qdev = fb->dev->dev_private;
414 struct drm_clip_rect norect;
419 drm_modeset_lock_all(fb->dev);
421 qobj = gem_to_qxl_bo(fb->obj[0]);
422 /* if we aren't primary surface ignore this */
423 is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
425 drm_modeset_unlock_all(fb->dev);
432 norect.x1 = norect.y1 = 0;
433 norect.x2 = fb->width;
434 norect.y2 = fb->height;
435 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
437 inc = 2; /* skip source rects */
440 qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
441 clips, num_clips, inc, 0);
443 drm_modeset_unlock_all(fb->dev);
448 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
449 .destroy = drm_gem_fb_destroy,
450 .dirty = qxl_framebuffer_surface_dirty,
451 .create_handle = drm_gem_fb_create_handle,
454 static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
455 struct drm_crtc_state *old_state)
457 qxl_crtc_update_monitors_config(crtc, "enable");
460 static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
461 struct drm_crtc_state *old_state)
463 qxl_crtc_update_monitors_config(crtc, "disable");
466 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
467 .atomic_flush = qxl_crtc_atomic_flush,
468 .atomic_enable = qxl_crtc_atomic_enable,
469 .atomic_disable = qxl_crtc_atomic_disable,
472 static int qxl_primary_atomic_check(struct drm_plane *plane,
473 struct drm_plane_state *state)
475 struct qxl_device *qdev = plane->dev->dev_private;
478 if (!state->crtc || !state->fb)
481 bo = gem_to_qxl_bo(state->fb->obj[0]);
483 return qxl_check_framebuffer(qdev, bo);
486 static int qxl_primary_apply_cursor(struct drm_plane *plane)
488 struct drm_device *dev = plane->dev;
489 struct qxl_device *qdev = dev->dev_private;
490 struct drm_framebuffer *fb = plane->state->fb;
491 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
492 struct qxl_cursor_cmd *cmd;
493 struct qxl_release *release;
496 if (!qcrtc->cursor_bo)
499 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
500 QXL_RELEASE_CURSOR_CMD,
505 ret = qxl_release_list_add(release, qcrtc->cursor_bo);
507 goto out_free_release;
509 ret = qxl_release_reserve_list(release, false);
511 goto out_free_release;
513 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
514 cmd->type = QXL_CURSOR_SET;
515 cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
516 cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
518 cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
520 cmd->u.set.visible = 1;
521 qxl_release_unmap(qdev, release, &cmd->release_info);
523 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
524 qxl_release_fence_buffer_objects(release);
529 qxl_release_free(qdev, release);
533 static void qxl_primary_atomic_update(struct drm_plane *plane,
534 struct drm_plane_state *old_state)
536 struct qxl_device *qdev = plane->dev->dev_private;
537 struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]);
538 struct qxl_bo *primary;
539 struct drm_clip_rect norect = {
542 .x2 = plane->state->fb->width,
543 .y2 = plane->state->fb->height
545 uint32_t dumb_shadow_offset = 0;
547 primary = bo->shadow ? bo->shadow : bo;
549 if (!primary->is_primary) {
550 if (qdev->primary_bo)
551 qxl_io_destroy_primary(qdev);
552 qxl_io_create_primary(qdev, primary);
553 qxl_primary_apply_cursor(plane);
558 qdev->dumb_heads[plane->state->crtc->index].x;
560 qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1,
564 static void qxl_primary_atomic_disable(struct drm_plane *plane,
565 struct drm_plane_state *old_state)
567 struct qxl_device *qdev = plane->dev->dev_private;
570 struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
572 if (bo->is_primary) {
573 qxl_io_destroy_primary(qdev);
574 bo->is_primary = false;
579 static void qxl_cursor_atomic_update(struct drm_plane *plane,
580 struct drm_plane_state *old_state)
582 struct drm_device *dev = plane->dev;
583 struct qxl_device *qdev = dev->dev_private;
584 struct drm_framebuffer *fb = plane->state->fb;
585 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
586 struct qxl_release *release;
587 struct qxl_cursor_cmd *cmd;
588 struct qxl_cursor *cursor;
589 struct drm_gem_object *obj;
590 struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
595 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
596 QXL_RELEASE_CURSOR_CMD,
601 if (fb != old_state->fb) {
603 user_bo = gem_to_qxl_bo(obj);
605 /* pinning is done in the prepare/cleanup framevbuffer */
606 ret = qxl_bo_kmap(user_bo, &user_ptr);
608 goto out_free_release;
610 ret = qxl_alloc_bo_reserved(qdev, release,
611 sizeof(struct qxl_cursor) + size,
616 ret = qxl_bo_pin(cursor_bo);
620 ret = qxl_release_reserve_list(release, true);
624 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
628 cursor->header.unique = 0;
629 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
630 cursor->header.width = 64;
631 cursor->header.height = 64;
632 cursor->header.hot_spot_x = fb->hot_x;
633 cursor->header.hot_spot_y = fb->hot_y;
634 cursor->data_size = size;
635 cursor->chunk.next_chunk = 0;
636 cursor->chunk.prev_chunk = 0;
637 cursor->chunk.data_size = size;
638 memcpy(cursor->chunk.data, user_ptr, size);
639 qxl_bo_kunmap(cursor_bo);
640 qxl_bo_kunmap(user_bo);
642 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
643 cmd->u.set.visible = 1;
644 cmd->u.set.shape = qxl_bo_physical_address(qdev,
646 cmd->type = QXL_CURSOR_SET;
648 old_cursor_bo = qcrtc->cursor_bo;
649 qcrtc->cursor_bo = cursor_bo;
653 ret = qxl_release_reserve_list(release, true);
655 goto out_free_release;
657 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
658 cmd->type = QXL_CURSOR_MOVE;
661 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
662 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
664 qxl_release_unmap(qdev, release, &cmd->release_info);
665 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
666 qxl_release_fence_buffer_objects(release);
668 if (old_cursor_bo != NULL)
669 qxl_bo_unpin(old_cursor_bo);
670 qxl_bo_unref(&old_cursor_bo);
671 qxl_bo_unref(&cursor_bo);
676 qxl_release_backoff_reserve_list(release);
678 qxl_bo_unpin(cursor_bo);
680 qxl_bo_unref(&cursor_bo);
682 qxl_bo_kunmap(user_bo);
684 qxl_release_free(qdev, release);
689 static void qxl_cursor_atomic_disable(struct drm_plane *plane,
690 struct drm_plane_state *old_state)
692 struct qxl_device *qdev = plane->dev->dev_private;
693 struct qxl_release *release;
694 struct qxl_cursor_cmd *cmd;
697 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
698 QXL_RELEASE_CURSOR_CMD,
703 ret = qxl_release_reserve_list(release, true);
705 qxl_release_free(qdev, release);
709 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
710 cmd->type = QXL_CURSOR_HIDE;
711 qxl_release_unmap(qdev, release, &cmd->release_info);
713 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
714 qxl_release_fence_buffer_objects(release);
717 static void qxl_update_dumb_head(struct qxl_device *qdev,
718 int index, struct qxl_bo *bo)
720 uint32_t width, height;
722 if (index >= qdev->monitors_config->max_allowed)
725 if (bo && bo->is_dumb) {
726 width = bo->surf.width;
727 height = bo->surf.height;
733 if (qdev->dumb_heads[index].width == width &&
734 qdev->dumb_heads[index].height == height)
737 DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
738 qdev->dumb_heads[index].width,
739 qdev->dumb_heads[index].height,
741 qdev->dumb_heads[index].width = width;
742 qdev->dumb_heads[index].height = height;
745 static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
746 struct qxl_surface *surf)
748 struct qxl_head *head;
751 memset(surf, 0, sizeof(*surf));
752 for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
753 head = qdev->dumb_heads + i;
754 head->x = surf->width;
755 surf->width += head->width;
756 if (surf->height < head->height)
757 surf->height = head->height;
759 if (surf->width < 64)
761 if (surf->height < 64)
763 surf->format = SPICE_SURFACE_FMT_32_xRGB;
764 surf->stride = surf->width * 4;
766 if (!qdev->dumb_shadow_bo ||
767 qdev->dumb_shadow_bo->surf.width != surf->width ||
768 qdev->dumb_shadow_bo->surf.height != surf->height)
769 DRM_DEBUG("%dx%d\n", surf->width, surf->height);
772 static int qxl_plane_prepare_fb(struct drm_plane *plane,
773 struct drm_plane_state *new_state)
775 struct qxl_device *qdev = plane->dev->dev_private;
776 struct drm_gem_object *obj;
777 struct qxl_bo *user_bo;
778 struct qxl_surface surf;
784 obj = new_state->fb->obj[0];
785 user_bo = gem_to_qxl_bo(obj);
787 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
789 qxl_update_dumb_head(qdev, new_state->crtc->index,
791 qxl_calc_dumb_shadow(qdev, &surf);
792 if (!qdev->dumb_shadow_bo ||
793 qdev->dumb_shadow_bo->surf.width != surf.width ||
794 qdev->dumb_shadow_bo->surf.height != surf.height) {
795 if (qdev->dumb_shadow_bo) {
796 drm_gem_object_put_unlocked
797 (&qdev->dumb_shadow_bo->gem_base);
798 qdev->dumb_shadow_bo = NULL;
800 qxl_bo_create(qdev, surf.height * surf.stride,
801 true, true, QXL_GEM_DOMAIN_SURFACE, &surf,
802 &qdev->dumb_shadow_bo);
804 if (user_bo->shadow != qdev->dumb_shadow_bo) {
805 if (user_bo->shadow) {
806 drm_gem_object_put_unlocked
807 (&user_bo->shadow->gem_base);
808 user_bo->shadow = NULL;
810 drm_gem_object_get(&qdev->dumb_shadow_bo->gem_base);
811 user_bo->shadow = qdev->dumb_shadow_bo;
815 ret = qxl_bo_pin(user_bo);
822 static void qxl_plane_cleanup_fb(struct drm_plane *plane,
823 struct drm_plane_state *old_state)
825 struct drm_gem_object *obj;
826 struct qxl_bo *user_bo;
828 if (!old_state->fb) {
830 * we never executed prepare_fb, so there's nothing to
836 obj = old_state->fb->obj[0];
837 user_bo = gem_to_qxl_bo(obj);
838 qxl_bo_unpin(user_bo);
840 if (old_state->fb != plane->state->fb && user_bo->shadow) {
841 drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
842 user_bo->shadow = NULL;
846 static const uint32_t qxl_cursor_plane_formats[] = {
850 static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
851 .atomic_update = qxl_cursor_atomic_update,
852 .atomic_disable = qxl_cursor_atomic_disable,
853 .prepare_fb = qxl_plane_prepare_fb,
854 .cleanup_fb = qxl_plane_cleanup_fb,
857 static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
858 .update_plane = drm_atomic_helper_update_plane,
859 .disable_plane = drm_atomic_helper_disable_plane,
860 .destroy = drm_primary_helper_destroy,
861 .reset = drm_atomic_helper_plane_reset,
862 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
863 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
866 static const uint32_t qxl_primary_plane_formats[] = {
871 static const struct drm_plane_helper_funcs primary_helper_funcs = {
872 .atomic_check = qxl_primary_atomic_check,
873 .atomic_update = qxl_primary_atomic_update,
874 .atomic_disable = qxl_primary_atomic_disable,
875 .prepare_fb = qxl_plane_prepare_fb,
876 .cleanup_fb = qxl_plane_cleanup_fb,
879 static const struct drm_plane_funcs qxl_primary_plane_funcs = {
880 .update_plane = drm_atomic_helper_update_plane,
881 .disable_plane = drm_atomic_helper_disable_plane,
882 .destroy = drm_primary_helper_destroy,
883 .reset = drm_atomic_helper_plane_reset,
884 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
885 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
888 static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
889 unsigned int possible_crtcs,
890 enum drm_plane_type type)
892 const struct drm_plane_helper_funcs *helper_funcs = NULL;
893 struct drm_plane *plane;
894 const struct drm_plane_funcs *funcs;
895 const uint32_t *formats;
899 if (type == DRM_PLANE_TYPE_PRIMARY) {
900 funcs = &qxl_primary_plane_funcs;
901 formats = qxl_primary_plane_formats;
902 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
903 helper_funcs = &primary_helper_funcs;
904 } else if (type == DRM_PLANE_TYPE_CURSOR) {
905 funcs = &qxl_cursor_plane_funcs;
906 formats = qxl_cursor_plane_formats;
907 helper_funcs = &qxl_cursor_helper_funcs;
908 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
910 return ERR_PTR(-EINVAL);
913 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
915 return ERR_PTR(-ENOMEM);
917 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
918 funcs, formats, num_formats,
923 drm_plane_helper_add(plane, helper_funcs);
929 return ERR_PTR(-EINVAL);
932 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
934 struct qxl_crtc *qxl_crtc;
935 struct drm_plane *primary, *cursor;
936 struct qxl_device *qdev = dev->dev_private;
939 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
943 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
944 if (IS_ERR(primary)) {
949 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
950 if (IS_ERR(cursor)) {
955 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
956 &qxl_crtc_funcs, NULL);
960 qxl_crtc->index = crtc_id;
961 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
965 drm_plane_cleanup(cursor);
968 drm_plane_cleanup(primary);
975 static int qxl_conn_get_modes(struct drm_connector *connector)
977 struct drm_device *dev = connector->dev;
978 struct qxl_device *qdev = dev->dev_private;
979 struct qxl_output *output = drm_connector_to_qxl_output(connector);
980 unsigned int pwidth = 1024;
981 unsigned int pheight = 768;
984 if (qdev->client_monitors_config) {
985 struct qxl_head *head;
986 head = &qdev->client_monitors_config->heads[output->index];
988 pwidth = head->width;
990 pheight = head->height;
993 ret += drm_add_modes_noedid(connector, 8192, 8192);
994 ret += qxl_add_extra_modes(connector);
995 ret += qxl_add_monitors_config_modes(connector);
996 drm_set_preferred_mode(connector, pwidth, pheight);
1000 static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
1001 struct drm_display_mode *mode)
1003 struct drm_device *ddev = connector->dev;
1004 struct qxl_device *qdev = ddev->dev_private;
1006 if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
1012 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1014 struct qxl_output *qxl_output =
1015 drm_connector_to_qxl_output(connector);
1018 return &qxl_output->enc;
1021 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1024 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1025 .get_modes = qxl_conn_get_modes,
1026 .mode_valid = qxl_conn_mode_valid,
1027 .best_encoder = qxl_best_encoder,
1030 static enum drm_connector_status qxl_conn_detect(
1031 struct drm_connector *connector,
1034 struct qxl_output *output =
1035 drm_connector_to_qxl_output(connector);
1036 struct drm_device *ddev = connector->dev;
1037 struct qxl_device *qdev = ddev->dev_private;
1038 bool connected = false;
1040 /* The first monitor is always connected */
1041 if (!qdev->client_monitors_config) {
1042 if (output->index == 0)
1045 connected = qdev->client_monitors_config->count > output->index &&
1046 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1048 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1050 return connected ? connector_status_connected
1051 : connector_status_disconnected;
1054 static void qxl_conn_destroy(struct drm_connector *connector)
1056 struct qxl_output *qxl_output =
1057 drm_connector_to_qxl_output(connector);
1059 drm_connector_unregister(connector);
1060 drm_connector_cleanup(connector);
1064 static const struct drm_connector_funcs qxl_connector_funcs = {
1065 .detect = qxl_conn_detect,
1066 .fill_modes = drm_helper_probe_single_connector_modes,
1067 .destroy = qxl_conn_destroy,
1068 .reset = drm_atomic_helper_connector_reset,
1069 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1070 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1073 static void qxl_enc_destroy(struct drm_encoder *encoder)
1075 drm_encoder_cleanup(encoder);
1078 static const struct drm_encoder_funcs qxl_enc_funcs = {
1079 .destroy = qxl_enc_destroy,
1082 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1084 if (qdev->hotplug_mode_update_property)
1087 qdev->hotplug_mode_update_property =
1088 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1089 "hotplug_mode_update", 0, 1);
1094 static int qdev_output_init(struct drm_device *dev, int num_output)
1096 struct qxl_device *qdev = dev->dev_private;
1097 struct qxl_output *qxl_output;
1098 struct drm_connector *connector;
1099 struct drm_encoder *encoder;
1101 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1105 qxl_output->index = num_output;
1107 connector = &qxl_output->base;
1108 encoder = &qxl_output->enc;
1109 drm_connector_init(dev, &qxl_output->base,
1110 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1112 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
1113 DRM_MODE_ENCODER_VIRTUAL, NULL);
1115 /* we get HPD via client monitors config */
1116 connector->polled = DRM_CONNECTOR_POLL_HPD;
1117 encoder->possible_crtcs = 1 << num_output;
1118 drm_connector_attach_encoder(&qxl_output->base,
1120 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1121 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1123 drm_object_attach_property(&connector->base,
1124 qdev->hotplug_mode_update_property, 0);
1125 drm_object_attach_property(&connector->base,
1126 dev->mode_config.suggested_x_property, 0);
1127 drm_object_attach_property(&connector->base,
1128 dev->mode_config.suggested_y_property, 0);
1132 static struct drm_framebuffer *
1133 qxl_user_framebuffer_create(struct drm_device *dev,
1134 struct drm_file *file_priv,
1135 const struct drm_mode_fb_cmd2 *mode_cmd)
1137 return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
1141 static const struct drm_mode_config_funcs qxl_mode_funcs = {
1142 .fb_create = qxl_user_framebuffer_create,
1143 .atomic_check = drm_atomic_helper_check,
1144 .atomic_commit = drm_atomic_helper_commit,
1147 int qxl_create_monitors_object(struct qxl_device *qdev)
1150 struct drm_gem_object *gobj;
1151 int monitors_config_size = sizeof(struct qxl_monitors_config) +
1152 qxl_num_crtc * sizeof(struct qxl_head);
1154 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1155 QXL_GEM_DOMAIN_VRAM,
1156 false, false, NULL, &gobj);
1158 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1161 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1163 ret = qxl_bo_pin(qdev->monitors_config_bo);
1167 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
1169 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1170 qdev->ram_header->monitors_config =
1171 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1173 memset(qdev->monitors_config, 0, monitors_config_size);
1174 qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
1176 if (!qdev->dumb_heads) {
1177 qxl_destroy_monitors_object(qdev);
1183 int qxl_destroy_monitors_object(struct qxl_device *qdev)
1187 qdev->monitors_config = NULL;
1188 qdev->ram_header->monitors_config = 0;
1190 qxl_bo_kunmap(qdev->monitors_config_bo);
1191 ret = qxl_bo_unpin(qdev->monitors_config_bo);
1195 qxl_bo_unref(&qdev->monitors_config_bo);
1199 int qxl_modeset_init(struct qxl_device *qdev)
1204 drm_mode_config_init(&qdev->ddev);
1206 ret = qxl_create_monitors_object(qdev);
1210 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1212 /* modes will be validated against the framebuffer size */
1213 qdev->ddev.mode_config.min_width = 0;
1214 qdev->ddev.mode_config.min_height = 0;
1215 qdev->ddev.mode_config.max_width = 8192;
1216 qdev->ddev.mode_config.max_height = 8192;
1218 qdev->ddev.mode_config.fb_base = qdev->vram_base;
1220 drm_mode_create_suggested_offset_properties(&qdev->ddev);
1221 qxl_mode_create_hotplug_mode_update_property(qdev);
1223 for (i = 0 ; i < qxl_num_crtc; ++i) {
1224 qdev_crtc_init(&qdev->ddev, i);
1225 qdev_output_init(&qdev->ddev, i);
1228 qxl_display_read_client_monitors_config(qdev);
1230 drm_mode_config_reset(&qdev->ddev);
1234 void qxl_modeset_fini(struct qxl_device *qdev)
1236 qxl_destroy_monitors_object(qdev);
1237 drm_mode_config_cleanup(&qdev->ddev);