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 bool qxl_head_enabled(struct qxl_head *head)
35 return head->width && head->height;
38 void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
40 if (qdev->client_monitors_config &&
41 count > qdev->client_monitors_config->count) {
42 kfree(qdev->client_monitors_config);
43 qdev->client_monitors_config = NULL;
45 if (!qdev->client_monitors_config) {
46 qdev->client_monitors_config = kzalloc(
47 sizeof(struct qxl_monitors_config) +
48 sizeof(struct qxl_head) * count, GFP_KERNEL);
49 if (!qdev->client_monitors_config) {
51 "%s: allocation failure for %u heads\n",
56 qdev->client_monitors_config->count = count;
59 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
65 num_monitors = qdev->rom->client_monitors_config.count;
66 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
67 sizeof(qdev->rom->client_monitors_config));
68 if (crc != qdev->rom->client_monitors_config_crc) {
69 qxl_io_log(qdev, "crc mismatch: have %X (%d) != %X\n", crc,
70 sizeof(qdev->rom->client_monitors_config),
71 qdev->rom->client_monitors_config_crc);
74 if (num_monitors > qdev->monitors_config->max_allowed) {
75 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
76 qdev->monitors_config->max_allowed, num_monitors);
77 num_monitors = qdev->monitors_config->max_allowed;
79 num_monitors = qdev->rom->client_monitors_config.count;
81 qxl_alloc_client_monitors_config(qdev, num_monitors);
82 /* we copy max from the client but it isn't used */
83 qdev->client_monitors_config->max_allowed =
84 qdev->monitors_config->max_allowed;
85 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
86 struct qxl_urect *c_rect =
87 &qdev->rom->client_monitors_config.heads[i];
88 struct qxl_head *client_head =
89 &qdev->client_monitors_config->heads[i];
90 client_head->x = c_rect->left;
91 client_head->y = c_rect->top;
92 client_head->width = c_rect->right - c_rect->left;
93 client_head->height = c_rect->bottom - c_rect->top;
94 client_head->surface_id = 0;
96 client_head->flags = 0;
97 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
98 client_head->x, client_head->y);
103 void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
106 while (qxl_display_copy_rom_client_monitors_config(qdev)) {
107 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
110 drm_helper_hpd_irq_event(qdev->ddev);
113 static int qxl_add_monitors_config_modes(struct drm_connector *connector)
115 struct drm_device *dev = connector->dev;
116 struct qxl_device *qdev = dev->dev_private;
117 struct qxl_output *output = drm_connector_to_qxl_output(connector);
118 int h = output->index;
119 struct drm_display_mode *mode = NULL;
120 struct qxl_head *head;
122 if (!qdev->client_monitors_config)
124 head = &qdev->client_monitors_config->heads[h];
126 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
128 mode->type |= DRM_MODE_TYPE_PREFERRED;
129 drm_mode_probed_add(connector, mode);
133 static int qxl_add_common_modes(struct drm_connector *connector)
135 struct drm_device *dev = connector->dev;
136 struct drm_display_mode *mode = NULL;
161 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
162 if (common_modes[i].w < 320 || common_modes[i].h < 200)
165 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
166 60, false, false, false);
167 if (common_modes[i].w == 1024 && common_modes[i].h == 768)
168 mode->type |= DRM_MODE_TYPE_PREFERRED;
169 drm_mode_probed_add(connector, mode);
174 static void qxl_crtc_destroy(struct drm_crtc *crtc)
176 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
178 drm_crtc_cleanup(crtc);
183 qxl_hide_cursor(struct qxl_device *qdev)
185 struct qxl_release *release;
186 struct qxl_cursor_cmd *cmd;
189 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
192 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
193 cmd->type = QXL_CURSOR_HIDE;
194 qxl_release_unmap(qdev, release, &cmd->release_info);
196 qxl_fence_releaseable(qdev, release);
197 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
198 qxl_release_unreserve(qdev, release);
201 static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
202 struct drm_file *file_priv,
205 uint32_t height, int32_t hot_x, int32_t hot_y)
207 struct drm_device *dev = crtc->dev;
208 struct qxl_device *qdev = dev->dev_private;
209 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
210 struct drm_gem_object *obj;
211 struct qxl_cursor *cursor;
212 struct qxl_cursor_cmd *cmd;
213 struct qxl_bo *cursor_bo, *user_bo;
214 struct qxl_release *release;
220 qxl_hide_cursor(qdev);
224 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
226 DRM_ERROR("cannot find cursor object\n");
230 user_bo = gem_to_qxl_bo(obj);
232 ret = qxl_bo_reserve(user_bo, false);
236 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
240 ret = qxl_bo_kmap(user_bo, &user_ptr);
244 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
245 QXL_RELEASE_CURSOR_CMD,
249 ret = qxl_alloc_bo_reserved(qdev, sizeof(struct qxl_cursor) + size,
252 goto out_free_release;
253 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
257 cursor->header.unique = 0;
258 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
259 cursor->header.width = 64;
260 cursor->header.height = 64;
261 cursor->header.hot_spot_x = hot_x;
262 cursor->header.hot_spot_y = hot_y;
263 cursor->data_size = size;
264 cursor->chunk.next_chunk = 0;
265 cursor->chunk.prev_chunk = 0;
266 cursor->chunk.data_size = size;
268 memcpy(cursor->chunk.data, user_ptr, size);
270 qxl_bo_kunmap(cursor_bo);
272 /* finish with the userspace bo */
273 qxl_bo_kunmap(user_bo);
274 qxl_bo_unpin(user_bo);
275 qxl_bo_unreserve(user_bo);
276 drm_gem_object_unreference_unlocked(obj);
278 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
279 cmd->type = QXL_CURSOR_SET;
280 cmd->u.set.position.x = qcrtc->cur_x;
281 cmd->u.set.position.y = qcrtc->cur_y;
283 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
284 qxl_release_add_res(qdev, release, cursor_bo);
286 cmd->u.set.visible = 1;
287 qxl_release_unmap(qdev, release, &cmd->release_info);
289 qxl_fence_releaseable(qdev, release);
290 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
291 qxl_release_unreserve(qdev, release);
293 qxl_bo_unreserve(cursor_bo);
294 qxl_bo_unref(&cursor_bo);
298 qxl_bo_unref(&cursor_bo);
300 qxl_release_unreserve(qdev, release);
301 qxl_release_free(qdev, release);
303 qxl_bo_kunmap(user_bo);
305 qxl_bo_unpin(user_bo);
307 qxl_bo_unreserve(user_bo);
309 drm_gem_object_unreference_unlocked(obj);
313 static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
316 struct drm_device *dev = crtc->dev;
317 struct qxl_device *qdev = dev->dev_private;
318 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
319 struct qxl_release *release;
320 struct qxl_cursor_cmd *cmd;
323 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
329 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
330 cmd->type = QXL_CURSOR_MOVE;
331 cmd->u.position.x = qcrtc->cur_x;
332 cmd->u.position.y = qcrtc->cur_y;
333 qxl_release_unmap(qdev, release, &cmd->release_info);
335 qxl_fence_releaseable(qdev, release);
336 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
337 qxl_release_unreserve(qdev, release);
342 static const struct drm_crtc_funcs qxl_crtc_funcs = {
343 .cursor_set2 = qxl_crtc_cursor_set2,
344 .cursor_move = qxl_crtc_cursor_move,
345 .set_config = drm_crtc_helper_set_config,
346 .destroy = qxl_crtc_destroy,
349 static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
351 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
354 drm_gem_object_unreference_unlocked(qxl_fb->obj);
355 drm_framebuffer_cleanup(fb);
359 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
360 struct drm_file *file_priv,
361 unsigned flags, unsigned color,
362 struct drm_clip_rect *clips,
365 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
366 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
367 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
368 struct drm_clip_rect norect;
372 qobj = gem_to_qxl_bo(qxl_fb->obj);
373 /* if we aren't primary surface ignore this */
374 if (!qobj->is_primary)
380 norect.x1 = norect.y1 = 0;
381 norect.x2 = fb->width;
382 norect.y2 = fb->height;
383 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
385 inc = 2; /* skip source rects */
388 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
389 clips, num_clips, inc);
393 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
394 .destroy = qxl_user_framebuffer_destroy,
395 .dirty = qxl_framebuffer_surface_dirty,
397 * .create_handle = qxl_user_framebuffer_create_handle, */
401 qxl_framebuffer_init(struct drm_device *dev,
402 struct qxl_framebuffer *qfb,
403 struct drm_mode_fb_cmd2 *mode_cmd,
404 struct drm_gem_object *obj)
409 ret = drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs);
414 drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
418 static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
422 static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
423 const struct drm_display_mode *mode,
424 struct drm_display_mode *adjusted_mode)
426 struct drm_device *dev = crtc->dev;
427 struct qxl_device *qdev = dev->dev_private;
429 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
431 mode->hdisplay, mode->vdisplay,
432 adjusted_mode->hdisplay,
433 adjusted_mode->vdisplay);
438 qxl_send_monitors_config(struct qxl_device *qdev)
442 BUG_ON(!qdev->ram_header->monitors_config);
444 if (qdev->monitors_config->count == 0) {
445 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
448 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
449 struct qxl_head *head = &qdev->monitors_config->heads[i];
451 if (head->y > 8192 || head->x > 8192 ||
452 head->width > 8192 || head->height > 8192) {
453 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
454 i, head->width, head->height,
459 qxl_io_monitors_config(qdev);
462 static void qxl_monitors_config_set(struct qxl_device *qdev,
464 unsigned x, unsigned y,
465 unsigned width, unsigned height,
468 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
469 qdev->monitors_config->heads[index].x = x;
470 qdev->monitors_config->heads[index].y = y;
471 qdev->monitors_config->heads[index].width = width;
472 qdev->monitors_config->heads[index].height = height;
473 qdev->monitors_config->heads[index].surface_id = surf_id;
477 static int qxl_crtc_mode_set(struct drm_crtc *crtc,
478 struct drm_display_mode *mode,
479 struct drm_display_mode *adjusted_mode,
481 struct drm_framebuffer *old_fb)
483 struct drm_device *dev = crtc->dev;
484 struct qxl_device *qdev = dev->dev_private;
485 struct qxl_mode *m = (void *)mode->private;
486 struct qxl_framebuffer *qfb;
487 struct qxl_bo *bo, *old_bo = NULL;
488 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
489 uint32_t width, height, base_offset;
490 bool recreate_primary = false;
494 DRM_DEBUG_KMS("No FB bound\n");
499 qfb = to_qxl_framebuffer(old_fb);
500 old_bo = gem_to_qxl_bo(qfb->obj);
502 qfb = to_qxl_framebuffer(crtc->fb);
503 bo = gem_to_qxl_bo(qfb->obj);
505 /* and do we care? */
506 DRM_DEBUG("%dx%d: not a native mode\n", x, y);
508 DRM_DEBUG("%dx%d: qxl id %d\n",
509 mode->hdisplay, mode->vdisplay, m->id);
510 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
512 mode->hdisplay, mode->vdisplay,
513 adjusted_mode->hdisplay,
514 adjusted_mode->vdisplay);
516 if (qcrtc->index == 0)
517 recreate_primary = true;
519 width = mode->hdisplay;
520 height = mode->vdisplay;
523 ret = qxl_bo_reserve(bo, false);
526 ret = qxl_bo_pin(bo, bo->type, NULL);
528 qxl_bo_unreserve(bo);
531 qxl_bo_unreserve(bo);
532 if (recreate_primary) {
533 qxl_io_destroy_primary(qdev);
535 "recreate primary: %dx%d (was %dx%d,%d,%d)\n",
536 width, height, bo->surf.width,
537 bo->surf.height, bo->surf.stride, bo->surf.format);
538 qxl_io_create_primary(qdev, base_offset, bo);
539 bo->is_primary = true;
542 surf_id = bo->surface_id;
545 if (old_bo && old_bo != bo) {
546 old_bo->is_primary = false;
547 ret = qxl_bo_reserve(old_bo, false);
548 qxl_bo_unpin(old_bo);
549 qxl_bo_unreserve(old_bo);
552 qxl_monitors_config_set(qdev, qcrtc->index, x, y,
554 mode->vdisplay, surf_id);
558 static void qxl_crtc_prepare(struct drm_crtc *crtc)
560 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
561 crtc->mode.hdisplay, crtc->mode.vdisplay,
562 crtc->x, crtc->y, crtc->enabled);
565 static void qxl_crtc_commit(struct drm_crtc *crtc)
570 static void qxl_crtc_disable(struct drm_crtc *crtc)
572 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
573 struct drm_device *dev = crtc->dev;
574 struct qxl_device *qdev = dev->dev_private;
576 struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->fb);
577 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
579 ret = qxl_bo_reserve(bo, false);
581 qxl_bo_unreserve(bo);
585 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
587 qxl_send_monitors_config(qdev);
590 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
591 .dpms = qxl_crtc_dpms,
592 .disable = qxl_crtc_disable,
593 .mode_fixup = qxl_crtc_mode_fixup,
594 .mode_set = qxl_crtc_mode_set,
595 .prepare = qxl_crtc_prepare,
596 .commit = qxl_crtc_commit,
599 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
601 struct qxl_crtc *qxl_crtc;
603 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
607 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
608 qxl_crtc->index = crtc_id;
609 drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
610 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
614 static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
619 static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
620 const struct drm_display_mode *mode,
621 struct drm_display_mode *adjusted_mode)
627 static void qxl_enc_prepare(struct drm_encoder *encoder)
632 static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
633 struct drm_encoder *encoder)
636 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
637 struct qxl_head *head;
638 struct drm_display_mode *mode;
641 /* TODO: ugly, do better */
643 if (!qdev->monitors_config ||
644 qdev->monitors_config->max_allowed <= i) {
646 "head number too large or missing monitors config: %p, %d",
647 qdev->monitors_config,
648 qdev->monitors_config ?
649 qdev->monitors_config->max_allowed : -1);
652 if (!encoder->crtc) {
653 DRM_ERROR("missing crtc on encoder %p\n", encoder);
657 DRM_DEBUG("missing for multiple monitors: no head holes\n");
658 head = &qdev->monitors_config->heads[i];
660 if (encoder->crtc->enabled) {
661 mode = &encoder->crtc->mode;
662 head->width = mode->hdisplay;
663 head->height = mode->vdisplay;
664 head->x = encoder->crtc->x;
665 head->y = encoder->crtc->y;
666 if (qdev->monitors_config->count < i + 1)
667 qdev->monitors_config->count = i + 1;
674 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
675 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
677 /* TODO - somewhere else to call this for multiple monitors
678 * (config_commit?) */
679 qxl_send_monitors_config(qdev);
682 static void qxl_enc_commit(struct drm_encoder *encoder)
684 struct qxl_device *qdev = encoder->dev->dev_private;
686 qxl_write_monitors_config_for_encoder(qdev, encoder);
690 static void qxl_enc_mode_set(struct drm_encoder *encoder,
691 struct drm_display_mode *mode,
692 struct drm_display_mode *adjusted_mode)
697 static int qxl_conn_get_modes(struct drm_connector *connector)
700 struct qxl_device *qdev = connector->dev->dev_private;
702 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
703 /* TODO: what should we do here? only show the configured modes for the
704 * device, or allow the full list, or both? */
705 if (qdev->monitors_config && qdev->monitors_config->count) {
706 ret = qxl_add_monitors_config_modes(connector);
710 ret += qxl_add_common_modes(connector);
714 static int qxl_conn_mode_valid(struct drm_connector *connector,
715 struct drm_display_mode *mode)
717 /* TODO: is this called for user defined modes? (xrandr --add-mode)
718 * TODO: check that the mode fits in the framebuffer */
719 DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay,
720 mode->vdisplay, mode->status);
724 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
726 struct qxl_output *qxl_output =
727 drm_connector_to_qxl_output(connector);
730 return &qxl_output->enc;
734 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
735 .dpms = qxl_enc_dpms,
736 .mode_fixup = qxl_enc_mode_fixup,
737 .prepare = qxl_enc_prepare,
738 .mode_set = qxl_enc_mode_set,
739 .commit = qxl_enc_commit,
742 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
743 .get_modes = qxl_conn_get_modes,
744 .mode_valid = qxl_conn_mode_valid,
745 .best_encoder = qxl_best_encoder,
748 static void qxl_conn_save(struct drm_connector *connector)
753 static void qxl_conn_restore(struct drm_connector *connector)
758 static enum drm_connector_status qxl_conn_detect(
759 struct drm_connector *connector,
762 struct qxl_output *output =
763 drm_connector_to_qxl_output(connector);
764 struct drm_device *ddev = connector->dev;
765 struct qxl_device *qdev = ddev->dev_private;
768 /* The first monitor is always connected */
769 connected = (output->index == 0) ||
770 (qdev->client_monitors_config &&
771 qdev->client_monitors_config->count > output->index &&
772 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]));
775 return connected ? connector_status_connected
776 : connector_status_disconnected;
779 static int qxl_conn_set_property(struct drm_connector *connector,
780 struct drm_property *property,
787 static void qxl_conn_destroy(struct drm_connector *connector)
789 struct qxl_output *qxl_output =
790 drm_connector_to_qxl_output(connector);
792 drm_sysfs_connector_remove(connector);
793 drm_connector_cleanup(connector);
797 static const struct drm_connector_funcs qxl_connector_funcs = {
798 .dpms = drm_helper_connector_dpms,
799 .save = qxl_conn_save,
800 .restore = qxl_conn_restore,
801 .detect = qxl_conn_detect,
802 .fill_modes = drm_helper_probe_single_connector_modes,
803 .set_property = qxl_conn_set_property,
804 .destroy = qxl_conn_destroy,
807 static void qxl_enc_destroy(struct drm_encoder *encoder)
809 drm_encoder_cleanup(encoder);
812 static const struct drm_encoder_funcs qxl_enc_funcs = {
813 .destroy = qxl_enc_destroy,
816 static int qdev_output_init(struct drm_device *dev, int num_output)
818 struct qxl_output *qxl_output;
819 struct drm_connector *connector;
820 struct drm_encoder *encoder;
822 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
826 qxl_output->index = num_output;
828 connector = &qxl_output->base;
829 encoder = &qxl_output->enc;
830 drm_connector_init(dev, &qxl_output->base,
831 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
833 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
834 DRM_MODE_ENCODER_VIRTUAL);
836 /* we get HPD via client monitors config */
837 connector->polled = DRM_CONNECTOR_POLL_HPD;
838 encoder->possible_crtcs = 1 << num_output;
839 drm_mode_connector_attach_encoder(&qxl_output->base,
841 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
842 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
844 drm_sysfs_connector_add(connector);
848 static struct drm_framebuffer *
849 qxl_user_framebuffer_create(struct drm_device *dev,
850 struct drm_file *file_priv,
851 struct drm_mode_fb_cmd2 *mode_cmd)
853 struct drm_gem_object *obj;
854 struct qxl_framebuffer *qxl_fb;
857 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
859 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
863 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
866 drm_gem_object_unreference_unlocked(obj);
870 return &qxl_fb->base;
873 static const struct drm_mode_config_funcs qxl_mode_funcs = {
874 .fb_create = qxl_user_framebuffer_create,
877 int qxl_create_monitors_object(struct qxl_device *qdev)
880 struct drm_gem_object *gobj;
881 int max_allowed = qxl_num_crtc;
882 int monitors_config_size = sizeof(struct qxl_monitors_config) +
883 max_allowed * sizeof(struct qxl_head);
885 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
887 false, false, NULL, &gobj);
889 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
892 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
894 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
898 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
900 qxl_bo_unreserve(qdev->monitors_config_bo);
904 qxl_bo_unreserve(qdev->monitors_config_bo);
906 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
908 qdev->monitors_config = qdev->monitors_config_bo->kptr;
909 qdev->ram_header->monitors_config =
910 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
912 memset(qdev->monitors_config, 0, monitors_config_size);
913 qdev->monitors_config->max_allowed = max_allowed;
917 int qxl_destroy_monitors_object(struct qxl_device *qdev)
921 qdev->monitors_config = NULL;
922 qdev->ram_header->monitors_config = 0;
924 qxl_bo_kunmap(qdev->monitors_config_bo);
925 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
929 qxl_bo_unpin(qdev->monitors_config_bo);
930 qxl_bo_unreserve(qdev->monitors_config_bo);
932 qxl_bo_unref(&qdev->monitors_config_bo);
936 int qxl_modeset_init(struct qxl_device *qdev)
941 drm_mode_config_init(qdev->ddev);
943 ret = qxl_create_monitors_object(qdev);
947 qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
949 /* modes will be validated against the framebuffer size */
950 qdev->ddev->mode_config.min_width = 320;
951 qdev->ddev->mode_config.min_height = 200;
952 qdev->ddev->mode_config.max_width = 8192;
953 qdev->ddev->mode_config.max_height = 8192;
955 qdev->ddev->mode_config.fb_base = qdev->vram_base;
956 for (i = 0 ; i < qxl_num_crtc; ++i) {
957 qdev_crtc_init(qdev->ddev, i);
958 qdev_output_init(qdev->ddev, i);
961 qdev->mode_info.mode_config_initialized = true;
963 /* primary surface must be created by this point, to allow
964 * issuing command queue commands and having them read by
966 qxl_fbdev_init(qdev);
970 void qxl_modeset_fini(struct qxl_device *qdev)
972 qxl_fbdev_fini(qdev);
974 qxl_destroy_monitors_object(qdev);
975 if (qdev->mode_info.mode_config_initialized) {
976 drm_mode_config_cleanup(qdev->ddev);
977 qdev->mode_info.mode_config_initialized = false;