2 * Copyright (C) 2013-2017 Oracle Corporation
3 * This file is based on ast_mode.c
4 * Copyright 2012 Red Hat Inc.
5 * Parts based on xf86-video-ast
6 * Copyright (c) 2005 ASPEED Technology Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
30 * Authors: Dave Airlie <airlied@redhat.com>
31 * Michael Thayer <michael.thayer@oracle.com,
32 * Hans de Goede <hdegoede@redhat.com>
34 #include <linux/export.h>
35 #include <drm/drm_crtc_helper.h>
36 #include <drm/drm_plane_helper.h>
39 #include "vboxvideo.h"
40 #include "hgsmi_channels.h"
42 static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
43 u32 handle, u32 width, u32 height,
44 s32 hot_x, s32 hot_y);
45 static int vbox_cursor_move(struct drm_crtc *crtc, int x, int y);
48 * Set a graphics mode. Poke any required values into registers, do an HGSMI
49 * mode set and tell the host we support advanced graphics functions.
51 static void vbox_do_modeset(struct drm_crtc *crtc,
52 const struct drm_display_mode *mode)
54 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
55 struct vbox_private *vbox;
56 int width, height, bpp, pitch;
58 s32 x_offset, y_offset;
60 vbox = crtc->dev->dev_private;
61 width = mode->hdisplay ? mode->hdisplay : 640;
62 height = mode->vdisplay ? mode->vdisplay : 480;
63 bpp = crtc->enabled ? CRTC_FB(crtc)->format->cpp[0] * 8 : 32;
64 pitch = crtc->enabled ? CRTC_FB(crtc)->pitches[0] : width * bpp / 8;
65 x_offset = vbox->single_framebuffer ? crtc->x : vbox_crtc->x_hint;
66 y_offset = vbox->single_framebuffer ? crtc->y : vbox_crtc->y_hint;
69 * This is the old way of setting graphics modes. It assumed one screen
70 * and a frame-buffer at the start of video RAM. On older versions of
71 * VirtualBox, certain parts of the code still assume that the first
72 * screen is programmed this way, so try to fake it.
74 if (vbox_crtc->crtc_id == 0 && crtc->enabled &&
75 vbox_crtc->fb_offset / pitch < 0xffff - crtc->y &&
76 vbox_crtc->fb_offset % (bpp / 8) == 0) {
77 vbox_write_ioport(VBE_DISPI_INDEX_XRES, width);
78 vbox_write_ioport(VBE_DISPI_INDEX_YRES, height);
79 vbox_write_ioport(VBE_DISPI_INDEX_VIRT_WIDTH, pitch * 8 / bpp);
80 vbox_write_ioport(VBE_DISPI_INDEX_BPP,
81 CRTC_FB(crtc)->format->cpp[0] * 8);
82 vbox_write_ioport(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED);
84 VBE_DISPI_INDEX_X_OFFSET,
85 vbox_crtc->fb_offset % pitch / bpp * 8 + crtc->x);
86 vbox_write_ioport(VBE_DISPI_INDEX_Y_OFFSET,
87 vbox_crtc->fb_offset / pitch + crtc->y);
90 flags = VBVA_SCREEN_F_ACTIVE;
91 flags |= (crtc->enabled && !vbox_crtc->blanked) ?
92 0 : VBVA_SCREEN_F_BLANK;
93 flags |= vbox_crtc->disconnected ? VBVA_SCREEN_F_DISABLED : 0;
94 hgsmi_process_display_info(vbox->guest_pool, vbox_crtc->crtc_id,
96 crtc->x * bpp / 8 + crtc->y * pitch,
98 vbox_crtc->blanked ? 0 : bpp, flags);
101 static int vbox_set_view(struct drm_crtc *crtc)
103 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
104 struct vbox_private *vbox = crtc->dev->dev_private;
105 struct vbva_infoview *p;
108 * Tell the host about the view. This design originally targeted the
109 * Windows XP driver architecture and assumed that each screen would
110 * have a dedicated frame buffer with the command buffer following it,
111 * the whole being a "view". The host works out which screen a command
112 * buffer belongs to by checking whether it is in the first view, then
113 * whether it is in the second and so on. The first match wins. We
114 * cheat around this by making the first view be the managed memory
115 * plus the first command buffer, the second the same plus the second
118 p = hgsmi_buffer_alloc(vbox->guest_pool, sizeof(*p),
119 HGSMI_CH_VBVA, VBVA_INFO_VIEW);
123 p->view_index = vbox_crtc->crtc_id;
124 p->view_offset = vbox_crtc->fb_offset;
125 p->view_size = vbox->available_vram_size - vbox_crtc->fb_offset +
126 vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE;
127 p->max_screen_size = vbox->available_vram_size - vbox_crtc->fb_offset;
129 hgsmi_buffer_submit(vbox->guest_pool, p);
130 hgsmi_buffer_free(vbox->guest_pool, p);
135 static void vbox_crtc_dpms(struct drm_crtc *crtc, int mode)
137 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
138 struct vbox_private *vbox = crtc->dev->dev_private;
141 case DRM_MODE_DPMS_ON:
142 vbox_crtc->blanked = false;
144 case DRM_MODE_DPMS_STANDBY:
145 case DRM_MODE_DPMS_SUSPEND:
146 case DRM_MODE_DPMS_OFF:
147 vbox_crtc->blanked = true;
151 mutex_lock(&vbox->hw_mutex);
152 vbox_do_modeset(crtc, &crtc->hwmode);
153 mutex_unlock(&vbox->hw_mutex);
156 static bool vbox_crtc_mode_fixup(struct drm_crtc *crtc,
157 const struct drm_display_mode *mode,
158 struct drm_display_mode *adjusted_mode)
164 * Try to map the layout of virtual screens to the range of the input device.
165 * Return true if we need to re-set the crtc modes due to screen offset
168 static bool vbox_set_up_input_mapping(struct vbox_private *vbox)
170 struct drm_crtc *crtci;
171 struct drm_connector *connectori;
172 struct drm_framebuffer *fb, *fb1 = NULL;
173 bool single_framebuffer = true;
174 bool old_single_framebuffer = vbox->single_framebuffer;
175 u16 width = 0, height = 0;
178 * Are we using an X.Org-style single large frame-buffer for all crtcs?
179 * If so then screen layout can be deduced from the crtc offsets.
180 * Same fall-back if this is the fbdev frame-buffer.
182 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
189 if (to_vbox_framebuffer(fb1) == &vbox->fbdev->afb)
191 } else if (fb != fb1) {
192 single_framebuffer = false;
198 if (single_framebuffer) {
199 vbox->single_framebuffer = true;
200 vbox->input_mapping_width = fb1->width;
201 vbox->input_mapping_height = fb1->height;
202 return old_single_framebuffer != vbox->single_framebuffer;
204 /* Otherwise calculate the total span of all screens. */
205 list_for_each_entry(connectori, &vbox->ddev.mode_config.connector_list,
207 struct vbox_connector *vbox_connector =
208 to_vbox_connector(connectori);
209 struct vbox_crtc *vbox_crtc = vbox_connector->vbox_crtc;
211 width = max_t(u16, width, vbox_crtc->x_hint +
212 vbox_connector->mode_hint.width);
213 height = max_t(u16, height, vbox_crtc->y_hint +
214 vbox_connector->mode_hint.height);
217 vbox->single_framebuffer = false;
218 vbox->input_mapping_width = width;
219 vbox->input_mapping_height = height;
221 return old_single_framebuffer != vbox->single_framebuffer;
224 static void vbox_crtc_set_base_and_mode(struct drm_crtc *crtc,
225 struct drm_framebuffer *fb,
226 struct drm_display_mode *mode,
229 struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(fb)->obj);
230 struct vbox_private *vbox = crtc->dev->dev_private;
231 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
233 mutex_lock(&vbox->hw_mutex);
235 vbox_crtc->fb_offset = vbox_bo_gpu_offset(bo);
237 /* vbox_do_modeset() checks vbox->single_framebuffer so update it now */
238 if (mode && vbox_set_up_input_mapping(vbox)) {
239 struct drm_crtc *crtci;
241 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list,
245 vbox_do_modeset(crtci, &crtci->mode);
250 vbox_do_modeset(crtc, mode ? mode : &crtc->mode);
253 hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
254 vbox->input_mapping_width,
255 vbox->input_mapping_height);
257 mutex_unlock(&vbox->hw_mutex);
260 static int vbox_crtc_mode_set(struct drm_crtc *crtc,
261 struct drm_display_mode *mode,
262 struct drm_display_mode *adjusted_mode,
263 int x, int y, struct drm_framebuffer *old_fb)
265 struct drm_framebuffer *new_fb = CRTC_FB(crtc);
266 struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(new_fb)->obj);
269 ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM);
271 DRM_WARN("Error %d pinning new fb, out of video mem?\n", ret);
275 vbox_crtc_set_base_and_mode(crtc, new_fb, mode, x, y);
278 bo = gem_to_vbox_bo(to_vbox_framebuffer(old_fb)->obj);
285 static int vbox_crtc_page_flip(struct drm_crtc *crtc,
286 struct drm_framebuffer *fb,
287 struct drm_pending_vblank_event *event,
288 uint32_t page_flip_flags,
289 struct drm_modeset_acquire_ctx *ctx)
291 struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(fb)->obj);
292 struct drm_framebuffer *old_fb = CRTC_FB(crtc);
296 rc = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM);
298 DRM_WARN("Error %d pinning new fb, out of video mem?\n", rc);
302 vbox_crtc_set_base_and_mode(crtc, fb, NULL, 0, 0);
305 bo = gem_to_vbox_bo(to_vbox_framebuffer(old_fb)->obj);
309 spin_lock_irqsave(&crtc->dev->event_lock, flags);
312 drm_crtc_send_vblank_event(crtc, event);
314 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
319 static void vbox_crtc_disable(struct drm_crtc *crtc)
323 static void vbox_crtc_prepare(struct drm_crtc *crtc)
327 static void vbox_crtc_commit(struct drm_crtc *crtc)
331 static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = {
332 .dpms = vbox_crtc_dpms,
333 .mode_fixup = vbox_crtc_mode_fixup,
334 .mode_set = vbox_crtc_mode_set,
335 .disable = vbox_crtc_disable,
336 .prepare = vbox_crtc_prepare,
337 .commit = vbox_crtc_commit,
340 static void vbox_crtc_reset(struct drm_crtc *crtc)
344 static void vbox_crtc_destroy(struct drm_crtc *crtc)
346 drm_crtc_cleanup(crtc);
350 static const struct drm_crtc_funcs vbox_crtc_funcs = {
351 .cursor_move = vbox_cursor_move,
352 .cursor_set2 = vbox_cursor_set2,
353 .reset = vbox_crtc_reset,
354 .set_config = drm_crtc_helper_set_config,
355 /* .gamma_set = vbox_crtc_gamma_set, */
356 .page_flip = vbox_crtc_page_flip,
357 .destroy = vbox_crtc_destroy,
360 static const uint32_t vbox_primary_plane_formats[] = {
365 static const struct drm_plane_funcs vbox_primary_plane_funcs = {
366 .update_plane = drm_primary_helper_update,
367 .disable_plane = drm_primary_helper_disable,
368 .destroy = drm_primary_helper_destroy,
371 static struct drm_plane *vbox_create_plane(struct vbox_private *vbox,
372 unsigned int possible_crtcs,
373 enum drm_plane_type type)
375 const struct drm_plane_helper_funcs *helper_funcs = NULL;
376 const struct drm_plane_funcs *funcs;
377 struct drm_plane *plane;
378 const uint32_t *formats;
382 if (type == DRM_PLANE_TYPE_PRIMARY) {
383 funcs = &vbox_primary_plane_funcs;
384 formats = vbox_primary_plane_formats;
385 num_formats = ARRAY_SIZE(vbox_primary_plane_formats);
387 return ERR_PTR(-EINVAL);
390 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
392 return ERR_PTR(-ENOMEM);
394 err = drm_universal_plane_init(&vbox->ddev, plane, possible_crtcs,
395 funcs, formats, num_formats,
400 drm_plane_helper_add(plane, helper_funcs);
406 return ERR_PTR(-EINVAL);
409 static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned int i)
411 struct vbox_private *vbox =
412 container_of(dev, struct vbox_private, ddev);
413 struct vbox_crtc *vbox_crtc;
414 struct drm_plane *primary;
417 vbox_crtc = kzalloc(sizeof(*vbox_crtc), GFP_KERNEL);
419 return ERR_PTR(-ENOMEM);
421 primary = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_PRIMARY);
422 if (IS_ERR(primary)) {
423 ret = PTR_ERR(primary);
427 vbox_crtc->crtc_id = i;
429 ret = drm_crtc_init_with_planes(dev, &vbox_crtc->base, primary, NULL,
430 &vbox_crtc_funcs, NULL);
434 drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256);
435 drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs);
440 drm_plane_cleanup(primary);
447 static void vbox_encoder_destroy(struct drm_encoder *encoder)
449 drm_encoder_cleanup(encoder);
453 static struct drm_encoder *vbox_best_single_encoder(struct drm_connector
456 int enc_id = connector->encoder_ids[0];
458 /* pick the encoder ids */
460 return drm_encoder_find(connector->dev, NULL, enc_id);
465 static const struct drm_encoder_funcs vbox_enc_funcs = {
466 .destroy = vbox_encoder_destroy,
469 static void vbox_encoder_dpms(struct drm_encoder *encoder, int mode)
473 static bool vbox_mode_fixup(struct drm_encoder *encoder,
474 const struct drm_display_mode *mode,
475 struct drm_display_mode *adjusted_mode)
480 static void vbox_encoder_mode_set(struct drm_encoder *encoder,
481 struct drm_display_mode *mode,
482 struct drm_display_mode *adjusted_mode)
486 static void vbox_encoder_prepare(struct drm_encoder *encoder)
490 static void vbox_encoder_commit(struct drm_encoder *encoder)
494 static const struct drm_encoder_helper_funcs vbox_enc_helper_funcs = {
495 .dpms = vbox_encoder_dpms,
496 .mode_fixup = vbox_mode_fixup,
497 .prepare = vbox_encoder_prepare,
498 .commit = vbox_encoder_commit,
499 .mode_set = vbox_encoder_mode_set,
502 static struct drm_encoder *vbox_encoder_init(struct drm_device *dev,
505 struct vbox_encoder *vbox_encoder;
507 vbox_encoder = kzalloc(sizeof(*vbox_encoder), GFP_KERNEL);
511 drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
512 DRM_MODE_ENCODER_DAC, NULL);
513 drm_encoder_helper_add(&vbox_encoder->base, &vbox_enc_helper_funcs);
515 vbox_encoder->base.possible_crtcs = 1 << i;
516 return &vbox_encoder->base;
520 * Generate EDID data with a mode-unique serial number for the virtual
521 * monitor to try to persuade Unity that different modes correspond to
522 * different monitors and it should not try to force the same resolution on
525 static void vbox_set_edid(struct drm_connector *connector, int width,
528 enum { EDID_SIZE = 128 };
529 unsigned char edid[EDID_SIZE] = {
530 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
531 0x58, 0x58, /* manufacturer (VBX) */
532 0x00, 0x00, /* product code */
533 0x00, 0x00, 0x00, 0x00, /* serial number goes here */
534 0x01, /* week of manufacture */
535 0x00, /* year of manufacture */
536 0x01, 0x03, /* EDID version */
537 0x80, /* capabilities - digital */
538 0x00, /* horiz. res in cm, zero for projectors */
539 0x00, /* vert. res in cm */
540 0x78, /* display gamma (120 == 2.2). */
541 0xEE, /* features (standby, suspend, off, RGB, std */
542 /* colour space, preferred timing mode) */
543 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
544 /* chromaticity for standard colour space. */
545 0x00, 0x00, 0x00, /* no default timings */
546 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
548 0x01, 0x01, 0x01, 0x01, /* no standard timings */
549 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
551 /* descriptor block 1 goes below */
552 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
553 /* descriptor block 2, monitor ranges */
554 0x00, 0x00, 0x00, 0xFD, 0x00,
555 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
557 /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
559 /* descriptor block 3, monitor name */
560 0x00, 0x00, 0x00, 0xFC, 0x00,
561 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
563 /* descriptor block 4: dummy data */
564 0x00, 0x00, 0x00, 0x10, 0x00,
565 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
566 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
568 0x00, /* number of extensions */
569 0x00 /* checksum goes here */
571 int clock = (width + 6) * (height + 6) * 60 / 10000;
572 unsigned int i, sum = 0;
574 edid[12] = width & 0xff;
575 edid[13] = width >> 8;
576 edid[14] = height & 0xff;
577 edid[15] = height >> 8;
578 edid[54] = clock & 0xff;
579 edid[55] = clock >> 8;
580 edid[56] = width & 0xff;
581 edid[58] = (width >> 4) & 0xf0;
582 edid[59] = height & 0xff;
583 edid[61] = (height >> 4) & 0xf0;
584 for (i = 0; i < EDID_SIZE - 1; ++i)
586 edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF;
587 drm_connector_update_edid_property(connector, (struct edid *)edid);
590 static int vbox_get_modes(struct drm_connector *connector)
592 struct vbox_connector *vbox_connector = NULL;
593 struct drm_display_mode *mode = NULL;
594 struct vbox_private *vbox = NULL;
595 unsigned int num_modes = 0;
596 int preferred_width, preferred_height;
598 vbox_connector = to_vbox_connector(connector);
599 vbox = connector->dev->dev_private;
601 * Heuristic: we do not want to tell the host that we support dynamic
602 * resizing unless we feel confident that the user space client using
603 * the video driver can handle hot-plug events. So the first time modes
604 * are queried after a "master" switch we tell the host that we do not,
605 * and immediately after we send the client a hot-plug notification as
606 * a test to see if they will respond and query again.
607 * That is also the reason why capabilities are reported to the host at
608 * this place in the code rather than elsewhere.
609 * We need to report the flags location before reporting the IRQ
612 hgsmi_report_flags_location(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) +
614 if (vbox_connector->vbox_crtc->crtc_id == 0)
615 vbox_report_caps(vbox);
616 if (!vbox->initial_mode_queried) {
617 if (vbox_connector->vbox_crtc->crtc_id == 0) {
618 vbox->initial_mode_queried = true;
619 vbox_report_hotplug(vbox);
621 return drm_add_modes_noedid(connector, 800, 600);
623 num_modes = drm_add_modes_noedid(connector, 2560, 1600);
624 preferred_width = vbox_connector->mode_hint.width ?
625 vbox_connector->mode_hint.width : 1024;
626 preferred_height = vbox_connector->mode_hint.height ?
627 vbox_connector->mode_hint.height : 768;
628 mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height,
629 60, false, false, false);
631 mode->type |= DRM_MODE_TYPE_PREFERRED;
632 drm_mode_probed_add(connector, mode);
635 vbox_set_edid(connector, preferred_width, preferred_height);
637 if (vbox_connector->vbox_crtc->x_hint != -1)
638 drm_object_property_set_value(&connector->base,
639 vbox->ddev.mode_config.suggested_x_property,
640 vbox_connector->vbox_crtc->x_hint);
642 drm_object_property_set_value(&connector->base,
643 vbox->ddev.mode_config.suggested_x_property, 0);
645 if (vbox_connector->vbox_crtc->y_hint != -1)
646 drm_object_property_set_value(&connector->base,
647 vbox->ddev.mode_config.suggested_y_property,
648 vbox_connector->vbox_crtc->y_hint);
650 drm_object_property_set_value(&connector->base,
651 vbox->ddev.mode_config.suggested_y_property, 0);
656 static enum drm_mode_status vbox_mode_valid(struct drm_connector *connector,
657 struct drm_display_mode *mode)
662 static void vbox_connector_destroy(struct drm_connector *connector)
664 drm_connector_unregister(connector);
665 drm_connector_cleanup(connector);
669 static enum drm_connector_status
670 vbox_connector_detect(struct drm_connector *connector, bool force)
672 struct vbox_connector *vbox_connector;
674 vbox_connector = to_vbox_connector(connector);
676 return vbox_connector->mode_hint.disconnected ?
677 connector_status_disconnected : connector_status_connected;
680 static int vbox_fill_modes(struct drm_connector *connector, u32 max_x,
683 struct vbox_connector *vbox_connector;
684 struct drm_device *dev;
685 struct drm_display_mode *mode, *iterator;
687 vbox_connector = to_vbox_connector(connector);
688 dev = vbox_connector->base.dev;
689 list_for_each_entry_safe(mode, iterator, &connector->modes, head) {
690 list_del(&mode->head);
691 drm_mode_destroy(dev, mode);
694 return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
697 static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
698 .mode_valid = vbox_mode_valid,
699 .get_modes = vbox_get_modes,
700 .best_encoder = vbox_best_single_encoder,
703 static const struct drm_connector_funcs vbox_connector_funcs = {
704 .dpms = drm_helper_connector_dpms,
705 .detect = vbox_connector_detect,
706 .fill_modes = vbox_fill_modes,
707 .destroy = vbox_connector_destroy,
710 static int vbox_connector_init(struct drm_device *dev,
711 struct vbox_crtc *vbox_crtc,
712 struct drm_encoder *encoder)
714 struct vbox_connector *vbox_connector;
715 struct drm_connector *connector;
717 vbox_connector = kzalloc(sizeof(*vbox_connector), GFP_KERNEL);
721 connector = &vbox_connector->base;
722 vbox_connector->vbox_crtc = vbox_crtc;
724 drm_connector_init(dev, connector, &vbox_connector_funcs,
725 DRM_MODE_CONNECTOR_VGA);
726 drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
728 connector->interlace_allowed = 0;
729 connector->doublescan_allowed = 0;
731 drm_mode_create_suggested_offset_properties(dev);
732 drm_object_attach_property(&connector->base,
733 dev->mode_config.suggested_x_property, 0);
734 drm_object_attach_property(&connector->base,
735 dev->mode_config.suggested_y_property, 0);
737 drm_connector_attach_encoder(connector, encoder);
742 static struct drm_framebuffer *vbox_user_framebuffer_create(
743 struct drm_device *dev,
744 struct drm_file *filp,
745 const struct drm_mode_fb_cmd2 *mode_cmd)
747 struct vbox_private *vbox =
748 container_of(dev, struct vbox_private, ddev);
749 struct drm_gem_object *obj;
750 struct vbox_framebuffer *vbox_fb;
753 obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
755 return ERR_PTR(-ENOENT);
757 vbox_fb = kzalloc(sizeof(*vbox_fb), GFP_KERNEL);
761 ret = vbox_framebuffer_init(vbox, vbox_fb, mode_cmd, obj);
763 goto err_free_vbox_fb;
765 return &vbox_fb->base;
770 drm_gem_object_put_unlocked(obj);
774 static const struct drm_mode_config_funcs vbox_mode_funcs = {
775 .fb_create = vbox_user_framebuffer_create,
778 int vbox_mode_init(struct vbox_private *vbox)
780 struct drm_device *dev = &vbox->ddev;
781 struct drm_encoder *encoder;
782 struct vbox_crtc *vbox_crtc;
786 drm_mode_config_init(dev);
788 dev->mode_config.funcs = (void *)&vbox_mode_funcs;
789 dev->mode_config.min_width = 64;
790 dev->mode_config.min_height = 64;
791 dev->mode_config.preferred_depth = 24;
792 dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
793 dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
795 /* vbox_cursor_init(dev); */
796 for (i = 0; i < vbox->num_crtcs; ++i) {
797 vbox_crtc = vbox_crtc_init(dev, i);
798 if (IS_ERR(vbox_crtc)) {
799 ret = PTR_ERR(vbox_crtc);
800 goto err_drm_mode_cleanup;
802 encoder = vbox_encoder_init(dev, i);
805 goto err_drm_mode_cleanup;
807 ret = vbox_connector_init(dev, vbox_crtc, encoder);
809 goto err_drm_mode_cleanup;
814 err_drm_mode_cleanup:
815 drm_mode_config_cleanup(dev);
819 void vbox_mode_fini(struct vbox_private *vbox)
821 drm_mode_config_cleanup(&vbox->ddev);
822 /* vbox_cursor_fini(dev); */
826 * Copy the ARGB image and generate the mask, which is needed in case the host
827 * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set
828 * if the corresponding alpha value in the ARGB image is greater than 0xF0.
830 static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,
833 size_t line_size = (width + 7) / 8;
836 memcpy(dst + mask_size, src, width * height * 4);
837 for (i = 0; i < height; ++i)
838 for (j = 0; j < width; ++j)
839 if (((u32 *)src)[i * width + j] > 0xf0000000)
840 dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
843 static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
844 u32 handle, u32 width, u32 height,
845 s32 hot_x, s32 hot_y)
847 struct vbox_private *vbox = crtc->dev->dev_private;
848 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
849 struct ttm_bo_kmap_obj uobj_map;
850 size_t data_size, mask_size;
851 struct drm_gem_object *obj;
860 * Re-set this regularly as in 5.0.20 and earlier the information was
861 * lost on save and restore.
863 hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
864 vbox->input_mapping_width,
865 vbox->input_mapping_height);
867 bool cursor_enabled = false;
868 struct drm_crtc *crtci;
871 vbox_crtc->cursor_enabled = false;
872 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list,
874 if (to_vbox_crtc(crtci)->cursor_enabled)
875 cursor_enabled = true;
879 hgsmi_update_pointer_shape(vbox->guest_pool, 0, 0, 0,
884 vbox_crtc->cursor_enabled = true;
886 if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT ||
887 width == 0 || height == 0)
890 ret = hgsmi_query_conf(vbox->guest_pool,
891 VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
895 if (!(caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)) {
897 * -EINVAL means cursor_set2() not supported, -EAGAIN means
903 obj = drm_gem_object_lookup(file_priv, handle);
905 DRM_ERROR("Cannot find cursor object %x for crtc\n", handle);
909 bo = gem_to_vbox_bo(obj);
910 ret = vbox_bo_reserve(bo, false);
915 * The mask must be calculated based on the alpha
916 * channel, one bit per ARGB word, and must be 32-bit
919 mask_size = ((width + 7) / 8 * height + 3) & ~3;
920 data_size = width * height * 4 + mask_size;
921 vbox->cursor_hot_x = min_t(u32, max(hot_x, 0), width);
922 vbox->cursor_hot_y = min_t(u32, max(hot_y, 0), height);
923 vbox->cursor_width = width;
924 vbox->cursor_height = height;
925 vbox->cursor_data_size = data_size;
926 dst = vbox->cursor_data;
928 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &uobj_map);
930 vbox->cursor_data_size = 0;
931 goto out_unreserve_bo;
934 src = ttm_kmap_obj_virtual(&uobj_map, &src_isiomem);
936 DRM_ERROR("src cursor bo not in main memory\n");
941 copy_cursor_image(src, dst, width, height, mask_size);
943 flags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE |
944 VBOX_MOUSE_POINTER_ALPHA;
945 ret = hgsmi_update_pointer_shape(vbox->guest_pool, flags,
946 vbox->cursor_hot_x, vbox->cursor_hot_y,
947 width, height, dst, data_size);
949 ttm_bo_kunmap(&uobj_map);
951 vbox_bo_unreserve(bo);
953 drm_gem_object_put_unlocked(obj);
958 static int vbox_cursor_move(struct drm_crtc *crtc, int x, int y)
960 struct vbox_private *vbox = crtc->dev->dev_private;
961 u32 flags = VBOX_MOUSE_POINTER_VISIBLE |
962 VBOX_MOUSE_POINTER_SHAPE | VBOX_MOUSE_POINTER_ALPHA;
964 vbox->single_framebuffer ? crtc->x : to_vbox_crtc(crtc)->x_hint;
966 vbox->single_framebuffer ? crtc->y : to_vbox_crtc(crtc)->y_hint;
973 * We compare these to unsigned later and don't
974 * need to handle negative.
976 if (x + crtc_x < 0 || y + crtc_y < 0 || vbox->cursor_data_size == 0)
979 ret = hgsmi_cursor_position(vbox->guest_pool, true, x + crtc_x,
980 y + crtc_y, &host_x, &host_y);
983 * The only reason we have vbox_cursor_move() is that some older clients
984 * might use DRM_IOCTL_MODE_CURSOR instead of DRM_IOCTL_MODE_CURSOR2 and
985 * use DRM_MODE_CURSOR_MOVE to set the hot-spot.
987 * However VirtualBox 5.0.20 and earlier has a bug causing it to return
988 * 0,0 as host cursor location after a save and restore.
990 * To work around this we ignore a 0, 0 return, since missing the odd
991 * time when it legitimately happens is not going to hurt much.
993 if (ret || (host_x == 0 && host_y == 0))
996 if (x + crtc_x < host_x)
997 hot_x = min(host_x - x - crtc_x, vbox->cursor_width);
998 if (y + crtc_y < host_y)
999 hot_y = min(host_y - y - crtc_y, vbox->cursor_height);
1001 if (hot_x == vbox->cursor_hot_x && hot_y == vbox->cursor_hot_y)
1004 vbox->cursor_hot_x = hot_x;
1005 vbox->cursor_hot_y = hot_y;
1007 return hgsmi_update_pointer_shape(vbox->guest_pool, flags,
1008 hot_x, hot_y, vbox->cursor_width, vbox->cursor_height,
1009 vbox->cursor_data, vbox->cursor_data_size);