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 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;
60 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
66 num_monitors = qdev->rom->client_monitors_config.count;
67 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
68 sizeof(qdev->rom->client_monitors_config));
69 if (crc != qdev->rom->client_monitors_config_crc) {
70 qxl_io_log(qdev, "crc mismatch: have %X (%d) != %X\n", crc,
71 sizeof(qdev->rom->client_monitors_config),
72 qdev->rom->client_monitors_config_crc);
75 if (num_monitors > qdev->monitors_config->max_allowed) {
76 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
77 qdev->monitors_config->max_allowed, num_monitors);
78 num_monitors = qdev->monitors_config->max_allowed;
80 num_monitors = qdev->rom->client_monitors_config.count;
82 qxl_alloc_client_monitors_config(qdev, num_monitors);
83 /* we copy max from the client but it isn't used */
84 qdev->client_monitors_config->max_allowed =
85 qdev->monitors_config->max_allowed;
86 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
87 struct qxl_urect *c_rect =
88 &qdev->rom->client_monitors_config.heads[i];
89 struct qxl_head *client_head =
90 &qdev->client_monitors_config->heads[i];
91 client_head->x = c_rect->left;
92 client_head->y = c_rect->top;
93 client_head->width = c_rect->right - c_rect->left;
94 client_head->height = c_rect->bottom - c_rect->top;
95 client_head->surface_id = 0;
97 client_head->flags = 0;
98 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
99 client_head->x, client_head->y);
104 void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
107 while (qxl_display_copy_rom_client_monitors_config(qdev)) {
108 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
112 if (!drm_helper_hpd_irq_event(qdev->ddev)) {
113 /* notify that the monitor configuration changed, to
114 adjust at the arbitrary resolution */
115 drm_kms_helper_hotplug_event(qdev->ddev);
119 static int qxl_add_monitors_config_modes(struct drm_connector *connector,
123 struct drm_device *dev = connector->dev;
124 struct qxl_device *qdev = dev->dev_private;
125 struct qxl_output *output = drm_connector_to_qxl_output(connector);
126 int h = output->index;
127 struct drm_display_mode *mode = NULL;
128 struct qxl_head *head;
130 if (!qdev->client_monitors_config)
132 head = &qdev->client_monitors_config->heads[h];
134 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
136 mode->type |= DRM_MODE_TYPE_PREFERRED;
137 *pwidth = head->width;
138 *pheight = head->height;
139 drm_mode_probed_add(connector, mode);
143 static int qxl_add_common_modes(struct drm_connector *connector,
147 struct drm_device *dev = connector->dev;
148 struct drm_display_mode *mode = NULL;
173 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
174 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
175 60, false, false, false);
176 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
177 mode->type |= DRM_MODE_TYPE_PREFERRED;
178 drm_mode_probed_add(connector, mode);
183 static void qxl_crtc_destroy(struct drm_crtc *crtc)
185 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
187 drm_crtc_cleanup(crtc);
191 static int qxl_crtc_page_flip(struct drm_crtc *crtc,
192 struct drm_framebuffer *fb,
193 struct drm_pending_vblank_event *event,
194 uint32_t page_flip_flags)
196 struct drm_device *dev = crtc->dev;
197 struct qxl_device *qdev = dev->dev_private;
198 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
199 struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
200 struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
201 struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
202 struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
204 struct drm_clip_rect norect = {
211 int one_clip_rect = 1;
214 crtc->primary->fb = fb;
215 bo_old->is_primary = false;
216 bo->is_primary = true;
218 ret = qxl_bo_reserve(bo, false);
222 qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
223 &norect, one_clip_rect, inc);
225 drm_vblank_get(dev, qcrtc->index);
228 spin_lock_irqsave(&dev->event_lock, flags);
229 drm_send_vblank_event(dev, qcrtc->index, event);
230 spin_unlock_irqrestore(&dev->event_lock, flags);
232 drm_vblank_put(dev, qcrtc->index);
234 qxl_bo_unreserve(bo);
240 qxl_hide_cursor(struct qxl_device *qdev)
242 struct qxl_release *release;
243 struct qxl_cursor_cmd *cmd;
246 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
251 ret = qxl_release_reserve_list(release, true);
253 qxl_release_free(qdev, release);
257 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
258 cmd->type = QXL_CURSOR_HIDE;
259 qxl_release_unmap(qdev, release, &cmd->release_info);
261 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
262 qxl_release_fence_buffer_objects(release);
266 static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
267 struct drm_file *file_priv,
270 uint32_t height, int32_t hot_x, int32_t hot_y)
272 struct drm_device *dev = crtc->dev;
273 struct qxl_device *qdev = dev->dev_private;
274 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
275 struct drm_gem_object *obj;
276 struct qxl_cursor *cursor;
277 struct qxl_cursor_cmd *cmd;
278 struct qxl_bo *cursor_bo, *user_bo;
279 struct qxl_release *release;
285 return qxl_hide_cursor(qdev);
287 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
289 DRM_ERROR("cannot find cursor object\n");
293 user_bo = gem_to_qxl_bo(obj);
295 ret = qxl_bo_reserve(user_bo, false);
299 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
300 qxl_bo_unreserve(user_bo);
304 ret = qxl_bo_kmap(user_bo, &user_ptr);
308 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
309 QXL_RELEASE_CURSOR_CMD,
314 ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size,
317 goto out_free_release;
319 ret = qxl_release_reserve_list(release, false);
323 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
327 cursor->header.unique = 0;
328 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
329 cursor->header.width = 64;
330 cursor->header.height = 64;
331 cursor->header.hot_spot_x = hot_x;
332 cursor->header.hot_spot_y = hot_y;
333 cursor->data_size = size;
334 cursor->chunk.next_chunk = 0;
335 cursor->chunk.prev_chunk = 0;
336 cursor->chunk.data_size = size;
338 memcpy(cursor->chunk.data, user_ptr, size);
340 qxl_bo_kunmap(cursor_bo);
342 qxl_bo_kunmap(user_bo);
344 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
345 cmd->type = QXL_CURSOR_SET;
346 cmd->u.set.position.x = qcrtc->cur_x;
347 cmd->u.set.position.y = qcrtc->cur_y;
349 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
351 cmd->u.set.visible = 1;
352 qxl_release_unmap(qdev, release, &cmd->release_info);
354 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
355 qxl_release_fence_buffer_objects(release);
357 /* finish with the userspace bo */
358 ret = qxl_bo_reserve(user_bo, false);
360 qxl_bo_unpin(user_bo);
361 qxl_bo_unreserve(user_bo);
363 drm_gem_object_unreference_unlocked(obj);
365 qxl_bo_unref(&cursor_bo);
370 qxl_release_backoff_reserve_list(release);
372 qxl_bo_unref(&cursor_bo);
374 qxl_release_free(qdev, release);
376 qxl_bo_kunmap(user_bo);
378 qxl_bo_unpin(user_bo);
380 drm_gem_object_unreference_unlocked(obj);
384 static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
387 struct drm_device *dev = crtc->dev;
388 struct qxl_device *qdev = dev->dev_private;
389 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
390 struct qxl_release *release;
391 struct qxl_cursor_cmd *cmd;
394 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
399 ret = qxl_release_reserve_list(release, true);
401 qxl_release_free(qdev, release);
408 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
409 cmd->type = QXL_CURSOR_MOVE;
410 cmd->u.position.x = qcrtc->cur_x;
411 cmd->u.position.y = qcrtc->cur_y;
412 qxl_release_unmap(qdev, release, &cmd->release_info);
414 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
415 qxl_release_fence_buffer_objects(release);
421 static const struct drm_crtc_funcs qxl_crtc_funcs = {
422 .cursor_set2 = qxl_crtc_cursor_set2,
423 .cursor_move = qxl_crtc_cursor_move,
424 .set_config = drm_crtc_helper_set_config,
425 .destroy = qxl_crtc_destroy,
426 .page_flip = qxl_crtc_page_flip,
429 static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
431 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
434 drm_gem_object_unreference_unlocked(qxl_fb->obj);
435 drm_framebuffer_cleanup(fb);
439 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
440 struct drm_file *file_priv,
441 unsigned flags, unsigned color,
442 struct drm_clip_rect *clips,
445 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
446 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
447 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
448 struct drm_clip_rect norect;
452 drm_modeset_lock_all(fb->dev);
454 qobj = gem_to_qxl_bo(qxl_fb->obj);
455 /* if we aren't primary surface ignore this */
456 if (!qobj->is_primary) {
457 drm_modeset_unlock_all(fb->dev);
464 norect.x1 = norect.y1 = 0;
465 norect.x2 = fb->width;
466 norect.y2 = fb->height;
467 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
469 inc = 2; /* skip source rects */
472 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
473 clips, num_clips, inc);
475 drm_modeset_unlock_all(fb->dev);
480 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
481 .destroy = qxl_user_framebuffer_destroy,
482 .dirty = qxl_framebuffer_surface_dirty,
484 * .create_handle = qxl_user_framebuffer_create_handle, */
488 qxl_framebuffer_init(struct drm_device *dev,
489 struct qxl_framebuffer *qfb,
490 struct drm_mode_fb_cmd2 *mode_cmd,
491 struct drm_gem_object *obj)
496 ret = drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs);
501 drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
505 static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
509 static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
510 const struct drm_display_mode *mode,
511 struct drm_display_mode *adjusted_mode)
513 struct drm_device *dev = crtc->dev;
514 struct qxl_device *qdev = dev->dev_private;
516 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
518 mode->hdisplay, mode->vdisplay,
519 adjusted_mode->hdisplay,
520 adjusted_mode->vdisplay);
525 qxl_send_monitors_config(struct qxl_device *qdev)
529 BUG_ON(!qdev->ram_header->monitors_config);
531 if (qdev->monitors_config->count == 0) {
532 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
535 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
536 struct qxl_head *head = &qdev->monitors_config->heads[i];
538 if (head->y > 8192 || head->x > 8192 ||
539 head->width > 8192 || head->height > 8192) {
540 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
541 i, head->width, head->height,
546 qxl_io_monitors_config(qdev);
549 static void qxl_monitors_config_set(struct qxl_device *qdev,
551 unsigned x, unsigned y,
552 unsigned width, unsigned height,
555 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
556 qdev->monitors_config->heads[index].x = x;
557 qdev->monitors_config->heads[index].y = y;
558 qdev->monitors_config->heads[index].width = width;
559 qdev->monitors_config->heads[index].height = height;
560 qdev->monitors_config->heads[index].surface_id = surf_id;
564 static int qxl_crtc_mode_set(struct drm_crtc *crtc,
565 struct drm_display_mode *mode,
566 struct drm_display_mode *adjusted_mode,
568 struct drm_framebuffer *old_fb)
570 struct drm_device *dev = crtc->dev;
571 struct qxl_device *qdev = dev->dev_private;
572 struct qxl_mode *m = (void *)mode->private;
573 struct qxl_framebuffer *qfb;
574 struct qxl_bo *bo, *old_bo = NULL;
575 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
576 bool recreate_primary = false;
579 if (!crtc->primary->fb) {
580 DRM_DEBUG_KMS("No FB bound\n");
585 qfb = to_qxl_framebuffer(old_fb);
586 old_bo = gem_to_qxl_bo(qfb->obj);
588 qfb = to_qxl_framebuffer(crtc->primary->fb);
589 bo = gem_to_qxl_bo(qfb->obj);
591 /* and do we care? */
592 DRM_DEBUG("%dx%d: not a native mode\n", x, y);
594 DRM_DEBUG("%dx%d: qxl id %d\n",
595 mode->hdisplay, mode->vdisplay, m->id);
596 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
598 mode->hdisplay, mode->vdisplay,
599 adjusted_mode->hdisplay,
600 adjusted_mode->vdisplay);
602 if (qcrtc->index == 0)
603 recreate_primary = true;
605 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
606 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
610 ret = qxl_bo_reserve(bo, false);
613 ret = qxl_bo_pin(bo, bo->type, NULL);
615 qxl_bo_unreserve(bo);
618 qxl_bo_unreserve(bo);
619 if (recreate_primary) {
620 qxl_io_destroy_primary(qdev);
622 "recreate primary: %dx%d,%d,%d\n",
623 bo->surf.width, bo->surf.height,
624 bo->surf.stride, bo->surf.format);
625 qxl_io_create_primary(qdev, 0, bo);
626 bo->is_primary = true;
629 if (bo->is_primary) {
630 DRM_DEBUG_KMS("setting surface_id to 0 for primary surface %d on crtc %d\n", bo->surface_id, qcrtc->index);
633 surf_id = bo->surface_id;
636 if (old_bo && old_bo != bo) {
637 old_bo->is_primary = false;
638 ret = qxl_bo_reserve(old_bo, false);
639 qxl_bo_unpin(old_bo);
640 qxl_bo_unreserve(old_bo);
643 qxl_monitors_config_set(qdev, qcrtc->index, x, y,
645 mode->vdisplay, surf_id);
649 static void qxl_crtc_prepare(struct drm_crtc *crtc)
651 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
652 crtc->mode.hdisplay, crtc->mode.vdisplay,
653 crtc->x, crtc->y, crtc->enabled);
656 static void qxl_crtc_commit(struct drm_crtc *crtc)
661 static void qxl_crtc_disable(struct drm_crtc *crtc)
663 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
664 struct drm_device *dev = crtc->dev;
665 struct qxl_device *qdev = dev->dev_private;
666 if (crtc->primary->fb) {
667 struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb);
668 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
670 ret = qxl_bo_reserve(bo, false);
672 qxl_bo_unreserve(bo);
673 crtc->primary->fb = NULL;
676 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
678 qxl_send_monitors_config(qdev);
681 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
682 .dpms = qxl_crtc_dpms,
683 .disable = qxl_crtc_disable,
684 .mode_fixup = qxl_crtc_mode_fixup,
685 .mode_set = qxl_crtc_mode_set,
686 .prepare = qxl_crtc_prepare,
687 .commit = qxl_crtc_commit,
690 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
692 struct qxl_crtc *qxl_crtc;
694 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
698 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
699 qxl_crtc->index = crtc_id;
700 drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
701 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
705 static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
710 static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
711 const struct drm_display_mode *mode,
712 struct drm_display_mode *adjusted_mode)
718 static void qxl_enc_prepare(struct drm_encoder *encoder)
723 static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
724 struct drm_encoder *encoder)
727 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
728 struct qxl_head *head;
729 struct drm_display_mode *mode;
732 /* TODO: ugly, do better */
734 if (!qdev->monitors_config ||
735 qdev->monitors_config->max_allowed <= i) {
737 "head number too large or missing monitors config: %p, %d",
738 qdev->monitors_config,
739 qdev->monitors_config ?
740 qdev->monitors_config->max_allowed : -1);
743 if (!encoder->crtc) {
744 DRM_ERROR("missing crtc on encoder %p\n", encoder);
748 DRM_DEBUG("missing for multiple monitors: no head holes\n");
749 head = &qdev->monitors_config->heads[i];
751 if (encoder->crtc->enabled) {
752 mode = &encoder->crtc->mode;
753 head->width = mode->hdisplay;
754 head->height = mode->vdisplay;
755 head->x = encoder->crtc->x;
756 head->y = encoder->crtc->y;
757 if (qdev->monitors_config->count < i + 1)
758 qdev->monitors_config->count = i + 1;
765 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
766 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
768 /* TODO - somewhere else to call this for multiple monitors
769 * (config_commit?) */
770 qxl_send_monitors_config(qdev);
773 static void qxl_enc_commit(struct drm_encoder *encoder)
775 struct qxl_device *qdev = encoder->dev->dev_private;
777 qxl_write_monitors_config_for_encoder(qdev, encoder);
781 static void qxl_enc_mode_set(struct drm_encoder *encoder,
782 struct drm_display_mode *mode,
783 struct drm_display_mode *adjusted_mode)
788 static int qxl_conn_get_modes(struct drm_connector *connector)
791 struct qxl_device *qdev = connector->dev->dev_private;
792 unsigned pwidth = 1024;
793 unsigned pheight = 768;
795 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
796 /* TODO: what should we do here? only show the configured modes for the
797 * device, or allow the full list, or both? */
798 if (qdev->monitors_config && qdev->monitors_config->count) {
799 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
803 ret += qxl_add_common_modes(connector, pwidth, pheight);
807 static int qxl_conn_mode_valid(struct drm_connector *connector,
808 struct drm_display_mode *mode)
810 /* TODO: is this called for user defined modes? (xrandr --add-mode)
811 * TODO: check that the mode fits in the framebuffer */
812 DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay,
813 mode->vdisplay, mode->status);
817 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
819 struct qxl_output *qxl_output =
820 drm_connector_to_qxl_output(connector);
823 return &qxl_output->enc;
827 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
828 .dpms = qxl_enc_dpms,
829 .mode_fixup = qxl_enc_mode_fixup,
830 .prepare = qxl_enc_prepare,
831 .mode_set = qxl_enc_mode_set,
832 .commit = qxl_enc_commit,
835 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
836 .get_modes = qxl_conn_get_modes,
837 .mode_valid = qxl_conn_mode_valid,
838 .best_encoder = qxl_best_encoder,
841 static void qxl_conn_save(struct drm_connector *connector)
846 static void qxl_conn_restore(struct drm_connector *connector)
851 static enum drm_connector_status qxl_conn_detect(
852 struct drm_connector *connector,
855 struct qxl_output *output =
856 drm_connector_to_qxl_output(connector);
857 struct drm_device *ddev = connector->dev;
858 struct qxl_device *qdev = ddev->dev_private;
861 /* The first monitor is always connected */
862 connected = (output->index == 0) ||
863 (qdev->client_monitors_config &&
864 qdev->client_monitors_config->count > output->index &&
865 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]));
867 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
869 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
871 return connected ? connector_status_connected
872 : connector_status_disconnected;
875 static int qxl_conn_set_property(struct drm_connector *connector,
876 struct drm_property *property,
883 static void qxl_conn_destroy(struct drm_connector *connector)
885 struct qxl_output *qxl_output =
886 drm_connector_to_qxl_output(connector);
888 drm_connector_unregister(connector);
889 drm_connector_cleanup(connector);
893 static const struct drm_connector_funcs qxl_connector_funcs = {
894 .dpms = drm_helper_connector_dpms,
895 .save = qxl_conn_save,
896 .restore = qxl_conn_restore,
897 .detect = qxl_conn_detect,
898 .fill_modes = drm_helper_probe_single_connector_modes_nomerge,
899 .set_property = qxl_conn_set_property,
900 .destroy = qxl_conn_destroy,
903 static void qxl_enc_destroy(struct drm_encoder *encoder)
905 drm_encoder_cleanup(encoder);
908 static const struct drm_encoder_funcs qxl_enc_funcs = {
909 .destroy = qxl_enc_destroy,
912 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
914 if (qdev->hotplug_mode_update_property)
917 qdev->hotplug_mode_update_property =
918 drm_property_create_range(qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
919 "hotplug_mode_update", 0, 1);
924 static int qdev_output_init(struct drm_device *dev, int num_output)
926 struct qxl_device *qdev = dev->dev_private;
927 struct qxl_output *qxl_output;
928 struct drm_connector *connector;
929 struct drm_encoder *encoder;
931 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
935 qxl_output->index = num_output;
937 connector = &qxl_output->base;
938 encoder = &qxl_output->enc;
939 drm_connector_init(dev, &qxl_output->base,
940 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
942 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
943 DRM_MODE_ENCODER_VIRTUAL);
945 /* we get HPD via client monitors config */
946 connector->polled = DRM_CONNECTOR_POLL_HPD;
947 encoder->possible_crtcs = 1 << num_output;
948 drm_mode_connector_attach_encoder(&qxl_output->base,
950 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
951 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
953 drm_object_attach_property(&connector->base,
954 qdev->hotplug_mode_update_property, 0);
955 drm_connector_register(connector);
959 static struct drm_framebuffer *
960 qxl_user_framebuffer_create(struct drm_device *dev,
961 struct drm_file *file_priv,
962 struct drm_mode_fb_cmd2 *mode_cmd)
964 struct drm_gem_object *obj;
965 struct qxl_framebuffer *qxl_fb;
968 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
970 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
974 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
977 drm_gem_object_unreference_unlocked(obj);
981 return &qxl_fb->base;
984 static const struct drm_mode_config_funcs qxl_mode_funcs = {
985 .fb_create = qxl_user_framebuffer_create,
988 int qxl_create_monitors_object(struct qxl_device *qdev)
991 struct drm_gem_object *gobj;
992 int max_allowed = qxl_num_crtc;
993 int monitors_config_size = sizeof(struct qxl_monitors_config) +
994 max_allowed * sizeof(struct qxl_head);
996 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
998 false, false, NULL, &gobj);
1000 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1003 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1005 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1009 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
1011 qxl_bo_unreserve(qdev->monitors_config_bo);
1015 qxl_bo_unreserve(qdev->monitors_config_bo);
1017 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
1019 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1020 qdev->ram_header->monitors_config =
1021 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1023 memset(qdev->monitors_config, 0, monitors_config_size);
1024 qdev->monitors_config->max_allowed = max_allowed;
1028 int qxl_destroy_monitors_object(struct qxl_device *qdev)
1032 qdev->monitors_config = NULL;
1033 qdev->ram_header->monitors_config = 0;
1035 qxl_bo_kunmap(qdev->monitors_config_bo);
1036 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1040 qxl_bo_unpin(qdev->monitors_config_bo);
1041 qxl_bo_unreserve(qdev->monitors_config_bo);
1043 qxl_bo_unref(&qdev->monitors_config_bo);
1047 int qxl_modeset_init(struct qxl_device *qdev)
1052 drm_mode_config_init(qdev->ddev);
1054 ret = qxl_create_monitors_object(qdev);
1058 qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
1060 /* modes will be validated against the framebuffer size */
1061 qdev->ddev->mode_config.min_width = 320;
1062 qdev->ddev->mode_config.min_height = 200;
1063 qdev->ddev->mode_config.max_width = 8192;
1064 qdev->ddev->mode_config.max_height = 8192;
1066 qdev->ddev->mode_config.fb_base = qdev->vram_base;
1068 qxl_mode_create_hotplug_mode_update_property(qdev);
1070 for (i = 0 ; i < qxl_num_crtc; ++i) {
1071 qdev_crtc_init(qdev->ddev, i);
1072 qdev_output_init(qdev->ddev, i);
1075 qdev->mode_info.mode_config_initialized = true;
1077 /* primary surface must be created by this point, to allow
1078 * issuing command queue commands and having them read by
1080 qxl_fbdev_init(qdev);
1084 void qxl_modeset_fini(struct qxl_device *qdev)
1086 qxl_fbdev_fini(qdev);
1088 qxl_destroy_monitors_object(qdev);
1089 if (qdev->mode_info.mode_config_initialized) {
1090 drm_mode_config_cleanup(qdev->ddev);
1091 qdev->mode_info.mode_config_initialized = false;