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_destroy(struct drm_crtc *crtc)
227 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
229 drm_crtc_cleanup(crtc);
234 qxl_hide_cursor(struct qxl_device *qdev)
236 struct qxl_release *release;
237 struct qxl_cursor_cmd *cmd;
240 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
243 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
244 cmd->type = QXL_CURSOR_HIDE;
245 qxl_release_unmap(qdev, release, &cmd->release_info);
247 qxl_fence_releaseable(qdev, release);
248 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
249 qxl_release_unreserve(qdev, release);
252 static int qxl_crtc_cursor_set(struct drm_crtc *crtc,
253 struct drm_file *file_priv,
258 struct drm_device *dev = crtc->dev;
259 struct qxl_device *qdev = dev->dev_private;
260 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
261 struct drm_gem_object *obj;
262 struct qxl_cursor *cursor;
263 struct qxl_cursor_cmd *cmd;
264 struct qxl_bo *cursor_bo, *user_bo;
265 struct qxl_release *release;
271 qxl_hide_cursor(qdev);
275 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
277 DRM_ERROR("cannot find cursor object\n");
281 user_bo = gem_to_qxl_bo(obj);
283 ret = qxl_bo_reserve(user_bo, false);
287 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
291 ret = qxl_bo_kmap(user_bo, &user_ptr);
295 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
296 QXL_RELEASE_CURSOR_CMD,
300 ret = qxl_alloc_bo_reserved(qdev, sizeof(struct qxl_cursor) + size,
303 goto out_free_release;
304 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
308 cursor->header.unique = 0;
309 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
310 cursor->header.width = 64;
311 cursor->header.height = 64;
312 cursor->header.hot_spot_x = 0;
313 cursor->header.hot_spot_y = 0;
314 cursor->data_size = size;
315 cursor->chunk.next_chunk = 0;
316 cursor->chunk.prev_chunk = 0;
317 cursor->chunk.data_size = size;
319 memcpy(cursor->chunk.data, user_ptr, size);
321 qxl_bo_kunmap(cursor_bo);
323 /* finish with the userspace bo */
324 qxl_bo_kunmap(user_bo);
325 qxl_bo_unpin(user_bo);
326 qxl_bo_unreserve(user_bo);
327 drm_gem_object_unreference_unlocked(obj);
329 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
330 cmd->type = QXL_CURSOR_SET;
331 cmd->u.set.position.x = qcrtc->cur_x;
332 cmd->u.set.position.y = qcrtc->cur_y;
334 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
335 qxl_release_add_res(qdev, release, cursor_bo);
337 cmd->u.set.visible = 1;
338 qxl_release_unmap(qdev, release, &cmd->release_info);
340 qxl_fence_releaseable(qdev, release);
341 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
342 qxl_release_unreserve(qdev, release);
344 qxl_bo_unreserve(cursor_bo);
345 qxl_bo_unref(&cursor_bo);
349 qxl_bo_unref(&cursor_bo);
351 qxl_release_unreserve(qdev, release);
352 qxl_release_free(qdev, release);
354 qxl_bo_kunmap(user_bo);
356 qxl_bo_unpin(user_bo);
358 qxl_bo_unreserve(user_bo);
360 drm_gem_object_unreference_unlocked(obj);
364 static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
367 struct drm_device *dev = crtc->dev;
368 struct qxl_device *qdev = dev->dev_private;
369 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
370 struct qxl_release *release;
371 struct qxl_cursor_cmd *cmd;
374 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
380 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
381 cmd->type = QXL_CURSOR_MOVE;
382 cmd->u.position.x = qcrtc->cur_x;
383 cmd->u.position.y = qcrtc->cur_y;
384 qxl_release_unmap(qdev, release, &cmd->release_info);
386 qxl_fence_releaseable(qdev, release);
387 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
388 qxl_release_unreserve(qdev, release);
393 static const struct drm_crtc_funcs qxl_crtc_funcs = {
394 .cursor_set = qxl_crtc_cursor_set,
395 .cursor_move = qxl_crtc_cursor_move,
396 .set_config = drm_crtc_helper_set_config,
397 .destroy = qxl_crtc_destroy,
400 static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
402 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
405 drm_gem_object_unreference_unlocked(qxl_fb->obj);
406 drm_framebuffer_cleanup(fb);
410 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
411 struct drm_file *file_priv,
412 unsigned flags, unsigned color,
413 struct drm_clip_rect *clips,
416 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
417 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
418 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
419 struct drm_clip_rect norect;
423 qobj = gem_to_qxl_bo(qxl_fb->obj);
424 /* if we aren't primary surface ignore this */
425 if (!qobj->is_primary)
431 norect.x1 = norect.y1 = 0;
432 norect.x2 = fb->width;
433 norect.y2 = fb->height;
434 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
436 inc = 2; /* skip source rects */
439 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
440 clips, num_clips, inc);
444 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
445 .destroy = qxl_user_framebuffer_destroy,
446 .dirty = qxl_framebuffer_surface_dirty,
448 * .create_handle = qxl_user_framebuffer_create_handle, */
452 qxl_framebuffer_init(struct drm_device *dev,
453 struct qxl_framebuffer *qfb,
454 struct drm_mode_fb_cmd2 *mode_cmd,
455 struct drm_gem_object *obj)
460 ret = drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs);
465 drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
469 static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
473 static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
474 const struct drm_display_mode *mode,
475 struct drm_display_mode *adjusted_mode)
477 struct drm_device *dev = crtc->dev;
478 struct qxl_device *qdev = dev->dev_private;
480 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
482 mode->hdisplay, mode->vdisplay,
483 adjusted_mode->hdisplay,
484 adjusted_mode->vdisplay);
489 qxl_send_monitors_config(struct qxl_device *qdev)
493 BUG_ON(!qdev->ram_header->monitors_config);
495 if (qdev->monitors_config->count == 0) {
496 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
499 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
500 struct qxl_head *head = &qdev->monitors_config->heads[i];
502 if (head->y > 8192 || head->y < head->x ||
503 head->width > 8192 || head->height > 8192) {
504 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
505 i, head->width, head->height,
510 qxl_io_monitors_config(qdev);
513 static void qxl_monitors_config_set_single(struct qxl_device *qdev,
514 unsigned x, unsigned y,
515 unsigned width, unsigned height)
517 DRM_DEBUG("%dx%d+%d+%d\n", width, height, x, y);
518 qdev->monitors_config->count = 1;
519 qdev->monitors_config->heads[0].x = x;
520 qdev->monitors_config->heads[0].y = y;
521 qdev->monitors_config->heads[0].width = width;
522 qdev->monitors_config->heads[0].height = height;
525 static int qxl_crtc_mode_set(struct drm_crtc *crtc,
526 struct drm_display_mode *mode,
527 struct drm_display_mode *adjusted_mode,
529 struct drm_framebuffer *old_fb)
531 struct drm_device *dev = crtc->dev;
532 struct qxl_device *qdev = dev->dev_private;
533 struct qxl_mode *m = (void *)mode->private;
534 struct qxl_framebuffer *qfb;
535 struct qxl_bo *bo, *old_bo = NULL;
536 uint32_t width, height, base_offset;
537 bool recreate_primary = false;
541 DRM_DEBUG_KMS("No FB bound\n");
546 qfb = to_qxl_framebuffer(old_fb);
547 old_bo = gem_to_qxl_bo(qfb->obj);
549 qfb = to_qxl_framebuffer(crtc->fb);
550 bo = gem_to_qxl_bo(qfb->obj);
552 /* and do we care? */
553 DRM_DEBUG("%dx%d: not a native mode\n", x, y);
555 DRM_DEBUG("%dx%d: qxl id %d\n",
556 mode->hdisplay, mode->vdisplay, m->id);
557 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
559 mode->hdisplay, mode->vdisplay,
560 adjusted_mode->hdisplay,
561 adjusted_mode->vdisplay);
563 recreate_primary = true;
565 width = mode->hdisplay;
566 height = mode->vdisplay;
569 ret = qxl_bo_reserve(bo, false);
572 ret = qxl_bo_pin(bo, bo->type, NULL);
574 qxl_bo_unreserve(bo);
577 qxl_bo_unreserve(bo);
578 if (recreate_primary) {
579 qxl_io_destroy_primary(qdev);
581 "recreate primary: %dx%d (was %dx%d,%d,%d)\n",
582 width, height, bo->surf.width,
583 bo->surf.height, bo->surf.stride, bo->surf.format);
584 qxl_io_create_primary(qdev, width, height, base_offset, bo);
585 bo->is_primary = true;
588 if (old_bo && old_bo != bo) {
589 old_bo->is_primary = false;
590 ret = qxl_bo_reserve(old_bo, false);
591 qxl_bo_unpin(old_bo);
592 qxl_bo_unreserve(old_bo);
595 if (qdev->monitors_config->count == 0) {
596 qxl_monitors_config_set_single(qdev, x, y,
603 static void qxl_crtc_prepare(struct drm_crtc *crtc)
605 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
606 crtc->mode.hdisplay, crtc->mode.vdisplay,
607 crtc->x, crtc->y, crtc->enabled);
610 static void qxl_crtc_commit(struct drm_crtc *crtc)
615 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
616 .dpms = qxl_crtc_dpms,
617 .mode_fixup = qxl_crtc_mode_fixup,
618 .mode_set = qxl_crtc_mode_set,
619 .prepare = qxl_crtc_prepare,
620 .commit = qxl_crtc_commit,
623 static int qdev_crtc_init(struct drm_device *dev, int num_crtc)
625 struct qxl_crtc *qxl_crtc;
627 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
631 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
633 drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
634 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
638 static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
643 static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
644 const struct drm_display_mode *mode,
645 struct drm_display_mode *adjusted_mode)
651 static void qxl_enc_prepare(struct drm_encoder *encoder)
656 static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
657 struct drm_encoder *encoder)
660 struct qxl_head *head;
661 struct drm_display_mode *mode;
664 /* TODO: ugly, do better */
665 for (i = 0 ; (encoder->possible_crtcs != (1 << i)) && i < 32; ++i)
667 if (encoder->possible_crtcs != (1 << i)) {
668 DRM_ERROR("encoder has wrong possible_crtcs: %x\n",
669 encoder->possible_crtcs);
672 if (!qdev->monitors_config ||
673 qdev->monitors_config->max_allowed <= i) {
675 "head number too large or missing monitors config: %p, %d",
676 qdev->monitors_config,
677 qdev->monitors_config ?
678 qdev->monitors_config->max_allowed : -1);
681 if (!encoder->crtc) {
682 DRM_ERROR("missing crtc on encoder %p\n", encoder);
686 DRM_DEBUG("missing for multiple monitors: no head holes\n");
687 head = &qdev->monitors_config->heads[i];
689 head->surface_id = 0;
690 if (encoder->crtc->enabled) {
691 mode = &encoder->crtc->mode;
692 head->width = mode->hdisplay;
693 head->height = mode->vdisplay;
694 head->x = encoder->crtc->x;
695 head->y = encoder->crtc->y;
696 if (qdev->monitors_config->count < i + 1)
697 qdev->monitors_config->count = i + 1;
704 DRM_DEBUG("setting head %d to +%d+%d %dx%d\n",
705 i, head->x, head->y, head->width, head->height);
707 /* TODO - somewhere else to call this for multiple monitors
708 * (config_commit?) */
709 qxl_send_monitors_config(qdev);
712 static void qxl_enc_commit(struct drm_encoder *encoder)
714 struct qxl_device *qdev = encoder->dev->dev_private;
716 qxl_write_monitors_config_for_encoder(qdev, encoder);
720 static void qxl_enc_mode_set(struct drm_encoder *encoder,
721 struct drm_display_mode *mode,
722 struct drm_display_mode *adjusted_mode)
727 static int qxl_conn_get_modes(struct drm_connector *connector)
730 struct qxl_device *qdev = connector->dev->dev_private;
732 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
733 /* TODO: what should we do here? only show the configured modes for the
734 * device, or allow the full list, or both? */
735 if (qdev->monitors_config && qdev->monitors_config->count) {
736 ret = qxl_add_monitors_config_modes(connector);
740 ret += qxl_add_common_modes(connector);
744 static int qxl_conn_mode_valid(struct drm_connector *connector,
745 struct drm_display_mode *mode)
747 /* TODO: is this called for user defined modes? (xrandr --add-mode)
748 * TODO: check that the mode fits in the framebuffer */
749 DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay,
750 mode->vdisplay, mode->status);
754 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
756 struct qxl_output *qxl_output =
757 drm_connector_to_qxl_output(connector);
760 return &qxl_output->enc;
764 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
765 .dpms = qxl_enc_dpms,
766 .mode_fixup = qxl_enc_mode_fixup,
767 .prepare = qxl_enc_prepare,
768 .mode_set = qxl_enc_mode_set,
769 .commit = qxl_enc_commit,
772 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
773 .get_modes = qxl_conn_get_modes,
774 .mode_valid = qxl_conn_mode_valid,
775 .best_encoder = qxl_best_encoder,
778 static void qxl_conn_save(struct drm_connector *connector)
783 static void qxl_conn_restore(struct drm_connector *connector)
788 static enum drm_connector_status qxl_conn_detect(
789 struct drm_connector *connector,
792 struct qxl_output *output =
793 drm_connector_to_qxl_output(connector);
794 struct drm_device *ddev = connector->dev;
795 struct qxl_device *qdev = ddev->dev_private;
798 /* The first monitor is always connected */
799 connected = (output->index == 0) ||
800 (qdev->monitors_config &&
801 qdev->monitors_config->count > output->index);
804 return connected ? connector_status_connected
805 : connector_status_disconnected;
808 static int qxl_conn_set_property(struct drm_connector *connector,
809 struct drm_property *property,
816 static void qxl_conn_destroy(struct drm_connector *connector)
818 struct qxl_output *qxl_output =
819 drm_connector_to_qxl_output(connector);
821 drm_sysfs_connector_remove(connector);
822 drm_connector_cleanup(connector);
826 static const struct drm_connector_funcs qxl_connector_funcs = {
827 .dpms = drm_helper_connector_dpms,
828 .save = qxl_conn_save,
829 .restore = qxl_conn_restore,
830 .detect = qxl_conn_detect,
831 .fill_modes = drm_helper_probe_single_connector_modes,
832 .set_property = qxl_conn_set_property,
833 .destroy = qxl_conn_destroy,
836 static void qxl_enc_destroy(struct drm_encoder *encoder)
838 drm_encoder_cleanup(encoder);
841 static const struct drm_encoder_funcs qxl_enc_funcs = {
842 .destroy = qxl_enc_destroy,
845 static int qdev_output_init(struct drm_device *dev, int num_output)
847 struct qxl_output *qxl_output;
848 struct drm_connector *connector;
849 struct drm_encoder *encoder;
851 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
855 qxl_output->index = num_output;
857 connector = &qxl_output->base;
858 encoder = &qxl_output->enc;
859 drm_connector_init(dev, &qxl_output->base,
860 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
862 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
863 DRM_MODE_ENCODER_VIRTUAL);
865 encoder->possible_crtcs = 1 << num_output;
866 drm_mode_connector_attach_encoder(&qxl_output->base,
868 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
869 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
871 drm_sysfs_connector_add(connector);
875 static struct drm_framebuffer *
876 qxl_user_framebuffer_create(struct drm_device *dev,
877 struct drm_file *file_priv,
878 struct drm_mode_fb_cmd2 *mode_cmd)
880 struct drm_gem_object *obj;
881 struct qxl_framebuffer *qxl_fb;
884 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
886 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
890 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
893 drm_gem_object_unreference_unlocked(obj);
897 return &qxl_fb->base;
900 static const struct drm_mode_config_funcs qxl_mode_funcs = {
901 .fb_create = qxl_user_framebuffer_create,
904 int qxl_modeset_init(struct qxl_device *qdev)
908 struct drm_gem_object *gobj;
909 int max_allowed = QXL_NUM_OUTPUTS;
910 int monitors_config_size = sizeof(struct qxl_monitors_config) +
911 max_allowed * sizeof(struct qxl_head);
913 drm_mode_config_init(qdev->ddev);
914 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
916 false, false, NULL, &gobj);
918 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
921 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
922 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
923 qdev->monitors_config = qdev->monitors_config_bo->kptr;
924 qdev->ram_header->monitors_config =
925 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
927 memset(qdev->monitors_config, 0, monitors_config_size);
928 qdev->monitors_config->max_allowed = max_allowed;
930 qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
932 /* modes will be validated against the framebuffer size */
933 qdev->ddev->mode_config.min_width = 320;
934 qdev->ddev->mode_config.min_height = 200;
935 qdev->ddev->mode_config.max_width = 8192;
936 qdev->ddev->mode_config.max_height = 8192;
938 qdev->ddev->mode_config.fb_base = qdev->vram_base;
939 for (i = 0 ; i < QXL_NUM_OUTPUTS; ++i) {
940 qdev_crtc_init(qdev->ddev, i);
941 qdev_output_init(qdev->ddev, i);
944 qdev->mode_info.mode_config_initialized = true;
946 /* primary surface must be created by this point, to allow
947 * issuing command queue commands and having them read by
949 qxl_fbdev_init(qdev);
953 void qxl_modeset_fini(struct qxl_device *qdev)
955 qxl_fbdev_fini(qdev);
956 if (qdev->mode_info.mode_config_initialized) {
957 drm_mode_config_cleanup(qdev->ddev);
958 qdev->mode_info.mode_config_initialized = false;