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
27 #include <linux/crc32.h>
30 #include "qxl_object.h"
31 #include "drm_crtc_helper.h"
32 #include <drm/drm_plane_helper.h>
34 static bool qxl_head_enabled(struct qxl_head *head)
36 return head->width && head->height;
39 static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
41 if (qdev->client_monitors_config &&
42 count > qdev->client_monitors_config->count) {
43 kfree(qdev->client_monitors_config);
44 qdev->client_monitors_config = NULL;
46 if (!qdev->client_monitors_config) {
47 qdev->client_monitors_config = kzalloc(
48 sizeof(struct qxl_monitors_config) +
49 sizeof(struct qxl_head) * count, GFP_KERNEL);
50 if (!qdev->client_monitors_config) {
52 "%s: allocation failure for %u heads\n",
57 qdev->client_monitors_config->count = count;
61 MONITORS_CONFIG_MODIFIED,
62 MONITORS_CONFIG_UNCHANGED,
63 MONITORS_CONFIG_BAD_CRC,
66 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
71 int status = MONITORS_CONFIG_UNCHANGED;
73 num_monitors = qdev->rom->client_monitors_config.count;
74 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
75 sizeof(qdev->rom->client_monitors_config));
76 if (crc != qdev->rom->client_monitors_config_crc) {
77 qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
78 sizeof(qdev->rom->client_monitors_config),
79 qdev->rom->client_monitors_config_crc);
80 return MONITORS_CONFIG_BAD_CRC;
82 if (num_monitors > qdev->monitors_config->max_allowed) {
83 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
84 qdev->monitors_config->max_allowed, num_monitors);
85 num_monitors = qdev->monitors_config->max_allowed;
87 num_monitors = qdev->rom->client_monitors_config.count;
89 if (qdev->client_monitors_config
90 && (num_monitors != qdev->client_monitors_config->count)) {
91 status = MONITORS_CONFIG_MODIFIED;
93 qxl_alloc_client_monitors_config(qdev, num_monitors);
94 /* we copy max from the client but it isn't used */
95 qdev->client_monitors_config->max_allowed =
96 qdev->monitors_config->max_allowed;
97 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
98 struct qxl_urect *c_rect =
99 &qdev->rom->client_monitors_config.heads[i];
100 struct qxl_head *client_head =
101 &qdev->client_monitors_config->heads[i];
102 if (client_head->x != c_rect->left) {
103 client_head->x = c_rect->left;
104 status = MONITORS_CONFIG_MODIFIED;
106 if (client_head->y != c_rect->top) {
107 client_head->y = c_rect->top;
108 status = MONITORS_CONFIG_MODIFIED;
110 if (client_head->width != c_rect->right - c_rect->left) {
111 client_head->width = c_rect->right - c_rect->left;
112 status = MONITORS_CONFIG_MODIFIED;
114 if (client_head->height != c_rect->bottom - c_rect->top) {
115 client_head->height = c_rect->bottom - c_rect->top;
116 status = MONITORS_CONFIG_MODIFIED;
118 if (client_head->surface_id != 0) {
119 client_head->surface_id = 0;
120 status = MONITORS_CONFIG_MODIFIED;
122 if (client_head->id != i) {
124 status = MONITORS_CONFIG_MODIFIED;
126 if (client_head->flags != 0) {
127 client_head->flags = 0;
128 status = MONITORS_CONFIG_MODIFIED;
130 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
131 client_head->x, client_head->y);
137 static void qxl_update_offset_props(struct qxl_device *qdev)
139 struct drm_device *dev = qdev->ddev;
140 struct drm_connector *connector;
141 struct qxl_output *output;
142 struct qxl_head *head;
144 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
145 output = drm_connector_to_qxl_output(connector);
147 head = &qdev->client_monitors_config->heads[output->index];
149 drm_object_property_set_value(&connector->base,
150 dev->mode_config.suggested_x_property, head->x);
151 drm_object_property_set_value(&connector->base,
152 dev->mode_config.suggested_y_property, head->y);
156 void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
159 struct drm_device *dev = qdev->ddev;
162 status = qxl_display_copy_rom_client_monitors_config(qdev);
163 while (status == MONITORS_CONFIG_BAD_CRC) {
164 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
166 status = qxl_display_copy_rom_client_monitors_config(qdev);
168 if (status == MONITORS_CONFIG_UNCHANGED) {
169 qxl_io_log(qdev, "config unchanged\n");
170 DRM_DEBUG("ignoring unchanged client monitors config");
174 drm_modeset_lock_all(dev);
175 qxl_update_offset_props(qdev);
176 drm_modeset_unlock_all(dev);
177 if (!drm_helper_hpd_irq_event(qdev->ddev)) {
178 /* notify that the monitor configuration changed, to
179 adjust at the arbitrary resolution */
180 drm_kms_helper_hotplug_event(qdev->ddev);
184 static int qxl_add_monitors_config_modes(struct drm_connector *connector,
188 struct drm_device *dev = connector->dev;
189 struct qxl_device *qdev = dev->dev_private;
190 struct qxl_output *output = drm_connector_to_qxl_output(connector);
191 int h = output->index;
192 struct drm_display_mode *mode = NULL;
193 struct qxl_head *head;
195 if (!qdev->client_monitors_config)
197 head = &qdev->client_monitors_config->heads[h];
199 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
201 mode->type |= DRM_MODE_TYPE_PREFERRED;
202 mode->hdisplay = head->width;
203 mode->vdisplay = head->height;
204 drm_mode_set_name(mode);
205 *pwidth = head->width;
206 *pheight = head->height;
207 drm_mode_probed_add(connector, mode);
208 /* remember the last custom size for mode validation */
209 qdev->monitors_config_width = mode->hdisplay;
210 qdev->monitors_config_height = mode->vdisplay;
214 static struct mode_size {
237 static int qxl_add_common_modes(struct drm_connector *connector,
241 struct drm_device *dev = connector->dev;
242 struct drm_display_mode *mode = NULL;
244 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
245 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
246 60, false, false, false);
247 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
248 mode->type |= DRM_MODE_TYPE_PREFERRED;
249 drm_mode_probed_add(connector, mode);
254 static void qxl_crtc_destroy(struct drm_crtc *crtc)
256 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
258 drm_crtc_cleanup(crtc);
259 qxl_bo_unref(&qxl_crtc->cursor_bo);
263 static int qxl_crtc_page_flip(struct drm_crtc *crtc,
264 struct drm_framebuffer *fb,
265 struct drm_pending_vblank_event *event,
266 uint32_t page_flip_flags)
268 struct drm_device *dev = crtc->dev;
269 struct qxl_device *qdev = dev->dev_private;
270 struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
271 struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
272 struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
273 struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
275 struct drm_clip_rect norect = {
282 int one_clip_rect = 1;
285 crtc->primary->fb = fb;
286 bo_old->is_primary = false;
287 bo->is_primary = true;
289 ret = qxl_bo_reserve(bo, false);
292 ret = qxl_bo_pin(bo, bo->type, NULL);
293 qxl_bo_unreserve(bo);
297 qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
298 &norect, one_clip_rect, inc);
300 drm_crtc_vblank_get(crtc);
303 spin_lock_irqsave(&dev->event_lock, flags);
304 drm_crtc_send_vblank_event(crtc, event);
305 spin_unlock_irqrestore(&dev->event_lock, flags);
307 drm_crtc_vblank_put(crtc);
309 ret = qxl_bo_reserve(bo, false);
312 qxl_bo_unreserve(bo);
319 qxl_hide_cursor(struct qxl_device *qdev)
321 struct qxl_release *release;
322 struct qxl_cursor_cmd *cmd;
325 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
330 ret = qxl_release_reserve_list(release, true);
332 qxl_release_free(qdev, release);
336 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
337 cmd->type = QXL_CURSOR_HIDE;
338 qxl_release_unmap(qdev, release, &cmd->release_info);
340 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
341 qxl_release_fence_buffer_objects(release);
345 static int qxl_crtc_apply_cursor(struct drm_crtc *crtc)
347 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
348 struct drm_device *dev = crtc->dev;
349 struct qxl_device *qdev = dev->dev_private;
350 struct qxl_cursor_cmd *cmd;
351 struct qxl_release *release;
354 if (!qcrtc->cursor_bo)
357 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
358 QXL_RELEASE_CURSOR_CMD,
363 ret = qxl_release_list_add(release, qcrtc->cursor_bo);
365 goto out_free_release;
367 ret = qxl_release_reserve_list(release, false);
369 goto out_free_release;
371 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
372 cmd->type = QXL_CURSOR_SET;
373 cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
374 cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
376 cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
378 cmd->u.set.visible = 1;
379 qxl_release_unmap(qdev, release, &cmd->release_info);
381 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
382 qxl_release_fence_buffer_objects(release);
387 qxl_release_free(qdev, release);
391 static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
392 struct drm_file *file_priv,
395 uint32_t height, int32_t hot_x, int32_t hot_y)
397 struct drm_device *dev = crtc->dev;
398 struct qxl_device *qdev = dev->dev_private;
399 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
400 struct drm_gem_object *obj;
401 struct qxl_cursor *cursor;
402 struct qxl_cursor_cmd *cmd;
403 struct qxl_bo *cursor_bo, *user_bo;
404 struct qxl_release *release;
410 return qxl_hide_cursor(qdev);
412 obj = drm_gem_object_lookup(file_priv, handle);
414 DRM_ERROR("cannot find cursor object\n");
418 user_bo = gem_to_qxl_bo(obj);
420 ret = qxl_bo_reserve(user_bo, false);
424 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
425 qxl_bo_unreserve(user_bo);
429 ret = qxl_bo_kmap(user_bo, &user_ptr);
433 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
434 QXL_RELEASE_CURSOR_CMD,
439 ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size,
442 goto out_free_release;
444 ret = qxl_release_reserve_list(release, false);
448 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
452 cursor->header.unique = 0;
453 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
454 cursor->header.width = 64;
455 cursor->header.height = 64;
456 cursor->header.hot_spot_x = hot_x;
457 cursor->header.hot_spot_y = hot_y;
458 cursor->data_size = size;
459 cursor->chunk.next_chunk = 0;
460 cursor->chunk.prev_chunk = 0;
461 cursor->chunk.data_size = size;
463 memcpy(cursor->chunk.data, user_ptr, size);
465 qxl_bo_kunmap(cursor_bo);
467 qxl_bo_kunmap(user_bo);
469 qcrtc->cur_x += qcrtc->hot_spot_x - hot_x;
470 qcrtc->cur_y += qcrtc->hot_spot_y - hot_y;
471 qcrtc->hot_spot_x = hot_x;
472 qcrtc->hot_spot_y = hot_y;
474 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
475 cmd->type = QXL_CURSOR_SET;
476 cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
477 cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
479 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
481 cmd->u.set.visible = 1;
482 qxl_release_unmap(qdev, release, &cmd->release_info);
484 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
485 qxl_release_fence_buffer_objects(release);
487 /* finish with the userspace bo */
488 ret = qxl_bo_reserve(user_bo, false);
490 qxl_bo_unpin(user_bo);
491 qxl_bo_unreserve(user_bo);
493 drm_gem_object_unreference_unlocked(obj);
495 qxl_bo_unref (&qcrtc->cursor_bo);
496 qcrtc->cursor_bo = cursor_bo;
501 qxl_release_backoff_reserve_list(release);
503 qxl_bo_unref(&cursor_bo);
505 qxl_release_free(qdev, release);
507 qxl_bo_kunmap(user_bo);
509 qxl_bo_unpin(user_bo);
511 drm_gem_object_unreference_unlocked(obj);
515 static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
518 struct drm_device *dev = crtc->dev;
519 struct qxl_device *qdev = dev->dev_private;
520 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
521 struct qxl_release *release;
522 struct qxl_cursor_cmd *cmd;
525 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
530 ret = qxl_release_reserve_list(release, true);
532 qxl_release_free(qdev, release);
539 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
540 cmd->type = QXL_CURSOR_MOVE;
541 cmd->u.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
542 cmd->u.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
543 qxl_release_unmap(qdev, release, &cmd->release_info);
545 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
546 qxl_release_fence_buffer_objects(release);
552 static const struct drm_crtc_funcs qxl_crtc_funcs = {
553 .cursor_set2 = qxl_crtc_cursor_set2,
554 .cursor_move = qxl_crtc_cursor_move,
555 .set_config = drm_crtc_helper_set_config,
556 .destroy = qxl_crtc_destroy,
557 .page_flip = qxl_crtc_page_flip,
560 void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
562 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
564 drm_gem_object_unreference_unlocked(qxl_fb->obj);
565 drm_framebuffer_cleanup(fb);
569 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
570 struct drm_file *file_priv,
571 unsigned flags, unsigned color,
572 struct drm_clip_rect *clips,
575 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
576 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
577 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
578 struct drm_clip_rect norect;
582 drm_modeset_lock_all(fb->dev);
584 qobj = gem_to_qxl_bo(qxl_fb->obj);
585 /* if we aren't primary surface ignore this */
586 if (!qobj->is_primary) {
587 drm_modeset_unlock_all(fb->dev);
594 norect.x1 = norect.y1 = 0;
595 norect.x2 = fb->width;
596 norect.y2 = fb->height;
597 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
599 inc = 2; /* skip source rects */
602 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
603 clips, num_clips, inc);
605 drm_modeset_unlock_all(fb->dev);
610 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
611 .destroy = qxl_user_framebuffer_destroy,
612 .dirty = qxl_framebuffer_surface_dirty,
614 * .create_handle = qxl_user_framebuffer_create_handle, */
618 qxl_framebuffer_init(struct drm_device *dev,
619 struct qxl_framebuffer *qfb,
620 const struct drm_mode_fb_cmd2 *mode_cmd,
621 struct drm_gem_object *obj,
622 const struct drm_framebuffer_funcs *funcs)
627 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
632 drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
636 static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
640 static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
641 const struct drm_display_mode *mode,
642 struct drm_display_mode *adjusted_mode)
644 struct drm_device *dev = crtc->dev;
645 struct qxl_device *qdev = dev->dev_private;
647 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
649 mode->hdisplay, mode->vdisplay,
650 adjusted_mode->hdisplay,
651 adjusted_mode->vdisplay);
656 qxl_send_monitors_config(struct qxl_device *qdev)
660 BUG_ON(!qdev->ram_header->monitors_config);
662 if (qdev->monitors_config->count == 0) {
663 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
666 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
667 struct qxl_head *head = &qdev->monitors_config->heads[i];
669 if (head->y > 8192 || head->x > 8192 ||
670 head->width > 8192 || head->height > 8192) {
671 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
672 i, head->width, head->height,
677 qxl_io_monitors_config(qdev);
680 static void qxl_monitors_config_set(struct qxl_device *qdev,
682 unsigned x, unsigned y,
683 unsigned width, unsigned height,
686 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
687 qdev->monitors_config->heads[index].x = x;
688 qdev->monitors_config->heads[index].y = y;
689 qdev->monitors_config->heads[index].width = width;
690 qdev->monitors_config->heads[index].height = height;
691 qdev->monitors_config->heads[index].surface_id = surf_id;
695 static int qxl_crtc_mode_set(struct drm_crtc *crtc,
696 struct drm_display_mode *mode,
697 struct drm_display_mode *adjusted_mode,
699 struct drm_framebuffer *old_fb)
701 struct drm_device *dev = crtc->dev;
702 struct qxl_device *qdev = dev->dev_private;
703 struct qxl_framebuffer *qfb;
704 struct qxl_bo *bo, *old_bo = NULL;
705 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
706 bool recreate_primary = false;
709 if (!crtc->primary->fb) {
710 DRM_DEBUG_KMS("No FB bound\n");
715 qfb = to_qxl_framebuffer(old_fb);
716 old_bo = gem_to_qxl_bo(qfb->obj);
718 qfb = to_qxl_framebuffer(crtc->primary->fb);
719 bo = gem_to_qxl_bo(qfb->obj);
720 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
722 mode->hdisplay, mode->vdisplay,
723 adjusted_mode->hdisplay,
724 adjusted_mode->vdisplay);
726 if (bo->is_primary == false)
727 recreate_primary = true;
729 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
730 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
734 ret = qxl_bo_reserve(bo, false);
737 ret = qxl_bo_pin(bo, bo->type, NULL);
739 qxl_bo_unreserve(bo);
742 qxl_bo_unreserve(bo);
743 if (recreate_primary) {
744 qxl_io_destroy_primary(qdev);
746 "recreate primary: %dx%d,%d,%d\n",
747 bo->surf.width, bo->surf.height,
748 bo->surf.stride, bo->surf.format);
749 qxl_io_create_primary(qdev, 0, bo);
750 bo->is_primary = true;
752 ret = qxl_crtc_apply_cursor(crtc);
754 DRM_ERROR("could not set cursor after modeset");
759 if (bo->is_primary) {
760 DRM_DEBUG_KMS("setting surface_id to 0 for primary surface %d on crtc %d\n", bo->surface_id, qcrtc->index);
763 surf_id = bo->surface_id;
766 if (old_bo && old_bo != bo) {
767 old_bo->is_primary = false;
768 ret = qxl_bo_reserve(old_bo, false);
769 qxl_bo_unpin(old_bo);
770 qxl_bo_unreserve(old_bo);
773 qxl_monitors_config_set(qdev, qcrtc->index, x, y,
775 mode->vdisplay, surf_id);
779 static void qxl_crtc_prepare(struct drm_crtc *crtc)
781 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
782 crtc->mode.hdisplay, crtc->mode.vdisplay,
783 crtc->x, crtc->y, crtc->enabled);
786 static void qxl_crtc_commit(struct drm_crtc *crtc)
791 static void qxl_crtc_disable(struct drm_crtc *crtc)
793 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
794 struct drm_device *dev = crtc->dev;
795 struct qxl_device *qdev = dev->dev_private;
796 if (crtc->primary->fb) {
797 struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb);
798 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
800 ret = qxl_bo_reserve(bo, false);
802 qxl_bo_unreserve(bo);
803 crtc->primary->fb = NULL;
806 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
808 qxl_send_monitors_config(qdev);
811 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
812 .dpms = qxl_crtc_dpms,
813 .disable = qxl_crtc_disable,
814 .mode_fixup = qxl_crtc_mode_fixup,
815 .mode_set = qxl_crtc_mode_set,
816 .prepare = qxl_crtc_prepare,
817 .commit = qxl_crtc_commit,
820 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
822 struct qxl_crtc *qxl_crtc;
824 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
828 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
829 qxl_crtc->index = crtc_id;
830 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
834 static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
839 static void qxl_enc_prepare(struct drm_encoder *encoder)
844 static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
845 struct drm_encoder *encoder)
848 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
849 struct qxl_head *head;
850 struct drm_display_mode *mode;
853 /* TODO: ugly, do better */
855 if (!qdev->monitors_config ||
856 qdev->monitors_config->max_allowed <= i) {
858 "head number too large or missing monitors config: %p, %d",
859 qdev->monitors_config,
860 qdev->monitors_config ?
861 qdev->monitors_config->max_allowed : -1);
864 if (!encoder->crtc) {
865 DRM_ERROR("missing crtc on encoder %p\n", encoder);
869 DRM_DEBUG("missing for multiple monitors: no head holes\n");
870 head = &qdev->monitors_config->heads[i];
872 if (encoder->crtc->enabled) {
873 mode = &encoder->crtc->mode;
874 head->width = mode->hdisplay;
875 head->height = mode->vdisplay;
876 head->x = encoder->crtc->x;
877 head->y = encoder->crtc->y;
878 if (qdev->monitors_config->count < i + 1)
879 qdev->monitors_config->count = i + 1;
886 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
887 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
889 /* TODO - somewhere else to call this for multiple monitors
890 * (config_commit?) */
891 qxl_send_monitors_config(qdev);
894 static void qxl_enc_commit(struct drm_encoder *encoder)
896 struct qxl_device *qdev = encoder->dev->dev_private;
898 qxl_write_monitors_config_for_encoder(qdev, encoder);
902 static void qxl_enc_mode_set(struct drm_encoder *encoder,
903 struct drm_display_mode *mode,
904 struct drm_display_mode *adjusted_mode)
909 static int qxl_conn_get_modes(struct drm_connector *connector)
912 struct qxl_device *qdev = connector->dev->dev_private;
913 unsigned pwidth = 1024;
914 unsigned pheight = 768;
916 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
917 /* TODO: what should we do here? only show the configured modes for the
918 * device, or allow the full list, or both? */
919 if (qdev->monitors_config && qdev->monitors_config->count) {
920 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
924 ret += qxl_add_common_modes(connector, pwidth, pheight);
928 static int qxl_conn_mode_valid(struct drm_connector *connector,
929 struct drm_display_mode *mode)
931 struct drm_device *ddev = connector->dev;
932 struct qxl_device *qdev = ddev->dev_private;
935 /* TODO: is this called for user defined modes? (xrandr --add-mode)
936 * TODO: check that the mode fits in the framebuffer */
938 if(qdev->monitors_config_width == mode->hdisplay &&
939 qdev->monitors_config_height == mode->vdisplay)
942 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
943 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
949 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
951 struct qxl_output *qxl_output =
952 drm_connector_to_qxl_output(connector);
955 return &qxl_output->enc;
959 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
960 .dpms = qxl_enc_dpms,
961 .prepare = qxl_enc_prepare,
962 .mode_set = qxl_enc_mode_set,
963 .commit = qxl_enc_commit,
966 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
967 .get_modes = qxl_conn_get_modes,
968 .mode_valid = qxl_conn_mode_valid,
969 .best_encoder = qxl_best_encoder,
972 static enum drm_connector_status qxl_conn_detect(
973 struct drm_connector *connector,
976 struct qxl_output *output =
977 drm_connector_to_qxl_output(connector);
978 struct drm_device *ddev = connector->dev;
979 struct qxl_device *qdev = ddev->dev_private;
980 bool connected = false;
982 /* The first monitor is always connected */
983 if (!qdev->client_monitors_config) {
984 if (output->index == 0)
987 connected = qdev->client_monitors_config->count > output->index &&
988 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
990 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
992 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
994 return connected ? connector_status_connected
995 : connector_status_disconnected;
998 static int qxl_conn_set_property(struct drm_connector *connector,
999 struct drm_property *property,
1006 static void qxl_conn_destroy(struct drm_connector *connector)
1008 struct qxl_output *qxl_output =
1009 drm_connector_to_qxl_output(connector);
1011 drm_connector_unregister(connector);
1012 drm_connector_cleanup(connector);
1016 static const struct drm_connector_funcs qxl_connector_funcs = {
1017 .dpms = drm_helper_connector_dpms,
1018 .detect = qxl_conn_detect,
1019 .fill_modes = drm_helper_probe_single_connector_modes,
1020 .set_property = qxl_conn_set_property,
1021 .destroy = qxl_conn_destroy,
1024 static void qxl_enc_destroy(struct drm_encoder *encoder)
1026 drm_encoder_cleanup(encoder);
1029 static const struct drm_encoder_funcs qxl_enc_funcs = {
1030 .destroy = qxl_enc_destroy,
1033 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1035 if (qdev->hotplug_mode_update_property)
1038 qdev->hotplug_mode_update_property =
1039 drm_property_create_range(qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1040 "hotplug_mode_update", 0, 1);
1045 static int qdev_output_init(struct drm_device *dev, int num_output)
1047 struct qxl_device *qdev = dev->dev_private;
1048 struct qxl_output *qxl_output;
1049 struct drm_connector *connector;
1050 struct drm_encoder *encoder;
1052 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1056 qxl_output->index = num_output;
1058 connector = &qxl_output->base;
1059 encoder = &qxl_output->enc;
1060 drm_connector_init(dev, &qxl_output->base,
1061 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1063 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
1064 DRM_MODE_ENCODER_VIRTUAL, NULL);
1066 /* we get HPD via client monitors config */
1067 connector->polled = DRM_CONNECTOR_POLL_HPD;
1068 encoder->possible_crtcs = 1 << num_output;
1069 drm_mode_connector_attach_encoder(&qxl_output->base,
1071 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1072 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1074 drm_object_attach_property(&connector->base,
1075 qdev->hotplug_mode_update_property, 0);
1076 drm_object_attach_property(&connector->base,
1077 dev->mode_config.suggested_x_property, 0);
1078 drm_object_attach_property(&connector->base,
1079 dev->mode_config.suggested_y_property, 0);
1080 drm_connector_register(connector);
1084 static struct drm_framebuffer *
1085 qxl_user_framebuffer_create(struct drm_device *dev,
1086 struct drm_file *file_priv,
1087 const struct drm_mode_fb_cmd2 *mode_cmd)
1089 struct drm_gem_object *obj;
1090 struct qxl_framebuffer *qxl_fb;
1093 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1097 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1101 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
1104 drm_gem_object_unreference_unlocked(obj);
1108 return &qxl_fb->base;
1111 static const struct drm_mode_config_funcs qxl_mode_funcs = {
1112 .fb_create = qxl_user_framebuffer_create,
1115 int qxl_create_monitors_object(struct qxl_device *qdev)
1118 struct drm_gem_object *gobj;
1119 int max_allowed = qxl_num_crtc;
1120 int monitors_config_size = sizeof(struct qxl_monitors_config) +
1121 max_allowed * sizeof(struct qxl_head);
1123 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1124 QXL_GEM_DOMAIN_VRAM,
1125 false, false, NULL, &gobj);
1127 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1130 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1132 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1136 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
1138 qxl_bo_unreserve(qdev->monitors_config_bo);
1142 qxl_bo_unreserve(qdev->monitors_config_bo);
1144 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
1146 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1147 qdev->ram_header->monitors_config =
1148 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1150 memset(qdev->monitors_config, 0, monitors_config_size);
1151 qdev->monitors_config->max_allowed = max_allowed;
1155 int qxl_destroy_monitors_object(struct qxl_device *qdev)
1159 qdev->monitors_config = NULL;
1160 qdev->ram_header->monitors_config = 0;
1162 qxl_bo_kunmap(qdev->monitors_config_bo);
1163 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1167 qxl_bo_unpin(qdev->monitors_config_bo);
1168 qxl_bo_unreserve(qdev->monitors_config_bo);
1170 qxl_bo_unref(&qdev->monitors_config_bo);
1174 int qxl_modeset_init(struct qxl_device *qdev)
1179 drm_mode_config_init(qdev->ddev);
1181 ret = qxl_create_monitors_object(qdev);
1185 qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
1187 /* modes will be validated against the framebuffer size */
1188 qdev->ddev->mode_config.min_width = 320;
1189 qdev->ddev->mode_config.min_height = 200;
1190 qdev->ddev->mode_config.max_width = 8192;
1191 qdev->ddev->mode_config.max_height = 8192;
1193 qdev->ddev->mode_config.fb_base = qdev->vram_base;
1195 drm_mode_create_suggested_offset_properties(qdev->ddev);
1196 qxl_mode_create_hotplug_mode_update_property(qdev);
1198 for (i = 0 ; i < qxl_num_crtc; ++i) {
1199 qdev_crtc_init(qdev->ddev, i);
1200 qdev_output_init(qdev->ddev, i);
1203 qdev->mode_info.mode_config_initialized = true;
1205 /* primary surface must be created by this point, to allow
1206 * issuing command queue commands and having them read by
1208 qxl_fbdev_init(qdev);
1212 void qxl_modeset_fini(struct qxl_device *qdev)
1214 qxl_fbdev_fini(qdev);
1216 qxl_destroy_monitors_object(qdev);
1217 if (qdev->mode_info.mode_config_initialized) {
1218 drm_mode_config_cleanup(qdev->ddev);
1219 qdev->mode_info.mode_config_initialized = false;