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"
33 static void qxl_crtc_set_to_mode(struct qxl_device *qdev,
34 struct drm_connector *connector,
35 struct qxl_head *head)
37 struct drm_device *dev = connector->dev;
38 struct drm_display_mode *mode, *t;
39 int width = head->width;
40 int height = head->height;
42 if (width < 320 || height < 240) {
43 qxl_io_log(qdev, "%s: bad head: %dx%d", width, height);
47 if (width * height * 4 > 16*1024*1024) {
51 /* TODO: go over regular modes and removed preferred? */
52 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
53 drm_mode_remove(connector, mode);
54 mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
55 mode->type |= DRM_MODE_TYPE_PREFERRED;
56 mode->status = MODE_OK;
57 drm_mode_probed_add(connector, mode);
58 qxl_io_log(qdev, "%s: %d x %d\n", __func__, width, height);
61 void qxl_crtc_set_from_monitors_config(struct qxl_device *qdev)
63 struct drm_connector *connector;
65 struct drm_device *dev = qdev->ddev;
68 qxl_io_log(qdev, "%s: %d, %d\n", __func__,
69 dev->mode_config.num_connector,
70 qdev->monitors_config->count);
71 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
72 if (i > qdev->monitors_config->count) {
73 /* crtc will be reported as disabled */
76 qxl_crtc_set_to_mode(qdev, connector,
77 &qdev->monitors_config->heads[i]);
82 void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
84 if (qdev->client_monitors_config &&
85 count > qdev->client_monitors_config->count) {
86 kfree(qdev->client_monitors_config);
87 qdev->client_monitors_config = NULL;
89 if (!qdev->client_monitors_config) {
90 qdev->client_monitors_config = kzalloc(
91 sizeof(struct qxl_monitors_config) +
92 sizeof(struct qxl_head) * count, GFP_KERNEL);
93 if (!qdev->client_monitors_config) {
95 "%s: allocation failure for %u heads\n",
100 qdev->client_monitors_config->count = count;
103 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
109 BUG_ON(!qdev->monitors_config);
110 num_monitors = qdev->rom->client_monitors_config.count;
111 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
112 sizeof(qdev->rom->client_monitors_config));
113 if (crc != qdev->rom->client_monitors_config_crc) {
114 qxl_io_log(qdev, "crc mismatch: have %X (%d) != %X\n", crc,
115 sizeof(qdev->rom->client_monitors_config),
116 qdev->rom->client_monitors_config_crc);
119 if (num_monitors > qdev->monitors_config->max_allowed) {
120 DRM_INFO("client monitors list will be truncated: %d < %d\n",
121 qdev->monitors_config->max_allowed, num_monitors);
122 num_monitors = qdev->monitors_config->max_allowed;
124 num_monitors = qdev->rom->client_monitors_config.count;
126 qxl_alloc_client_monitors_config(qdev, num_monitors);
127 /* we copy max from the client but it isn't used */
128 qdev->client_monitors_config->max_allowed =
129 qdev->monitors_config->max_allowed;
130 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
131 struct qxl_urect *c_rect =
132 &qdev->rom->client_monitors_config.heads[i];
133 struct qxl_head *client_head =
134 &qdev->client_monitors_config->heads[i];
135 struct qxl_head *head = &qdev->monitors_config->heads[i];
136 client_head->x = head->x = c_rect->left;
137 client_head->y = head->y = c_rect->top;
138 client_head->width = head->width =
139 c_rect->right - c_rect->left;
140 client_head->height = head->height =
141 c_rect->bottom - c_rect->top;
142 client_head->surface_id = head->surface_id = 0;
143 client_head->id = head->id = i;
144 client_head->flags = head->flags = 0;
145 QXL_DEBUG(qdev, "read %dx%d+%d+%d\n", head->width, head->height,
151 void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
154 while (qxl_display_copy_rom_client_monitors_config(qdev)) {
155 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
158 qxl_crtc_set_from_monitors_config(qdev);
159 /* fire off a uevent and let userspace tell us what to do */
160 qxl_io_log(qdev, "calling drm_sysfs_hotplug_event\n");
161 drm_sysfs_hotplug_event(qdev->ddev);
164 static int qxl_add_monitors_config_modes(struct drm_connector *connector)
166 struct drm_device *dev = connector->dev;
167 struct qxl_device *qdev = dev->dev_private;
168 struct qxl_output *output = drm_connector_to_qxl_output(connector);
169 int h = output->index;
170 struct drm_display_mode *mode = NULL;
171 struct qxl_head *head;
173 if (!qdev->monitors_config)
175 head = &qdev->monitors_config->heads[h];
177 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
179 mode->type |= DRM_MODE_TYPE_PREFERRED;
180 drm_mode_probed_add(connector, mode);
184 static int qxl_add_common_modes(struct drm_connector *connector)
186 struct drm_device *dev = connector->dev;
187 struct drm_display_mode *mode = NULL;
212 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
213 if (common_modes[i].w < 320 || common_modes[i].h < 200)
216 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
217 60, false, false, false);
218 if (common_modes[i].w == 1024 && common_modes[i].h == 768)
219 mode->type |= DRM_MODE_TYPE_PREFERRED;
220 drm_mode_probed_add(connector, mode);
225 static void qxl_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
226 u16 *blue, uint32_t start, uint32_t size)
231 static void qxl_crtc_destroy(struct drm_crtc *crtc)
233 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
235 drm_crtc_cleanup(crtc);
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,
249 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
250 cmd->type = QXL_CURSOR_HIDE;
251 qxl_release_unmap(qdev, release, &cmd->release_info);
253 qxl_fence_releaseable(qdev, release);
254 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
255 qxl_release_unreserve(qdev, release);
258 static int qxl_crtc_cursor_set(struct drm_crtc *crtc,
259 struct drm_file *file_priv,
264 struct drm_device *dev = crtc->dev;
265 struct qxl_device *qdev = dev->dev_private;
266 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
267 struct drm_gem_object *obj;
268 struct qxl_cursor *cursor;
269 struct qxl_cursor_cmd *cmd;
270 struct qxl_bo *cursor_bo, *user_bo;
271 struct qxl_release *release;
277 qxl_hide_cursor(qdev);
281 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
283 DRM_ERROR("cannot find cursor object\n");
287 user_bo = gem_to_qxl_bo(obj);
289 ret = qxl_bo_reserve(user_bo, false);
293 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
297 ret = qxl_bo_kmap(user_bo, &user_ptr);
301 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
302 QXL_RELEASE_CURSOR_CMD,
306 ret = qxl_alloc_bo_reserved(qdev, sizeof(struct qxl_cursor) + size,
309 goto out_free_release;
310 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
314 cursor->header.unique = 0;
315 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
316 cursor->header.width = 64;
317 cursor->header.height = 64;
318 cursor->header.hot_spot_x = 0;
319 cursor->header.hot_spot_y = 0;
320 cursor->data_size = size;
321 cursor->chunk.next_chunk = 0;
322 cursor->chunk.prev_chunk = 0;
323 cursor->chunk.data_size = size;
325 memcpy(cursor->chunk.data, user_ptr, size);
327 qxl_bo_kunmap(cursor_bo);
329 /* finish with the userspace bo */
330 qxl_bo_kunmap(user_bo);
331 qxl_bo_unpin(user_bo);
332 qxl_bo_unreserve(user_bo);
333 drm_gem_object_unreference_unlocked(obj);
335 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
336 cmd->type = QXL_CURSOR_SET;
337 cmd->u.set.position.x = qcrtc->cur_x;
338 cmd->u.set.position.y = qcrtc->cur_y;
340 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
341 qxl_release_add_res(qdev, release, cursor_bo);
343 cmd->u.set.visible = 1;
344 qxl_release_unmap(qdev, release, &cmd->release_info);
346 qxl_fence_releaseable(qdev, release);
347 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
348 qxl_release_unreserve(qdev, release);
350 qxl_bo_unreserve(cursor_bo);
351 qxl_bo_unref(&cursor_bo);
355 qxl_bo_unref(&cursor_bo);
357 qxl_release_unreserve(qdev, release);
358 qxl_release_free(qdev, release);
360 qxl_bo_kunmap(user_bo);
362 qxl_bo_unpin(user_bo);
364 qxl_bo_unreserve(user_bo);
366 drm_gem_object_unreference_unlocked(obj);
370 static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
373 struct drm_device *dev = crtc->dev;
374 struct qxl_device *qdev = dev->dev_private;
375 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
376 struct qxl_release *release;
377 struct qxl_cursor_cmd *cmd;
380 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
386 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
387 cmd->type = QXL_CURSOR_MOVE;
388 cmd->u.position.x = qcrtc->cur_x;
389 cmd->u.position.y = qcrtc->cur_y;
390 qxl_release_unmap(qdev, release, &cmd->release_info);
392 qxl_fence_releaseable(qdev, release);
393 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
394 qxl_release_unreserve(qdev, release);
399 static const struct drm_crtc_funcs qxl_crtc_funcs = {
400 .cursor_set = qxl_crtc_cursor_set,
401 .cursor_move = qxl_crtc_cursor_move,
402 .gamma_set = qxl_crtc_gamma_set,
403 .set_config = drm_crtc_helper_set_config,
404 .destroy = qxl_crtc_destroy,
407 static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
409 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
412 drm_gem_object_unreference_unlocked(qxl_fb->obj);
413 drm_framebuffer_cleanup(fb);
417 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
418 struct drm_file *file_priv,
419 unsigned flags, unsigned color,
420 struct drm_clip_rect *clips,
423 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
424 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
425 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
426 struct drm_clip_rect norect;
430 qobj = gem_to_qxl_bo(qxl_fb->obj);
431 if (qxl_fb != qdev->active_user_framebuffer) {
432 DRM_INFO("%s: qxl_fb 0x%p != qdev->active_user_framebuffer 0x%p\n",
433 __func__, qxl_fb, qdev->active_user_framebuffer);
438 norect.x1 = norect.y1 = 0;
439 norect.x2 = fb->width;
440 norect.y2 = fb->height;
441 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
443 inc = 2; /* skip source rects */
446 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
447 clips, num_clips, inc);
451 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
452 .destroy = qxl_user_framebuffer_destroy,
453 .dirty = qxl_framebuffer_surface_dirty,
455 * .create_handle = qxl_user_framebuffer_create_handle, */
459 qxl_framebuffer_init(struct drm_device *dev,
460 struct qxl_framebuffer *qfb,
461 struct drm_mode_fb_cmd2 *mode_cmd,
462 struct drm_gem_object *obj)
467 ret = drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs);
472 drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
476 static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
480 static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
481 const struct drm_display_mode *mode,
482 struct drm_display_mode *adjusted_mode)
484 struct drm_device *dev = crtc->dev;
485 struct qxl_device *qdev = dev->dev_private;
487 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
489 mode->hdisplay, mode->vdisplay,
490 adjusted_mode->hdisplay,
491 adjusted_mode->vdisplay);
496 qxl_send_monitors_config(struct qxl_device *qdev)
500 BUG_ON(!qdev->ram_header->monitors_config);
502 if (qdev->monitors_config->count == 0) {
503 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
506 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
507 struct qxl_head *head = &qdev->monitors_config->heads[i];
509 if (head->y > 8192 || head->y < head->x ||
510 head->width > 8192 || head->height > 8192) {
511 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
512 i, head->width, head->height,
517 qxl_io_monitors_config(qdev);
520 static void qxl_monitors_config_set_single(struct qxl_device *qdev,
521 unsigned x, unsigned y,
522 unsigned width, unsigned height)
524 DRM_DEBUG("%dx%d+%d+%d\n", width, height, x, y);
525 qdev->monitors_config->count = 1;
526 qdev->monitors_config->heads[0].x = x;
527 qdev->monitors_config->heads[0].y = y;
528 qdev->monitors_config->heads[0].width = width;
529 qdev->monitors_config->heads[0].height = height;
532 static int qxl_crtc_mode_set(struct drm_crtc *crtc,
533 struct drm_display_mode *mode,
534 struct drm_display_mode *adjusted_mode,
536 struct drm_framebuffer *old_fb)
538 struct drm_device *dev = crtc->dev;
539 struct qxl_device *qdev = dev->dev_private;
540 struct qxl_mode *m = (void *)mode->private;
541 struct qxl_framebuffer *qfb;
542 struct qxl_bo *bo, *old_bo = NULL;
543 uint32_t width, height, base_offset;
544 bool recreate_primary = false;
548 DRM_DEBUG_KMS("No FB bound\n");
553 qfb = to_qxl_framebuffer(old_fb);
554 old_bo = gem_to_qxl_bo(qfb->obj);
556 qfb = to_qxl_framebuffer(crtc->fb);
557 bo = gem_to_qxl_bo(qfb->obj);
559 /* and do we care? */
560 DRM_DEBUG("%dx%d: not a native mode\n", x, y);
562 DRM_DEBUG("%dx%d: qxl id %d\n",
563 mode->hdisplay, mode->vdisplay, m->id);
564 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
566 mode->hdisplay, mode->vdisplay,
567 adjusted_mode->hdisplay,
568 adjusted_mode->vdisplay);
570 recreate_primary = true;
572 width = mode->hdisplay;
573 height = mode->vdisplay;
576 ret = qxl_bo_reserve(bo, false);
579 ret = qxl_bo_pin(bo, bo->type, NULL);
581 qxl_bo_unreserve(bo);
584 qxl_bo_unreserve(bo);
585 if (recreate_primary) {
586 qxl_io_destroy_primary(qdev);
588 "recreate primary: %dx%d (was %dx%d,%d,%d)\n",
589 width, height, bo->surf.width,
590 bo->surf.height, bo->surf.stride, bo->surf.format);
591 qxl_io_create_primary(qdev, width, height, base_offset, bo);
592 bo->is_primary = true;
595 if (old_bo && old_bo != bo) {
596 old_bo->is_primary = false;
597 ret = qxl_bo_reserve(old_bo, false);
598 qxl_bo_unpin(old_bo);
599 qxl_bo_unreserve(old_bo);
602 if (qdev->monitors_config->count == 0) {
603 qxl_monitors_config_set_single(qdev, x, y,
607 qdev->mode_set = true;
611 static void qxl_crtc_prepare(struct drm_crtc *crtc)
613 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
614 crtc->mode.hdisplay, crtc->mode.vdisplay,
615 crtc->x, crtc->y, crtc->enabled);
618 static void qxl_crtc_commit(struct drm_crtc *crtc)
623 static void qxl_crtc_load_lut(struct drm_crtc *crtc)
628 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
629 .dpms = qxl_crtc_dpms,
630 .mode_fixup = qxl_crtc_mode_fixup,
631 .mode_set = qxl_crtc_mode_set,
632 .prepare = qxl_crtc_prepare,
633 .commit = qxl_crtc_commit,
634 .load_lut = qxl_crtc_load_lut,
637 static int qdev_crtc_init(struct drm_device *dev, int num_crtc)
639 struct qxl_crtc *qxl_crtc;
641 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
645 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
647 drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
648 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
652 static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
657 static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
658 const struct drm_display_mode *mode,
659 struct drm_display_mode *adjusted_mode)
665 static void qxl_enc_prepare(struct drm_encoder *encoder)
670 static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
671 struct drm_encoder *encoder)
674 struct qxl_head *head;
675 struct drm_display_mode *mode;
678 /* TODO: ugly, do better */
679 for (i = 0 ; (encoder->possible_crtcs != (1 << i)) && i < 32; ++i)
681 if (encoder->possible_crtcs != (1 << i)) {
682 DRM_ERROR("encoder has wrong possible_crtcs: %x\n",
683 encoder->possible_crtcs);
686 if (!qdev->monitors_config ||
687 qdev->monitors_config->max_allowed <= i) {
689 "head number too large or missing monitors config: %p, %d",
690 qdev->monitors_config,
691 qdev->monitors_config ?
692 qdev->monitors_config->max_allowed : -1);
695 if (!encoder->crtc) {
696 DRM_ERROR("missing crtc on encoder %p\n", encoder);
700 DRM_DEBUG("missing for multiple monitors: no head holes\n");
701 head = &qdev->monitors_config->heads[i];
703 head->surface_id = 0;
704 if (encoder->crtc->enabled) {
705 mode = &encoder->crtc->mode;
706 head->width = mode->hdisplay;
707 head->height = mode->vdisplay;
708 head->x = encoder->crtc->x;
709 head->y = encoder->crtc->y;
710 if (qdev->monitors_config->count < i + 1)
711 qdev->monitors_config->count = i + 1;
718 DRM_DEBUG("setting head %d to +%d+%d %dx%d\n",
719 i, head->x, head->y, head->width, head->height);
721 /* TODO - somewhere else to call this for multiple monitors
722 * (config_commit?) */
723 qxl_send_monitors_config(qdev);
726 static void qxl_enc_commit(struct drm_encoder *encoder)
728 struct qxl_device *qdev = encoder->dev->dev_private;
730 qxl_write_monitors_config_for_encoder(qdev, encoder);
734 static void qxl_enc_mode_set(struct drm_encoder *encoder,
735 struct drm_display_mode *mode,
736 struct drm_display_mode *adjusted_mode)
741 static int qxl_conn_get_modes(struct drm_connector *connector)
744 struct qxl_device *qdev = connector->dev->dev_private;
746 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
747 /* TODO: what should we do here? only show the configured modes for the
748 * device, or allow the full list, or both? */
749 if (qdev->monitors_config && qdev->monitors_config->count) {
750 ret = qxl_add_monitors_config_modes(connector);
754 ret += qxl_add_common_modes(connector);
758 static int qxl_conn_mode_valid(struct drm_connector *connector,
759 struct drm_display_mode *mode)
761 /* TODO: is this called for user defined modes? (xrandr --add-mode)
762 * TODO: check that the mode fits in the framebuffer */
763 DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay,
764 mode->vdisplay, mode->status);
768 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
770 struct qxl_output *qxl_output =
771 drm_connector_to_qxl_output(connector);
774 return &qxl_output->enc;
778 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
779 .dpms = qxl_enc_dpms,
780 .mode_fixup = qxl_enc_mode_fixup,
781 .prepare = qxl_enc_prepare,
782 .mode_set = qxl_enc_mode_set,
783 .commit = qxl_enc_commit,
786 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
787 .get_modes = qxl_conn_get_modes,
788 .mode_valid = qxl_conn_mode_valid,
789 .best_encoder = qxl_best_encoder,
792 static void qxl_conn_save(struct drm_connector *connector)
797 static void qxl_conn_restore(struct drm_connector *connector)
802 static enum drm_connector_status qxl_conn_detect(
803 struct drm_connector *connector,
806 struct qxl_output *output =
807 drm_connector_to_qxl_output(connector);
808 struct drm_device *ddev = connector->dev;
809 struct qxl_device *qdev = ddev->dev_private;
812 /* The first monitor is always connected */
813 connected = (output->index == 0) ||
814 (qdev->monitors_config &&
815 qdev->monitors_config->count > output->index);
818 return connected ? connector_status_connected
819 : connector_status_disconnected;
822 static int qxl_conn_set_property(struct drm_connector *connector,
823 struct drm_property *property,
830 static void qxl_conn_destroy(struct drm_connector *connector)
832 struct qxl_output *qxl_output =
833 drm_connector_to_qxl_output(connector);
835 drm_sysfs_connector_remove(connector);
836 drm_connector_cleanup(connector);
840 static const struct drm_connector_funcs qxl_connector_funcs = {
841 .dpms = drm_helper_connector_dpms,
842 .save = qxl_conn_save,
843 .restore = qxl_conn_restore,
844 .detect = qxl_conn_detect,
845 .fill_modes = drm_helper_probe_single_connector_modes,
846 .set_property = qxl_conn_set_property,
847 .destroy = qxl_conn_destroy,
850 static void qxl_enc_destroy(struct drm_encoder *encoder)
852 drm_encoder_cleanup(encoder);
855 static const struct drm_encoder_funcs qxl_enc_funcs = {
856 .destroy = qxl_enc_destroy,
859 static int qdev_output_init(struct drm_device *dev, int num_output)
861 struct qxl_output *qxl_output;
862 struct drm_connector *connector;
863 struct drm_encoder *encoder;
865 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
869 qxl_output->index = num_output;
871 connector = &qxl_output->base;
872 encoder = &qxl_output->enc;
873 drm_connector_init(dev, &qxl_output->base,
874 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
876 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
877 DRM_MODE_ENCODER_VIRTUAL);
879 encoder->possible_crtcs = 1 << num_output;
880 drm_mode_connector_attach_encoder(&qxl_output->base,
882 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
883 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
885 drm_sysfs_connector_add(connector);
889 static struct drm_framebuffer *
890 qxl_user_framebuffer_create(struct drm_device *dev,
891 struct drm_file *file_priv,
892 struct drm_mode_fb_cmd2 *mode_cmd)
894 struct drm_gem_object *obj;
895 struct qxl_framebuffer *qxl_fb;
896 struct qxl_device *qdev = dev->dev_private;
899 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
901 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
905 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
908 drm_gem_object_unreference_unlocked(obj);
912 if (qdev->active_user_framebuffer) {
913 DRM_INFO("%s: active_user_framebuffer %p -> %p\n",
915 qdev->active_user_framebuffer, qxl_fb);
917 qdev->active_user_framebuffer = qxl_fb;
919 return &qxl_fb->base;
922 static const struct drm_mode_config_funcs qxl_mode_funcs = {
923 .fb_create = qxl_user_framebuffer_create,
926 int qxl_modeset_init(struct qxl_device *qdev)
930 struct drm_gem_object *gobj;
931 int max_allowed = QXL_NUM_OUTPUTS;
932 int monitors_config_size = sizeof(struct qxl_monitors_config) +
933 max_allowed * sizeof(struct qxl_head);
935 drm_mode_config_init(qdev->ddev);
936 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
938 false, false, NULL, &gobj);
940 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
943 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
944 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
945 qdev->monitors_config = qdev->monitors_config_bo->kptr;
946 qdev->ram_header->monitors_config =
947 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
949 memset(qdev->monitors_config, 0, monitors_config_size);
950 qdev->monitors_config->max_allowed = max_allowed;
952 qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
954 /* modes will be validated against the framebuffer size */
955 qdev->ddev->mode_config.min_width = 320;
956 qdev->ddev->mode_config.min_height = 200;
957 qdev->ddev->mode_config.max_width = 8192;
958 qdev->ddev->mode_config.max_height = 8192;
960 qdev->ddev->mode_config.fb_base = qdev->vram_base;
961 for (i = 0 ; i < QXL_NUM_OUTPUTS; ++i) {
962 qdev_crtc_init(qdev->ddev, i);
963 qdev_output_init(qdev->ddev, i);
966 qdev->mode_info.mode_config_initialized = true;
968 /* primary surface must be created by this point, to allow
969 * issuing command queue commands and having them read by
971 qxl_fbdev_init(qdev);
975 void qxl_modeset_fini(struct qxl_device *qdev)
977 qxl_fbdev_fini(qdev);
978 if (qdev->mode_info.mode_config_initialized) {
979 drm_mode_config_cleanup(qdev->ddev);
980 qdev->mode_info.mode_config_initialized = false;