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>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_atomic.h>
36 static bool qxl_head_enabled(struct qxl_head *head)
38 return head->width && head->height;
41 static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
43 if (qdev->client_monitors_config &&
44 count > qdev->client_monitors_config->count) {
45 kfree(qdev->client_monitors_config);
46 qdev->client_monitors_config = NULL;
48 if (!qdev->client_monitors_config) {
49 qdev->client_monitors_config = kzalloc(
50 sizeof(struct qxl_monitors_config) +
51 sizeof(struct qxl_head) * count, GFP_KERNEL);
52 if (!qdev->client_monitors_config) {
54 "%s: allocation failure for %u heads\n",
59 qdev->client_monitors_config->count = count;
63 MONITORS_CONFIG_MODIFIED,
64 MONITORS_CONFIG_UNCHANGED,
65 MONITORS_CONFIG_BAD_CRC,
68 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
73 int status = MONITORS_CONFIG_UNCHANGED;
75 num_monitors = qdev->rom->client_monitors_config.count;
76 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
77 sizeof(qdev->rom->client_monitors_config));
78 if (crc != qdev->rom->client_monitors_config_crc) {
79 qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
80 sizeof(qdev->rom->client_monitors_config),
81 qdev->rom->client_monitors_config_crc);
82 return MONITORS_CONFIG_BAD_CRC;
84 if (num_monitors > qdev->monitors_config->max_allowed) {
85 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
86 qdev->monitors_config->max_allowed, num_monitors);
87 num_monitors = qdev->monitors_config->max_allowed;
89 num_monitors = qdev->rom->client_monitors_config.count;
91 if (qdev->client_monitors_config
92 && (num_monitors != qdev->client_monitors_config->count)) {
93 status = MONITORS_CONFIG_MODIFIED;
95 qxl_alloc_client_monitors_config(qdev, num_monitors);
96 /* we copy max from the client but it isn't used */
97 qdev->client_monitors_config->max_allowed =
98 qdev->monitors_config->max_allowed;
99 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
100 struct qxl_urect *c_rect =
101 &qdev->rom->client_monitors_config.heads[i];
102 struct qxl_head *client_head =
103 &qdev->client_monitors_config->heads[i];
104 if (client_head->x != c_rect->left) {
105 client_head->x = c_rect->left;
106 status = MONITORS_CONFIG_MODIFIED;
108 if (client_head->y != c_rect->top) {
109 client_head->y = c_rect->top;
110 status = MONITORS_CONFIG_MODIFIED;
112 if (client_head->width != c_rect->right - c_rect->left) {
113 client_head->width = c_rect->right - c_rect->left;
114 status = MONITORS_CONFIG_MODIFIED;
116 if (client_head->height != c_rect->bottom - c_rect->top) {
117 client_head->height = c_rect->bottom - c_rect->top;
118 status = MONITORS_CONFIG_MODIFIED;
120 if (client_head->surface_id != 0) {
121 client_head->surface_id = 0;
122 status = MONITORS_CONFIG_MODIFIED;
124 if (client_head->id != i) {
126 status = MONITORS_CONFIG_MODIFIED;
128 if (client_head->flags != 0) {
129 client_head->flags = 0;
130 status = MONITORS_CONFIG_MODIFIED;
132 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
133 client_head->x, client_head->y);
139 static void qxl_update_offset_props(struct qxl_device *qdev)
141 struct drm_device *dev = &qdev->ddev;
142 struct drm_connector *connector;
143 struct qxl_output *output;
144 struct qxl_head *head;
146 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
147 output = drm_connector_to_qxl_output(connector);
149 head = &qdev->client_monitors_config->heads[output->index];
151 drm_object_property_set_value(&connector->base,
152 dev->mode_config.suggested_x_property, head->x);
153 drm_object_property_set_value(&connector->base,
154 dev->mode_config.suggested_y_property, head->y);
158 void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
161 struct drm_device *dev = &qdev->ddev;
164 status = qxl_display_copy_rom_client_monitors_config(qdev);
165 while (status == MONITORS_CONFIG_BAD_CRC) {
166 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
168 status = qxl_display_copy_rom_client_monitors_config(qdev);
170 if (status == MONITORS_CONFIG_UNCHANGED) {
171 qxl_io_log(qdev, "config unchanged\n");
172 DRM_DEBUG("ignoring unchanged client monitors config");
176 drm_modeset_lock_all(dev);
177 qxl_update_offset_props(qdev);
178 drm_modeset_unlock_all(dev);
179 if (!drm_helper_hpd_irq_event(dev)) {
180 /* notify that the monitor configuration changed, to
181 adjust at the arbitrary resolution */
182 drm_kms_helper_hotplug_event(dev);
186 static int qxl_add_monitors_config_modes(struct drm_connector *connector,
190 struct drm_device *dev = connector->dev;
191 struct qxl_device *qdev = dev->dev_private;
192 struct qxl_output *output = drm_connector_to_qxl_output(connector);
193 int h = output->index;
194 struct drm_display_mode *mode = NULL;
195 struct qxl_head *head;
197 if (!qdev->client_monitors_config)
199 head = &qdev->client_monitors_config->heads[h];
201 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
203 mode->type |= DRM_MODE_TYPE_PREFERRED;
204 mode->hdisplay = head->width;
205 mode->vdisplay = head->height;
206 drm_mode_set_name(mode);
207 *pwidth = head->width;
208 *pheight = head->height;
209 drm_mode_probed_add(connector, mode);
210 /* remember the last custom size for mode validation */
211 qdev->monitors_config_width = mode->hdisplay;
212 qdev->monitors_config_height = mode->vdisplay;
216 static struct mode_size {
239 static int qxl_add_common_modes(struct drm_connector *connector,
243 struct drm_device *dev = connector->dev;
244 struct drm_display_mode *mode = NULL;
246 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
247 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
248 60, false, false, false);
249 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
250 mode->type |= DRM_MODE_TYPE_PREFERRED;
251 drm_mode_probed_add(connector, mode);
256 static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
257 struct drm_crtc_state *old_crtc_state)
259 struct drm_device *dev = crtc->dev;
260 struct drm_pending_vblank_event *event;
263 if (crtc->state && crtc->state->event) {
264 event = crtc->state->event;
265 crtc->state->event = NULL;
267 spin_lock_irqsave(&dev->event_lock, flags);
268 drm_crtc_send_vblank_event(crtc, event);
269 spin_unlock_irqrestore(&dev->event_lock, flags);
273 static void qxl_crtc_destroy(struct drm_crtc *crtc)
275 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
277 drm_crtc_cleanup(crtc);
281 static int qxl_crtc_page_flip(struct drm_crtc *crtc,
282 struct drm_framebuffer *fb,
283 struct drm_pending_vblank_event *event,
284 uint32_t page_flip_flags)
286 struct drm_device *dev = crtc->dev;
287 struct qxl_device *qdev = dev->dev_private;
288 struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
289 struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
290 struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
291 struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
293 struct drm_clip_rect norect = {
300 int one_clip_rect = 1;
303 drm_atomic_set_fb_for_plane(crtc->primary->state, fb);
305 bo_old->is_primary = false;
306 bo->is_primary = true;
308 ret = qxl_bo_pin(bo, bo->type, NULL);
312 qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
313 &norect, one_clip_rect, inc);
316 spin_lock_irqsave(&dev->event_lock, flags);
317 drm_crtc_send_vblank_event(crtc, event);
318 spin_unlock_irqrestore(&dev->event_lock, flags);
326 static const struct drm_crtc_funcs qxl_crtc_funcs = {
327 .set_config = drm_crtc_helper_set_config,
328 .destroy = qxl_crtc_destroy,
329 .page_flip = qxl_crtc_page_flip,
330 .reset = drm_atomic_helper_crtc_reset,
331 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
332 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
335 void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
337 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
339 drm_gem_object_unreference_unlocked(qxl_fb->obj);
340 drm_framebuffer_cleanup(fb);
344 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
345 struct drm_file *file_priv,
346 unsigned flags, unsigned color,
347 struct drm_clip_rect *clips,
350 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
351 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
352 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
353 struct drm_clip_rect norect;
357 drm_modeset_lock_all(fb->dev);
359 qobj = gem_to_qxl_bo(qxl_fb->obj);
360 /* if we aren't primary surface ignore this */
361 if (!qobj->is_primary) {
362 drm_modeset_unlock_all(fb->dev);
369 norect.x1 = norect.y1 = 0;
370 norect.x2 = fb->width;
371 norect.y2 = fb->height;
372 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
374 inc = 2; /* skip source rects */
377 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
378 clips, num_clips, inc);
380 drm_modeset_unlock_all(fb->dev);
385 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
386 .destroy = qxl_user_framebuffer_destroy,
387 .dirty = qxl_framebuffer_surface_dirty,
389 * .create_handle = qxl_user_framebuffer_create_handle, */
393 qxl_framebuffer_init(struct drm_device *dev,
394 struct qxl_framebuffer *qfb,
395 const struct drm_mode_fb_cmd2 *mode_cmd,
396 struct drm_gem_object *obj,
397 const struct drm_framebuffer_funcs *funcs)
402 drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
403 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
411 static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
415 static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
416 const struct drm_display_mode *mode,
417 struct drm_display_mode *adjusted_mode)
419 struct drm_device *dev = crtc->dev;
420 struct qxl_device *qdev = dev->dev_private;
422 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
424 mode->hdisplay, mode->vdisplay,
425 adjusted_mode->hdisplay,
426 adjusted_mode->vdisplay);
431 qxl_send_monitors_config(struct qxl_device *qdev)
435 BUG_ON(!qdev->ram_header->monitors_config);
437 if (qdev->monitors_config->count == 0) {
438 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
441 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
442 struct qxl_head *head = &qdev->monitors_config->heads[i];
444 if (head->y > 8192 || head->x > 8192 ||
445 head->width > 8192 || head->height > 8192) {
446 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
447 i, head->width, head->height,
452 qxl_io_monitors_config(qdev);
455 static void qxl_monitors_config_set(struct qxl_device *qdev,
457 unsigned x, unsigned y,
458 unsigned width, unsigned height,
461 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
462 qdev->monitors_config->heads[index].x = x;
463 qdev->monitors_config->heads[index].y = y;
464 qdev->monitors_config->heads[index].width = width;
465 qdev->monitors_config->heads[index].height = height;
466 qdev->monitors_config->heads[index].surface_id = surf_id;
470 static void qxl_crtc_prepare(struct drm_crtc *crtc)
472 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
473 crtc->mode.hdisplay, crtc->mode.vdisplay,
474 crtc->x, crtc->y, crtc->enabled);
477 void qxl_mode_set_nofb(struct drm_crtc *crtc)
479 struct qxl_device *qdev = crtc->dev->dev_private;
480 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
481 struct drm_display_mode *mode = &crtc->mode;
483 DRM_DEBUG("Mode set (%d,%d)\n",
484 mode->hdisplay, mode->vdisplay);
486 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
487 mode->hdisplay, mode->vdisplay, 0);
491 static void qxl_crtc_commit(struct drm_crtc *crtc)
496 static void qxl_crtc_disable(struct drm_crtc *crtc)
498 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
499 struct qxl_device *qdev = crtc->dev->dev_private;
501 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
503 qxl_send_monitors_config(qdev);
506 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
507 .dpms = qxl_crtc_dpms,
508 .disable = qxl_crtc_disable,
509 .mode_fixup = qxl_crtc_mode_fixup,
510 .mode_set = drm_helper_crtc_mode_set,
511 .mode_set_nofb = qxl_mode_set_nofb,
512 .prepare = qxl_crtc_prepare,
513 .commit = qxl_crtc_commit,
514 .atomic_flush = qxl_crtc_atomic_flush,
517 int qxl_primary_atomic_check(struct drm_plane *plane,
518 struct drm_plane_state *state)
520 struct qxl_device *qdev = plane->dev->dev_private;
521 struct qxl_framebuffer *qfb;
524 if (!state->crtc || !state->fb)
527 qfb = to_qxl_framebuffer(state->fb);
528 bo = gem_to_qxl_bo(qfb->obj);
530 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
531 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
538 static void qxl_primary_atomic_update(struct drm_plane *plane,
539 struct drm_plane_state *old_state)
541 struct qxl_device *qdev = plane->dev->dev_private;
542 struct qxl_framebuffer *qfb =
543 to_qxl_framebuffer(plane->state->fb);
544 struct qxl_framebuffer *qfb_old;
545 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
546 struct qxl_bo *bo_old;
547 struct drm_clip_rect norect = {
550 .x2 = qfb->base.width,
551 .y2 = qfb->base.height
554 if (!old_state->fb) {
556 "create primary fb: %dx%d,%d,%d\n",
557 bo->surf.width, bo->surf.height,
558 bo->surf.stride, bo->surf.format);
560 qxl_io_create_primary(qdev, 0, bo);
561 bo->is_primary = true;
565 qfb_old = to_qxl_framebuffer(old_state->fb);
566 bo_old = gem_to_qxl_bo(qfb_old->obj);
567 bo_old->is_primary = false;
570 bo->is_primary = true;
571 qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
574 static void qxl_primary_atomic_disable(struct drm_plane *plane,
575 struct drm_plane_state *old_state)
577 struct qxl_device *qdev = plane->dev->dev_private;
580 { struct qxl_framebuffer *qfb =
581 to_qxl_framebuffer(old_state->fb);
582 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
584 qxl_io_destroy_primary(qdev);
585 bo->is_primary = false;
589 int qxl_plane_atomic_check(struct drm_plane *plane,
590 struct drm_plane_state *state)
595 static void qxl_cursor_atomic_update(struct drm_plane *plane,
596 struct drm_plane_state *old_state)
598 struct drm_device *dev = plane->dev;
599 struct qxl_device *qdev = dev->dev_private;
600 struct drm_framebuffer *fb = plane->state->fb;
601 struct qxl_release *release;
602 struct qxl_cursor_cmd *cmd;
603 struct qxl_cursor *cursor;
604 struct drm_gem_object *obj;
605 struct qxl_bo *cursor_bo, *user_bo = NULL;
610 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
611 QXL_RELEASE_CURSOR_CMD,
614 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
616 if (fb != old_state->fb) {
617 obj = to_qxl_framebuffer(fb)->obj;
618 user_bo = gem_to_qxl_bo(obj);
620 /* pinning is done in the prepare/cleanup framevbuffer */
621 ret = qxl_bo_kmap(user_bo, &user_ptr);
623 goto out_free_release;
625 ret = qxl_alloc_bo_reserved(qdev, release,
626 sizeof(struct qxl_cursor) + size,
631 ret = qxl_release_reserve_list(release, true);
635 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
639 cursor->header.unique = 0;
640 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
641 cursor->header.width = 64;
642 cursor->header.height = 64;
643 cursor->header.hot_spot_x = fb->hot_x;
644 cursor->header.hot_spot_y = fb->hot_y;
645 cursor->data_size = size;
646 cursor->chunk.next_chunk = 0;
647 cursor->chunk.prev_chunk = 0;
648 cursor->chunk.data_size = size;
649 memcpy(cursor->chunk.data, user_ptr, size);
650 qxl_bo_kunmap(cursor_bo);
651 qxl_bo_kunmap(user_bo);
653 cmd->u.set.visible = 1;
654 cmd->u.set.shape = qxl_bo_physical_address(qdev,
656 cmd->type = QXL_CURSOR_SET;
659 ret = qxl_release_reserve_list(release, true);
661 goto out_free_release;
663 cmd->type = QXL_CURSOR_MOVE;
666 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
667 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
669 qxl_release_unmap(qdev, release, &cmd->release_info);
670 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
671 qxl_release_fence_buffer_objects(release);
676 qxl_release_backoff_reserve_list(release);
678 qxl_bo_unref(&cursor_bo);
680 qxl_bo_kunmap(user_bo);
682 qxl_release_free(qdev, release);
687 void qxl_cursor_atomic_disable(struct drm_plane *plane,
688 struct drm_plane_state *old_state)
690 struct qxl_device *qdev = plane->dev->dev_private;
691 struct qxl_release *release;
692 struct qxl_cursor_cmd *cmd;
695 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
696 QXL_RELEASE_CURSOR_CMD,
701 ret = qxl_release_reserve_list(release, true);
703 qxl_release_free(qdev, release);
707 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
708 cmd->type = QXL_CURSOR_HIDE;
709 qxl_release_unmap(qdev, release, &cmd->release_info);
711 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
712 qxl_release_fence_buffer_objects(release);
715 int qxl_plane_prepare_fb(struct drm_plane *plane,
716 struct drm_plane_state *new_state)
718 struct drm_gem_object *obj;
719 struct qxl_bo *user_bo;
725 obj = to_qxl_framebuffer(new_state->fb)->obj;
726 user_bo = gem_to_qxl_bo(obj);
728 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
735 static void qxl_plane_cleanup_fb(struct drm_plane *plane,
736 struct drm_plane_state *old_state)
738 struct drm_gem_object *obj;
739 struct qxl_bo *user_bo;
741 if (!plane->state->fb) {
742 /* we never executed prepare_fb, so there's nothing to
748 obj = to_qxl_framebuffer(plane->state->fb)->obj;
749 user_bo = gem_to_qxl_bo(obj);
750 qxl_bo_unpin(user_bo);
753 static const uint32_t qxl_cursor_plane_formats[] = {
757 static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
758 .atomic_check = qxl_plane_atomic_check,
759 .atomic_update = qxl_cursor_atomic_update,
760 .atomic_disable = qxl_cursor_atomic_disable,
761 .prepare_fb = qxl_plane_prepare_fb,
762 .cleanup_fb = qxl_plane_cleanup_fb,
765 static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
766 .update_plane = drm_atomic_helper_update_plane,
767 .disable_plane = drm_atomic_helper_disable_plane,
768 .destroy = drm_primary_helper_destroy,
769 .reset = drm_atomic_helper_plane_reset,
770 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
771 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
774 static const uint32_t qxl_primary_plane_formats[] = {
779 static const struct drm_plane_helper_funcs primary_helper_funcs = {
780 .atomic_check = qxl_primary_atomic_check,
781 .atomic_update = qxl_primary_atomic_update,
782 .atomic_disable = qxl_primary_atomic_disable,
783 .prepare_fb = qxl_plane_prepare_fb,
784 .cleanup_fb = qxl_plane_cleanup_fb,
787 static const struct drm_plane_funcs qxl_primary_plane_funcs = {
788 .update_plane = drm_atomic_helper_update_plane,
789 .disable_plane = drm_atomic_helper_disable_plane,
790 .destroy = drm_primary_helper_destroy,
791 .reset = drm_atomic_helper_plane_reset,
792 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
793 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
796 static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
797 unsigned int possible_crtcs,
798 enum drm_plane_type type)
800 const struct drm_plane_helper_funcs *helper_funcs = NULL;
801 struct drm_plane *plane;
802 const struct drm_plane_funcs *funcs;
803 const uint32_t *formats;
807 if (type == DRM_PLANE_TYPE_PRIMARY) {
808 funcs = &qxl_primary_plane_funcs;
809 formats = qxl_primary_plane_formats;
810 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
811 helper_funcs = &primary_helper_funcs;
812 } else if (type == DRM_PLANE_TYPE_CURSOR) {
813 funcs = &qxl_cursor_plane_funcs;
814 formats = qxl_cursor_plane_formats;
815 helper_funcs = &qxl_cursor_helper_funcs;
816 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
818 return ERR_PTR(-EINVAL);
821 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
823 return ERR_PTR(-ENOMEM);
825 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
826 funcs, formats, num_formats,
831 drm_plane_helper_add(plane, helper_funcs);
837 return ERR_PTR(-EINVAL);
840 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
842 struct qxl_crtc *qxl_crtc;
843 struct drm_plane *primary, *cursor;
844 struct qxl_device *qdev = dev->dev_private;
847 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
851 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
852 if (IS_ERR(primary)) {
857 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
858 if (IS_ERR(cursor)) {
863 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
864 &qxl_crtc_funcs, NULL);
868 qxl_crtc->index = crtc_id;
869 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
873 drm_plane_cleanup(cursor);
876 drm_plane_cleanup(primary);
883 static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
888 static void qxl_enc_prepare(struct drm_encoder *encoder)
893 static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
894 struct drm_encoder *encoder)
897 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
898 struct qxl_head *head;
899 struct drm_display_mode *mode;
902 /* TODO: ugly, do better */
904 if (!qdev->monitors_config ||
905 qdev->monitors_config->max_allowed <= i) {
907 "head number too large or missing monitors config: %p, %d",
908 qdev->monitors_config,
909 qdev->monitors_config ?
910 qdev->monitors_config->max_allowed : -1);
913 if (!encoder->crtc) {
914 DRM_ERROR("missing crtc on encoder %p\n", encoder);
918 DRM_DEBUG("missing for multiple monitors: no head holes\n");
919 head = &qdev->monitors_config->heads[i];
921 if (encoder->crtc->enabled) {
922 mode = &encoder->crtc->mode;
923 head->width = mode->hdisplay;
924 head->height = mode->vdisplay;
925 head->x = encoder->crtc->x;
926 head->y = encoder->crtc->y;
927 if (qdev->monitors_config->count < i + 1)
928 qdev->monitors_config->count = i + 1;
935 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
936 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
938 /* TODO - somewhere else to call this for multiple monitors
939 * (config_commit?) */
940 qxl_send_monitors_config(qdev);
943 static void qxl_enc_commit(struct drm_encoder *encoder)
945 struct qxl_device *qdev = encoder->dev->dev_private;
947 qxl_write_monitors_config_for_encoder(qdev, encoder);
951 static void qxl_enc_mode_set(struct drm_encoder *encoder,
952 struct drm_display_mode *mode,
953 struct drm_display_mode *adjusted_mode)
958 static int qxl_conn_get_modes(struct drm_connector *connector)
961 struct qxl_device *qdev = connector->dev->dev_private;
962 unsigned pwidth = 1024;
963 unsigned pheight = 768;
965 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
966 /* TODO: what should we do here? only show the configured modes for the
967 * device, or allow the full list, or both? */
968 if (qdev->monitors_config && qdev->monitors_config->count) {
969 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
973 ret += qxl_add_common_modes(connector, pwidth, pheight);
977 static int qxl_conn_mode_valid(struct drm_connector *connector,
978 struct drm_display_mode *mode)
980 struct drm_device *ddev = connector->dev;
981 struct qxl_device *qdev = ddev->dev_private;
984 /* TODO: is this called for user defined modes? (xrandr --add-mode)
985 * TODO: check that the mode fits in the framebuffer */
987 if(qdev->monitors_config_width == mode->hdisplay &&
988 qdev->monitors_config_height == mode->vdisplay)
991 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
992 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
998 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1000 struct qxl_output *qxl_output =
1001 drm_connector_to_qxl_output(connector);
1004 return &qxl_output->enc;
1008 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1009 .dpms = qxl_enc_dpms,
1010 .prepare = qxl_enc_prepare,
1011 .mode_set = qxl_enc_mode_set,
1012 .commit = qxl_enc_commit,
1015 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1016 .get_modes = qxl_conn_get_modes,
1017 .mode_valid = qxl_conn_mode_valid,
1018 .best_encoder = qxl_best_encoder,
1021 static enum drm_connector_status qxl_conn_detect(
1022 struct drm_connector *connector,
1025 struct qxl_output *output =
1026 drm_connector_to_qxl_output(connector);
1027 struct drm_device *ddev = connector->dev;
1028 struct qxl_device *qdev = ddev->dev_private;
1029 bool connected = false;
1031 /* The first monitor is always connected */
1032 if (!qdev->client_monitors_config) {
1033 if (output->index == 0)
1036 connected = qdev->client_monitors_config->count > output->index &&
1037 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1039 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1041 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
1043 return connected ? connector_status_connected
1044 : connector_status_disconnected;
1047 static int qxl_conn_set_property(struct drm_connector *connector,
1048 struct drm_property *property,
1055 static void qxl_conn_destroy(struct drm_connector *connector)
1057 struct qxl_output *qxl_output =
1058 drm_connector_to_qxl_output(connector);
1060 drm_connector_unregister(connector);
1061 drm_connector_cleanup(connector);
1065 static const struct drm_connector_funcs qxl_connector_funcs = {
1066 .dpms = drm_helper_connector_dpms,
1067 .detect = qxl_conn_detect,
1068 .fill_modes = drm_helper_probe_single_connector_modes,
1069 .set_property = qxl_conn_set_property,
1070 .destroy = qxl_conn_destroy,
1071 .reset = drm_atomic_helper_connector_reset,
1072 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1073 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1076 static void qxl_enc_destroy(struct drm_encoder *encoder)
1078 drm_encoder_cleanup(encoder);
1081 static const struct drm_encoder_funcs qxl_enc_funcs = {
1082 .destroy = qxl_enc_destroy,
1085 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1087 if (qdev->hotplug_mode_update_property)
1090 qdev->hotplug_mode_update_property =
1091 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1092 "hotplug_mode_update", 0, 1);
1097 static int qdev_output_init(struct drm_device *dev, int num_output)
1099 struct qxl_device *qdev = dev->dev_private;
1100 struct qxl_output *qxl_output;
1101 struct drm_connector *connector;
1102 struct drm_encoder *encoder;
1104 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1108 qxl_output->index = num_output;
1110 connector = &qxl_output->base;
1111 encoder = &qxl_output->enc;
1112 drm_connector_init(dev, &qxl_output->base,
1113 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1115 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
1116 DRM_MODE_ENCODER_VIRTUAL, NULL);
1118 /* we get HPD via client monitors config */
1119 connector->polled = DRM_CONNECTOR_POLL_HPD;
1120 encoder->possible_crtcs = 1 << num_output;
1121 drm_mode_connector_attach_encoder(&qxl_output->base,
1123 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1124 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1126 drm_object_attach_property(&connector->base,
1127 qdev->hotplug_mode_update_property, 0);
1128 drm_object_attach_property(&connector->base,
1129 dev->mode_config.suggested_x_property, 0);
1130 drm_object_attach_property(&connector->base,
1131 dev->mode_config.suggested_y_property, 0);
1135 static struct drm_framebuffer *
1136 qxl_user_framebuffer_create(struct drm_device *dev,
1137 struct drm_file *file_priv,
1138 const struct drm_mode_fb_cmd2 *mode_cmd)
1140 struct drm_gem_object *obj;
1141 struct qxl_framebuffer *qxl_fb;
1144 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1148 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1152 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
1155 drm_gem_object_unreference_unlocked(obj);
1159 return &qxl_fb->base;
1162 static const struct drm_mode_config_funcs qxl_mode_funcs = {
1163 .fb_create = qxl_user_framebuffer_create,
1164 .atomic_check = drm_atomic_helper_check,
1165 .atomic_commit = drm_atomic_helper_commit,
1168 int qxl_create_monitors_object(struct qxl_device *qdev)
1171 struct drm_gem_object *gobj;
1172 int max_allowed = qxl_num_crtc;
1173 int monitors_config_size = sizeof(struct qxl_monitors_config) +
1174 max_allowed * sizeof(struct qxl_head);
1176 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1177 QXL_GEM_DOMAIN_VRAM,
1178 false, false, NULL, &gobj);
1180 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1183 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1185 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
1189 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
1191 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1192 qdev->ram_header->monitors_config =
1193 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1195 memset(qdev->monitors_config, 0, monitors_config_size);
1196 qdev->monitors_config->max_allowed = max_allowed;
1200 int qxl_destroy_monitors_object(struct qxl_device *qdev)
1204 qdev->monitors_config = NULL;
1205 qdev->ram_header->monitors_config = 0;
1207 qxl_bo_kunmap(qdev->monitors_config_bo);
1208 ret = qxl_bo_unpin(qdev->monitors_config_bo);
1212 qxl_bo_unref(&qdev->monitors_config_bo);
1216 int qxl_modeset_init(struct qxl_device *qdev)
1221 drm_mode_config_init(&qdev->ddev);
1223 ret = qxl_create_monitors_object(qdev);
1227 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1229 /* modes will be validated against the framebuffer size */
1230 qdev->ddev.mode_config.min_width = 0;
1231 qdev->ddev.mode_config.min_height = 0;
1232 qdev->ddev.mode_config.max_width = 8192;
1233 qdev->ddev.mode_config.max_height = 8192;
1235 qdev->ddev.mode_config.fb_base = qdev->vram_base;
1237 drm_mode_create_suggested_offset_properties(&qdev->ddev);
1238 qxl_mode_create_hotplug_mode_update_property(qdev);
1240 for (i = 0 ; i < qxl_num_crtc; ++i) {
1241 qdev_crtc_init(&qdev->ddev, i);
1242 qdev_output_init(&qdev->ddev, i);
1245 qdev->mode_info.mode_config_initialized = true;
1247 drm_mode_config_reset(&qdev->ddev);
1249 /* primary surface must be created by this point, to allow
1250 * issuing command queue commands and having them read by
1252 qxl_fbdev_init(qdev);
1256 void qxl_modeset_fini(struct qxl_device *qdev)
1258 qxl_fbdev_fini(qdev);
1260 qxl_destroy_monitors_object(qdev);
1261 if (qdev->mode_info.mode_config_initialized) {
1262 drm_mode_config_cleanup(&qdev->ddev);
1263 qdev->mode_info.mode_config_initialized = false;