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
;
59 s32 x_offset
, y_offset
;
61 vbox
= crtc
->dev
->dev_private
;
62 width
= mode
->hdisplay
? mode
->hdisplay
: 640;
63 height
= mode
->vdisplay
? mode
->vdisplay
: 480;
64 crtc_id
= vbox_crtc
->crtc_id
;
65 bpp
= crtc
->enabled
? CRTC_FB(crtc
)->format
->cpp
[0] * 8 : 32;
66 pitch
= crtc
->enabled
? CRTC_FB(crtc
)->pitches
[0] : width
* bpp
/ 8;
67 x_offset
= vbox
->single_framebuffer
? crtc
->x
: vbox_crtc
->x_hint
;
68 y_offset
= vbox
->single_framebuffer
? crtc
->y
: vbox_crtc
->y_hint
;
71 * This is the old way of setting graphics modes. It assumed one screen
72 * and a frame-buffer at the start of video RAM. On older versions of
73 * VirtualBox, certain parts of the code still assume that the first
74 * screen is programmed this way, so try to fake it.
76 if (vbox_crtc
->crtc_id
== 0 && crtc
->enabled
&&
77 vbox_crtc
->fb_offset
/ pitch
< 0xffff - crtc
->y
&&
78 vbox_crtc
->fb_offset
% (bpp
/ 8) == 0) {
79 vbox_write_ioport(VBE_DISPI_INDEX_XRES
, width
);
80 vbox_write_ioport(VBE_DISPI_INDEX_YRES
, height
);
81 vbox_write_ioport(VBE_DISPI_INDEX_VIRT_WIDTH
, pitch
* 8 / bpp
);
82 vbox_write_ioport(VBE_DISPI_INDEX_BPP
,
83 CRTC_FB(crtc
)->format
->cpp
[0] * 8);
84 vbox_write_ioport(VBE_DISPI_INDEX_ENABLE
, VBE_DISPI_ENABLED
);
86 VBE_DISPI_INDEX_X_OFFSET
,
87 vbox_crtc
->fb_offset
% pitch
/ bpp
* 8 + crtc
->x
);
88 vbox_write_ioport(VBE_DISPI_INDEX_Y_OFFSET
,
89 vbox_crtc
->fb_offset
/ pitch
+ crtc
->y
);
92 flags
= VBVA_SCREEN_F_ACTIVE
;
93 flags
|= (crtc
->enabled
&& !vbox_crtc
->blanked
) ?
94 0 : VBVA_SCREEN_F_BLANK
;
95 flags
|= vbox_crtc
->disconnected
? VBVA_SCREEN_F_DISABLED
: 0;
96 hgsmi_process_display_info(vbox
->guest_pool
, vbox_crtc
->crtc_id
,
98 crtc
->x
* bpp
/ 8 + crtc
->y
* pitch
,
100 vbox_crtc
->blanked
? 0 : bpp
, flags
);
103 static int vbox_set_view(struct drm_crtc
*crtc
)
105 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
106 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
107 struct vbva_infoview
*p
;
110 * Tell the host about the view. This design originally targeted the
111 * Windows XP driver architecture and assumed that each screen would
112 * have a dedicated frame buffer with the command buffer following it,
113 * the whole being a "view". The host works out which screen a command
114 * buffer belongs to by checking whether it is in the first view, then
115 * whether it is in the second and so on. The first match wins. We
116 * cheat around this by making the first view be the managed memory
117 * plus the first command buffer, the second the same plus the second
120 p
= hgsmi_buffer_alloc(vbox
->guest_pool
, sizeof(*p
),
121 HGSMI_CH_VBVA
, VBVA_INFO_VIEW
);
125 p
->view_index
= vbox_crtc
->crtc_id
;
126 p
->view_offset
= vbox_crtc
->fb_offset
;
127 p
->view_size
= vbox
->available_vram_size
- vbox_crtc
->fb_offset
+
128 vbox_crtc
->crtc_id
* VBVA_MIN_BUFFER_SIZE
;
129 p
->max_screen_size
= vbox
->available_vram_size
- vbox_crtc
->fb_offset
;
131 hgsmi_buffer_submit(vbox
->guest_pool
, p
);
132 hgsmi_buffer_free(vbox
->guest_pool
, p
);
137 static void vbox_crtc_load_lut(struct drm_crtc
*crtc
)
141 static void vbox_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
143 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
144 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
147 case DRM_MODE_DPMS_ON
:
148 vbox_crtc
->blanked
= false;
150 case DRM_MODE_DPMS_STANDBY
:
151 case DRM_MODE_DPMS_SUSPEND
:
152 case DRM_MODE_DPMS_OFF
:
153 vbox_crtc
->blanked
= true;
157 mutex_lock(&vbox
->hw_mutex
);
158 vbox_do_modeset(crtc
, &crtc
->hwmode
);
159 mutex_unlock(&vbox
->hw_mutex
);
162 static bool vbox_crtc_mode_fixup(struct drm_crtc
*crtc
,
163 const struct drm_display_mode
*mode
,
164 struct drm_display_mode
*adjusted_mode
)
170 * Try to map the layout of virtual screens to the range of the input device.
171 * Return true if we need to re-set the crtc modes due to screen offset
174 static bool vbox_set_up_input_mapping(struct vbox_private
*vbox
)
176 struct drm_crtc
*crtci
;
177 struct drm_connector
*connectori
;
178 struct drm_framebuffer
*fb1
= NULL
;
179 bool single_framebuffer
= true;
180 bool old_single_framebuffer
= vbox
->single_framebuffer
;
181 u16 width
= 0, height
= 0;
184 * Are we using an X.Org-style single large frame-buffer for all crtcs?
185 * If so then screen layout can be deduced from the crtc offsets.
186 * Same fall-back if this is the fbdev frame-buffer.
188 list_for_each_entry(crtci
, &vbox
->dev
->mode_config
.crtc_list
, head
) {
190 fb1
= CRTC_FB(crtci
);
191 if (to_vbox_framebuffer(fb1
) == &vbox
->fbdev
->afb
)
193 } else if (CRTC_FB(crtci
) && fb1
!= CRTC_FB(crtci
)) {
194 single_framebuffer
= false;
197 if (single_framebuffer
) {
198 list_for_each_entry(crtci
, &vbox
->dev
->mode_config
.crtc_list
,
200 if (to_vbox_crtc(crtci
)->crtc_id
!= 0)
203 vbox
->single_framebuffer
= true;
204 vbox
->input_mapping_width
= CRTC_FB(crtci
)->width
;
205 vbox
->input_mapping_height
= CRTC_FB(crtci
)->height
;
206 return old_single_framebuffer
!=
207 vbox
->single_framebuffer
;
210 /* Otherwise calculate the total span of all screens. */
211 list_for_each_entry(connectori
, &vbox
->dev
->mode_config
.connector_list
,
213 struct vbox_connector
*vbox_connector
=
214 to_vbox_connector(connectori
);
215 struct vbox_crtc
*vbox_crtc
= vbox_connector
->vbox_crtc
;
217 width
= max_t(u16
, width
, vbox_crtc
->x_hint
+
218 vbox_connector
->mode_hint
.width
);
219 height
= max_t(u16
, height
, vbox_crtc
->y_hint
+
220 vbox_connector
->mode_hint
.height
);
223 vbox
->single_framebuffer
= false;
224 vbox
->input_mapping_width
= width
;
225 vbox
->input_mapping_height
= height
;
227 return old_single_framebuffer
!= vbox
->single_framebuffer
;
230 static int vbox_crtc_do_set_base(struct drm_crtc
*crtc
,
231 struct drm_framebuffer
*old_fb
, int x
, int y
)
233 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
234 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
235 struct drm_gem_object
*obj
;
236 struct vbox_framebuffer
*vbox_fb
;
241 /* Unpin the previous fb. */
243 vbox_fb
= to_vbox_framebuffer(old_fb
);
245 bo
= gem_to_vbox_bo(obj
);
246 ret
= vbox_bo_reserve(bo
, false);
251 vbox_bo_unreserve(bo
);
254 vbox_fb
= to_vbox_framebuffer(CRTC_FB(crtc
));
256 bo
= gem_to_vbox_bo(obj
);
258 ret
= vbox_bo_reserve(bo
, false);
262 ret
= vbox_bo_pin(bo
, TTM_PL_FLAG_VRAM
, &gpu_addr
);
264 vbox_bo_unreserve(bo
);
268 if (&vbox
->fbdev
->afb
== vbox_fb
)
269 vbox_fbdev_set_base(vbox
, gpu_addr
);
270 vbox_bo_unreserve(bo
);
272 /* vbox_set_start_address_crt1(crtc, (u32)gpu_addr); */
273 vbox_crtc
->fb_offset
= gpu_addr
;
274 if (vbox_set_up_input_mapping(vbox
)) {
275 struct drm_crtc
*crtci
;
277 list_for_each_entry(crtci
, &vbox
->dev
->mode_config
.crtc_list
,
280 vbox_do_modeset(crtci
, &crtci
->mode
);
287 static int vbox_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
288 struct drm_framebuffer
*old_fb
)
290 return vbox_crtc_do_set_base(crtc
, old_fb
, x
, y
);
293 static int vbox_crtc_mode_set(struct drm_crtc
*crtc
,
294 struct drm_display_mode
*mode
,
295 struct drm_display_mode
*adjusted_mode
,
296 int x
, int y
, struct drm_framebuffer
*old_fb
)
298 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
301 vbox_crtc_mode_set_base(crtc
, x
, y
, old_fb
);
303 mutex_lock(&vbox
->hw_mutex
);
304 ret
= vbox_set_view(crtc
);
306 vbox_do_modeset(crtc
, mode
);
307 hgsmi_update_input_mapping(vbox
->guest_pool
, 0, 0,
308 vbox
->input_mapping_width
,
309 vbox
->input_mapping_height
);
310 mutex_unlock(&vbox
->hw_mutex
);
315 static void vbox_crtc_disable(struct drm_crtc
*crtc
)
319 static void vbox_crtc_prepare(struct drm_crtc
*crtc
)
323 static void vbox_crtc_commit(struct drm_crtc
*crtc
)
327 static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs
= {
328 .dpms
= vbox_crtc_dpms
,
329 .mode_fixup
= vbox_crtc_mode_fixup
,
330 .mode_set
= vbox_crtc_mode_set
,
331 /* .mode_set_base = vbox_crtc_mode_set_base, */
332 .disable
= vbox_crtc_disable
,
333 .load_lut
= vbox_crtc_load_lut
,
334 .prepare
= vbox_crtc_prepare
,
335 .commit
= vbox_crtc_commit
,
338 static void vbox_crtc_reset(struct drm_crtc
*crtc
)
342 static void vbox_crtc_destroy(struct drm_crtc
*crtc
)
344 drm_crtc_cleanup(crtc
);
348 static const struct drm_crtc_funcs vbox_crtc_funcs
= {
349 .cursor_move
= vbox_cursor_move
,
350 .cursor_set2
= vbox_cursor_set2
,
351 .reset
= vbox_crtc_reset
,
352 .set_config
= drm_crtc_helper_set_config
,
353 /* .gamma_set = vbox_crtc_gamma_set, */
354 .destroy
= vbox_crtc_destroy
,
357 static struct vbox_crtc
*vbox_crtc_init(struct drm_device
*dev
, unsigned int i
)
359 struct vbox_crtc
*vbox_crtc
;
361 vbox_crtc
= kzalloc(sizeof(*vbox_crtc
), GFP_KERNEL
);
365 vbox_crtc
->crtc_id
= i
;
367 drm_crtc_init(dev
, &vbox_crtc
->base
, &vbox_crtc_funcs
);
368 drm_mode_crtc_set_gamma_size(&vbox_crtc
->base
, 256);
369 drm_crtc_helper_add(&vbox_crtc
->base
, &vbox_crtc_helper_funcs
);
374 static void vbox_encoder_destroy(struct drm_encoder
*encoder
)
376 drm_encoder_cleanup(encoder
);
380 static struct drm_encoder
*vbox_best_single_encoder(struct drm_connector
383 int enc_id
= connector
->encoder_ids
[0];
385 /* pick the encoder ids */
387 return drm_encoder_find(connector
->dev
, enc_id
);
392 static const struct drm_encoder_funcs vbox_enc_funcs
= {
393 .destroy
= vbox_encoder_destroy
,
396 static void vbox_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
400 static bool vbox_mode_fixup(struct drm_encoder
*encoder
,
401 const struct drm_display_mode
*mode
,
402 struct drm_display_mode
*adjusted_mode
)
407 static void vbox_encoder_mode_set(struct drm_encoder
*encoder
,
408 struct drm_display_mode
*mode
,
409 struct drm_display_mode
*adjusted_mode
)
413 static void vbox_encoder_prepare(struct drm_encoder
*encoder
)
417 static void vbox_encoder_commit(struct drm_encoder
*encoder
)
421 static const struct drm_encoder_helper_funcs vbox_enc_helper_funcs
= {
422 .dpms
= vbox_encoder_dpms
,
423 .mode_fixup
= vbox_mode_fixup
,
424 .prepare
= vbox_encoder_prepare
,
425 .commit
= vbox_encoder_commit
,
426 .mode_set
= vbox_encoder_mode_set
,
429 static struct drm_encoder
*vbox_encoder_init(struct drm_device
*dev
,
432 struct vbox_encoder
*vbox_encoder
;
434 vbox_encoder
= kzalloc(sizeof(*vbox_encoder
), GFP_KERNEL
);
438 drm_encoder_init(dev
, &vbox_encoder
->base
, &vbox_enc_funcs
,
439 DRM_MODE_ENCODER_DAC
, NULL
);
440 drm_encoder_helper_add(&vbox_encoder
->base
, &vbox_enc_helper_funcs
);
442 vbox_encoder
->base
.possible_crtcs
= 1 << i
;
443 return &vbox_encoder
->base
;
447 * Generate EDID data with a mode-unique serial number for the virtual
448 * monitor to try to persuade Unity that different modes correspond to
449 * different monitors and it should not try to force the same resolution on
452 static void vbox_set_edid(struct drm_connector
*connector
, int width
,
455 enum { EDID_SIZE
= 128 };
456 unsigned char edid
[EDID_SIZE
] = {
457 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
458 0x58, 0x58, /* manufacturer (VBX) */
459 0x00, 0x00, /* product code */
460 0x00, 0x00, 0x00, 0x00, /* serial number goes here */
461 0x01, /* week of manufacture */
462 0x00, /* year of manufacture */
463 0x01, 0x03, /* EDID version */
464 0x80, /* capabilities - digital */
465 0x00, /* horiz. res in cm, zero for projectors */
466 0x00, /* vert. res in cm */
467 0x78, /* display gamma (120 == 2.2). */
468 0xEE, /* features (standby, suspend, off, RGB, std */
469 /* colour space, preferred timing mode) */
470 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
471 /* chromaticity for standard colour space. */
472 0x00, 0x00, 0x00, /* no default timings */
473 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
475 0x01, 0x01, 0x01, 0x01, /* no standard timings */
476 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
478 /* descriptor block 1 goes below */
479 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
480 /* descriptor block 2, monitor ranges */
481 0x00, 0x00, 0x00, 0xFD, 0x00,
482 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
484 /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
486 /* descriptor block 3, monitor name */
487 0x00, 0x00, 0x00, 0xFC, 0x00,
488 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
490 /* descriptor block 4: dummy data */
491 0x00, 0x00, 0x00, 0x10, 0x00,
492 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
493 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
495 0x00, /* number of extensions */
496 0x00 /* checksum goes here */
498 int clock
= (width
+ 6) * (height
+ 6) * 60 / 10000;
499 unsigned int i
, sum
= 0;
501 edid
[12] = width
& 0xff;
502 edid
[13] = width
>> 8;
503 edid
[14] = height
& 0xff;
504 edid
[15] = height
>> 8;
505 edid
[54] = clock
& 0xff;
506 edid
[55] = clock
>> 8;
507 edid
[56] = width
& 0xff;
508 edid
[58] = (width
>> 4) & 0xf0;
509 edid
[59] = height
& 0xff;
510 edid
[61] = (height
>> 4) & 0xf0;
511 for (i
= 0; i
< EDID_SIZE
- 1; ++i
)
513 edid
[EDID_SIZE
- 1] = (0x100 - (sum
& 0xFF)) & 0xFF;
514 drm_mode_connector_update_edid_property(connector
, (struct edid
*)edid
);
517 static int vbox_get_modes(struct drm_connector
*connector
)
519 struct vbox_connector
*vbox_connector
= NULL
;
520 struct drm_display_mode
*mode
= NULL
;
521 struct vbox_private
*vbox
= NULL
;
522 unsigned int num_modes
= 0;
523 int preferred_width
, preferred_height
;
525 vbox_connector
= to_vbox_connector(connector
);
526 vbox
= connector
->dev
->dev_private
;
528 * Heuristic: we do not want to tell the host that we support dynamic
529 * resizing unless we feel confident that the user space client using
530 * the video driver can handle hot-plug events. So the first time modes
531 * are queried after a "master" switch we tell the host that we do not,
532 * and immediately after we send the client a hot-plug notification as
533 * a test to see if they will respond and query again.
534 * That is also the reason why capabilities are reported to the host at
535 * this place in the code rather than elsewhere.
536 * We need to report the flags location before reporting the IRQ
539 hgsmi_report_flags_location(vbox
->guest_pool
, GUEST_HEAP_OFFSET(vbox
) +
541 if (vbox_connector
->vbox_crtc
->crtc_id
== 0)
542 vbox_report_caps(vbox
);
543 if (!vbox
->initial_mode_queried
) {
544 if (vbox_connector
->vbox_crtc
->crtc_id
== 0) {
545 vbox
->initial_mode_queried
= true;
546 vbox_report_hotplug(vbox
);
548 return drm_add_modes_noedid(connector
, 800, 600);
550 num_modes
= drm_add_modes_noedid(connector
, 2560, 1600);
551 preferred_width
= vbox_connector
->mode_hint
.width
?
552 vbox_connector
->mode_hint
.width
: 1024;
553 preferred_height
= vbox_connector
->mode_hint
.height
?
554 vbox_connector
->mode_hint
.height
: 768;
555 mode
= drm_cvt_mode(connector
->dev
, preferred_width
, preferred_height
,
556 60, false, false, false);
558 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
559 drm_mode_probed_add(connector
, mode
);
562 vbox_set_edid(connector
, preferred_width
, preferred_height
);
564 if (vbox_connector
->vbox_crtc
->x_hint
!= -1)
565 drm_object_property_set_value(&connector
->base
,
566 vbox
->dev
->mode_config
.suggested_x_property
,
567 vbox_connector
->vbox_crtc
->x_hint
);
569 drm_object_property_set_value(&connector
->base
,
570 vbox
->dev
->mode_config
.suggested_x_property
, 0);
572 if (vbox_connector
->vbox_crtc
->y_hint
!= -1)
573 drm_object_property_set_value(&connector
->base
,
574 vbox
->dev
->mode_config
.suggested_y_property
,
575 vbox_connector
->vbox_crtc
->y_hint
);
577 drm_object_property_set_value(&connector
->base
,
578 vbox
->dev
->mode_config
.suggested_y_property
, 0);
583 static int vbox_mode_valid(struct drm_connector
*connector
,
584 struct drm_display_mode
*mode
)
589 static void vbox_connector_destroy(struct drm_connector
*connector
)
591 struct vbox_connector
*vbox_connector
;
593 vbox_connector
= to_vbox_connector(connector
);
594 drm_connector_unregister(connector
);
595 drm_connector_cleanup(connector
);
599 static enum drm_connector_status
600 vbox_connector_detect(struct drm_connector
*connector
, bool force
)
602 struct vbox_connector
*vbox_connector
;
604 vbox_connector
= to_vbox_connector(connector
);
606 return vbox_connector
->mode_hint
.disconnected
?
607 connector_status_disconnected
: connector_status_connected
;
610 static int vbox_fill_modes(struct drm_connector
*connector
, u32 max_x
,
613 struct vbox_connector
*vbox_connector
;
614 struct drm_device
*dev
;
615 struct drm_display_mode
*mode
, *iterator
;
617 vbox_connector
= to_vbox_connector(connector
);
618 dev
= vbox_connector
->base
.dev
;
619 list_for_each_entry_safe(mode
, iterator
, &connector
->modes
, head
) {
620 list_del(&mode
->head
);
621 drm_mode_destroy(dev
, mode
);
624 return drm_helper_probe_single_connector_modes(connector
, max_x
, max_y
);
627 static const struct drm_connector_helper_funcs vbox_connector_helper_funcs
= {
628 .mode_valid
= vbox_mode_valid
,
629 .get_modes
= vbox_get_modes
,
630 .best_encoder
= vbox_best_single_encoder
,
633 static const struct drm_connector_funcs vbox_connector_funcs
= {
634 .dpms
= drm_helper_connector_dpms
,
635 .detect
= vbox_connector_detect
,
636 .fill_modes
= vbox_fill_modes
,
637 .destroy
= vbox_connector_destroy
,
640 static int vbox_connector_init(struct drm_device
*dev
,
641 struct vbox_crtc
*vbox_crtc
,
642 struct drm_encoder
*encoder
)
644 struct vbox_connector
*vbox_connector
;
645 struct drm_connector
*connector
;
647 vbox_connector
= kzalloc(sizeof(*vbox_connector
), GFP_KERNEL
);
651 connector
= &vbox_connector
->base
;
652 vbox_connector
->vbox_crtc
= vbox_crtc
;
654 drm_connector_init(dev
, connector
, &vbox_connector_funcs
,
655 DRM_MODE_CONNECTOR_VGA
);
656 drm_connector_helper_add(connector
, &vbox_connector_helper_funcs
);
658 connector
->interlace_allowed
= 0;
659 connector
->doublescan_allowed
= 0;
661 drm_mode_create_suggested_offset_properties(dev
);
662 drm_object_attach_property(&connector
->base
,
663 dev
->mode_config
.suggested_x_property
, 0);
664 drm_object_attach_property(&connector
->base
,
665 dev
->mode_config
.suggested_y_property
, 0);
666 drm_connector_register(connector
);
668 drm_mode_connector_attach_encoder(connector
, encoder
);
673 int vbox_mode_init(struct drm_device
*dev
)
675 struct vbox_private
*vbox
= dev
->dev_private
;
676 struct drm_encoder
*encoder
;
677 struct vbox_crtc
*vbox_crtc
;
681 /* vbox_cursor_init(dev); */
682 for (i
= 0; i
< vbox
->num_crtcs
; ++i
) {
683 vbox_crtc
= vbox_crtc_init(dev
, i
);
686 encoder
= vbox_encoder_init(dev
, i
);
689 ret
= vbox_connector_init(dev
, vbox_crtc
, encoder
);
697 void vbox_mode_fini(struct drm_device
*dev
)
699 /* vbox_cursor_fini(dev); */
703 * Copy the ARGB image and generate the mask, which is needed in case the host
704 * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set
705 * if the corresponding alpha value in the ARGB image is greater than 0xF0.
707 static void copy_cursor_image(u8
*src
, u8
*dst
, u32 width
, u32 height
,
710 size_t line_size
= (width
+ 7) / 8;
713 memcpy(dst
+ mask_size
, src
, width
* height
* 4);
714 for (i
= 0; i
< height
; ++i
)
715 for (j
= 0; j
< width
; ++j
)
716 if (((u32
*)src
)[i
* width
+ j
] > 0xf0000000)
717 dst
[i
* line_size
+ j
/ 8] |= (0x80 >> (j
% 8));
720 static int vbox_cursor_set2(struct drm_crtc
*crtc
, struct drm_file
*file_priv
,
721 u32 handle
, u32 width
, u32 height
,
722 s32 hot_x
, s32 hot_y
)
724 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
725 struct vbox_crtc
*vbox_crtc
= to_vbox_crtc(crtc
);
726 struct ttm_bo_kmap_obj uobj_map
;
727 size_t data_size
, mask_size
;
728 struct drm_gem_object
*obj
;
737 * Re-set this regularly as in 5.0.20 and earlier the information was
738 * lost on save and restore.
740 hgsmi_update_input_mapping(vbox
->guest_pool
, 0, 0,
741 vbox
->input_mapping_width
,
742 vbox
->input_mapping_height
);
744 bool cursor_enabled
= false;
745 struct drm_crtc
*crtci
;
748 vbox_crtc
->cursor_enabled
= false;
749 list_for_each_entry(crtci
, &vbox
->dev
->mode_config
.crtc_list
,
751 if (to_vbox_crtc(crtci
)->cursor_enabled
)
752 cursor_enabled
= true;
756 hgsmi_update_pointer_shape(vbox
->guest_pool
, 0, 0, 0,
761 vbox_crtc
->cursor_enabled
= true;
763 if (width
> VBOX_MAX_CURSOR_WIDTH
|| height
> VBOX_MAX_CURSOR_HEIGHT
||
764 width
== 0 || height
== 0)
767 ret
= hgsmi_query_conf(vbox
->guest_pool
,
768 VBOX_VBVA_CONF32_CURSOR_CAPABILITIES
, &caps
);
772 if (!(caps
& VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE
)) {
774 * -EINVAL means cursor_set2() not supported, -EAGAIN means
780 obj
= drm_gem_object_lookup(file_priv
, handle
);
782 DRM_ERROR("Cannot find cursor object %x for crtc\n", handle
);
786 bo
= gem_to_vbox_bo(obj
);
787 ret
= vbox_bo_reserve(bo
, false);
792 * The mask must be calculated based on the alpha
793 * channel, one bit per ARGB word, and must be 32-bit
796 mask_size
= ((width
+ 7) / 8 * height
+ 3) & ~3;
797 data_size
= width
* height
* 4 + mask_size
;
798 vbox
->cursor_hot_x
= min_t(u32
, max(hot_x
, 0), width
);
799 vbox
->cursor_hot_y
= min_t(u32
, max(hot_y
, 0), height
);
800 vbox
->cursor_width
= width
;
801 vbox
->cursor_height
= height
;
802 vbox
->cursor_data_size
= data_size
;
803 dst
= vbox
->cursor_data
;
805 ret
= ttm_bo_kmap(&bo
->bo
, 0, bo
->bo
.num_pages
, &uobj_map
);
807 vbox
->cursor_data_size
= 0;
808 goto out_unreserve_bo
;
811 src
= ttm_kmap_obj_virtual(&uobj_map
, &src_isiomem
);
813 DRM_ERROR("src cursor bo not in main memory\n");
818 copy_cursor_image(src
, dst
, width
, height
, mask_size
);
820 flags
= VBOX_MOUSE_POINTER_VISIBLE
| VBOX_MOUSE_POINTER_SHAPE
|
821 VBOX_MOUSE_POINTER_ALPHA
;
822 ret
= hgsmi_update_pointer_shape(vbox
->guest_pool
, flags
,
823 vbox
->cursor_hot_x
, vbox
->cursor_hot_y
,
824 width
, height
, dst
, data_size
);
826 ttm_bo_kunmap(&uobj_map
);
828 vbox_bo_unreserve(bo
);
830 drm_gem_object_unreference_unlocked(obj
);
835 static int vbox_cursor_move(struct drm_crtc
*crtc
, int x
, int y
)
837 struct vbox_private
*vbox
= crtc
->dev
->dev_private
;
838 u32 flags
= VBOX_MOUSE_POINTER_VISIBLE
|
839 VBOX_MOUSE_POINTER_SHAPE
| VBOX_MOUSE_POINTER_ALPHA
;
841 vbox
->single_framebuffer
? crtc
->x
: to_vbox_crtc(crtc
)->x_hint
;
843 vbox
->single_framebuffer
? crtc
->y
: to_vbox_crtc(crtc
)->y_hint
;
850 * We compare these to unsigned later and don't
851 * need to handle negative.
853 if (x
+ crtc_x
< 0 || y
+ crtc_y
< 0 || vbox
->cursor_data_size
== 0)
856 ret
= hgsmi_cursor_position(vbox
->guest_pool
, true, x
+ crtc_x
,
857 y
+ crtc_y
, &host_x
, &host_y
);
860 * The only reason we have vbox_cursor_move() is that some older clients
861 * might use DRM_IOCTL_MODE_CURSOR instead of DRM_IOCTL_MODE_CURSOR2 and
862 * use DRM_MODE_CURSOR_MOVE to set the hot-spot.
864 * However VirtualBox 5.0.20 and earlier has a bug causing it to return
865 * 0,0 as host cursor location after a save and restore.
867 * To work around this we ignore a 0, 0 return, since missing the odd
868 * time when it legitimately happens is not going to hurt much.
870 if (ret
|| (host_x
== 0 && host_y
== 0))
873 if (x
+ crtc_x
< host_x
)
874 hot_x
= min(host_x
- x
- crtc_x
, vbox
->cursor_width
);
875 if (y
+ crtc_y
< host_y
)
876 hot_y
= min(host_y
- y
- crtc_y
, vbox
->cursor_height
);
878 if (hot_x
== vbox
->cursor_hot_x
&& hot_y
== vbox
->cursor_hot_y
)
881 vbox
->cursor_hot_x
= hot_x
;
882 vbox
->cursor_hot_y
= hot_y
;
884 return hgsmi_update_pointer_shape(vbox
->guest_pool
, flags
,
885 hot_x
, hot_y
, vbox
->cursor_width
, vbox
->cursor_height
,
886 vbox
->cursor_data
, vbox
->cursor_data_size
);